David
09/25/2024, 3:05 PMMarvin
09/25/2024, 3:06 PMprefect.yaml
file for ECR, you can follow these steps. Here's an example configuration for Prefect 2.x:
1. Create the ECR Repository:
Ensure you have created an ECR repository in your AWS account.
2. Set up your prefect.yaml
:
yaml
name: ecs-worker-guide
prefect-version: 2.14.20
build:
- prefect_docker.deployments.steps.build_docker_image:
id: build_image
requires: prefect-docker>=0.3.1
image_name: <my-ecr-repo>
tag: latest
dockerfile: auto
push:
- prefect_docker.deployments.steps.push_docker_image:
requires: prefect-docker>=0.3.1
image_name: '{{ build_image.image_name }}'
tag: '{{ build_image.tag }}'
deployments:
- name: my_ecs_deployment
entrypoint: flow.py:my_flow
work_pool:
name: ecs-dev-pool
job_variables:
image: '{{ build_image.image }}'
For Prefect 3.x, the structure is quite similar:
1. Set up your prefect.yaml
:
yaml
build:
- prefect_docker.deployments.steps.build_docker_image:
requires: prefect-docker>=0.3.1
image_name: my-repo/my-image
tag: my-tag
dockerfile: auto
push: true
deployments:
- name: my_deployment
entrypoint: flow.py:my_flow
work_pool:
name: my-process-work-pool
job_variables:
image: '{{ build_image.image }}'
In both cases, the build
section is used to specify how to build your Docker image, and the push
section details how to push that image to a remote repository (ECR in this case). The deployments
section configures the deployment specifics, such as the entry point for your flow and the work pool to use.
Useful Links:
- Prefect 2.x Build Stages: https://docs-2.prefect.io/latest/guides/prefect-deploy/
- Prefect 3.x Build Stages: https://docs-3.prefect.io/3.0/deploy/infrastructure-concepts/prefect-yamlDavid
09/25/2024, 3:18 PMMarvin
09/25/2024, 3:18 PMprefect.yaml
file, you need to ensure that AWS CLI is configured with the necessary permissions. The specific permissions required for ECR include:
- ecr:CreateRepository
- ecr:DescribeRepositories
- ecr:GetAuthorizationToken
- ecr:BatchCheckLayerAvailability
- ecr:CompleteLayerUpload
- ecr:InitiateLayerUpload
- ecr:PutImage
- ecr:UploadLayerPart
To simplify permissions management, you can use the AmazonEC2ContainerRegistryFullAccess
managed policy, which includes the necessary permissions for working with ECR.
Here are the steps and adjustments for both Prefect 2.x and 3.x:
1. Attach the Necessary Permissions:
Ensure you have attached the required permissions to your AWS user or role. You can use the AWS Management Console, AWS CLI, or Infrastructure as Code (IaC) tools like CloudFormation or Terraform.
Attach the following managed policies or custom policies with equivalent permissions:
- AmazonEC2ContainerRegistryFullAccess
2. Configure AWS CLI Authentication:
Ensure your environment is configured to authenticate with AWS. This typically involves setting up your ~/.aws/credentials
file or using environment variables:
sh
aws configure
Alternatively, you can export the credentials as environment variables:
sh
export AWS_ACCESS_KEY_ID=<your_access_key_id>
export AWS_SECRET_ACCESS_KEY=<your_secret_access_key>
3. Configure the Push Step in prefect.yaml
:
For Prefect 2.x:
yaml
build:
- prefect_docker.deployments.steps.build_docker_image:
id: build_image
requires: prefect-docker>=0.3.1
image_name: <my-ecr-repo>
tag: latest
dockerfile: auto
push:
- prefect_docker.deployments.steps.push_docker_image:
requires: prefect-docker>=0.3.1
image_name: '{{ build_image.image_name }}'
tag: '{{ build_image.tag }}'
registry_url: <your_account_id>.dkr.ecr.<region>.<http://amazonaws.com|amazonaws.com>
aws_access_key: $AWS_ACCESS_KEY_ID
aws_secret_access_key: $AWS_SECRET_ACCESS_KEY
For Prefect 3.x:
yaml
build:
- prefect_docker.deployments.steps.build_docker_image:
requires: prefect-docker>=0.3.1
image_name: my-repo/my-image
tag: my-tag
dockerfile: auto
push: true
registry_url: <your_account_id>.dkr.ecr.<region>.<http://amazonaws.com|amazonaws.com>
aws_access_key: $AWS_ACCESS_KEY_ID
aws_secret_access_key: $AWS_SECRET_ACCESS_KEY
With these settings, the aws_access_key
and aws_secret_access_key
fields ensure that your Prefect deployment has the necessary credentials to authenticate and push the Docker image to your ECR repository.
Useful Links:
- Prefect 2.x Authorization Guide: https://docs-2.prefect.io/latest/guides/deployment/push-work-pools/
- Prefect 3.x Authorization Guide: https://docs-3.prefect.io/integrations/prefect-aws/ecs_guideDavid
09/25/2024, 3:30 PMMarvin
09/25/2024, 3:31 PMregistry_url
keyword is not accepted by the BuildApiMixin.build
method in Prefect 3.x. Based on further research, you typically handle Docker registry authorization (including ECR) through environment variables or external tools rather than directly in the prefect.yaml
.
Here are updated steps to configure pushing Docker images to ECR using Prefect 3.x without encountering the registry_url
issue:
1. Ensure AWS CLI and Docker Are Configured Correctly:
Your AWS CLI should be configured with the appropriate credentials, and Docker should be able to use these credentials for ECR.
sh
aws configure
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.<http://amazonaws.com|amazonaws.com>
2. Adjust Your prefect.yaml
to Omit registry_url
and Utilize Environment Variables**:
Prefect will rely on your local Docker configuration to access ECR.
yaml
build:
- prefect_docker.deployments.steps.build_docker_image:
requires: prefect-docker>=0.3.1
image_name: my-repo/my-image
tag: my-tag
dockerfile: auto
push: true
deployments:
- name: my_deployment
entrypoint: flow.py:my_flow
work_pool:
name: my-process-work-pool
job_variables:
image: '{{ build_image.image }}'
This configuration ensures that Docker uses your pre-configured AWS credentials to build and push the Docker image to ECR.
Helpful Documentation:
- Prefect Docker Image Building: https://docs-3.prefect.io/3.0/deploy/infrastructure-examples/docker
- AWS ECR Configuration: https://docs.aws.amazon.com/AmazonECR/latest/userguide/getting-started-cli.htmlDavid
09/25/2024, 7:24 PMMarvin
09/25/2024, 7:25 PMsh
aws iam create-role \
--role-name ecsTaskExecutionRole \
--assume-role-policy-document <file://ecs-trust-policy.json>
Attach Managed Policies:
sh
aws iam attach-role-policy \
--role-name ecsTaskExecutionRole \
--policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
aws iam attach-role-policy \
--role-name ecsTaskExecutionRole \
--policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess
#### Trust Policy (ecs-trust-policy.json)
json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "<http://ecs-tasks.amazonaws.com|ecs-tasks.amazonaws.com>"
},
"Action": "sts:AssumeRole"
}
]
}
### 2. Additional Permissions for Flow Runs
Depending on what your flows need to accomplish, you need additional policies. For common tasks, these are:
#### Access to S3:
json
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::your-bucket-name",
"arn:aws:s3:::your-bucket-name/*"
]
}
### 3. ECS Task Definition
Create an ECS task definition that specifies the Docker image for the Prefect worker, the resources it requires, and the command it should run.
#### Task Definition JSON:
json
{
"family": "prefect-worker-task",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "prefect-worker",
"image": "prefecthq/prefect:latest",
"essential": true,
"command": [
"prefect",
"worker",
"start",
"--pool",
"my-ecs-pool"
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/prefect",
"awslogs-region": "<your-region>",
"awslogs-stream-prefix": "ecs"
}
}
}
]
}
### Reference Documentation
- ECS Worker Guide (Prefect 3.x): https://docs-3.prefect.io/integrations/prefect-aws/ecs_guide
- AWS Managed Policies: AmazonECSTaskExecutionRolePolicy, AmazonEC2ContainerRegistryFullAccess
This setup ensures that your ECS worker has all the necessary permissions to administer a task run in Prefect.David
09/25/2024, 9:04 PMMarvin
09/25/2024, 9:04 PMlogConfiguration
section that sends logs to CloudWatch. Here's an example:
json
{
"family": "prefect-worker-task",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "prefect-worker",
"image": "prefecthq/prefect:latest",
"essential": true,
"command": [
"prefect",
"worker",
"start",
"--pool",
"my-ecs-pool"
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/prefect",
"awslogs-region": "<your-region>",
"awslogs-stream-prefix": "ecs"
}
}
}
]
}
### Prefect Configuration to Enable Logging
You can add environment variables to your prefect.yaml
to ensure that logging is enabled. Here is an example:
yaml
deployments:
- name: my_deployment
entrypoint: flow.py:my_flow
work_pool:
name: my-ecs-pool
job_variables:
image: '{{ build_image.image }}'
environment:
PREFECT_LOGGING_LEVEL: DEBUG
PREFECT_LOGGING_SERVER_ENABLED: "true"
### Monitoring and Accessing Logs
1. AWS CloudWatch Logs:
- Navigate to the CloudWatch service in the AWS Management Console.
- Select “Logs” and then find your log group (e.g., /ecs/prefect
).
- The log streams within this log group will contain the logs from your ECS tasks.
### Understanding Worker Processes
A Prefect worker in ECS is responsible for:
1. Listening for Task Runs: It subscribes to task queues from the Prefect API, waiting for new task runs.
2. Executing Task Runs: When a new task run is received, the worker spin-ups additional containers or processes to execute the task.
3. Reporting Back: After completing the task, the worker will report the status back to the Prefect API.
#### Further Reading
- Prefect ECS Guide: This guide provides detailed steps to set up ECS workers and includes the required policies and configuration details.
- Prefect ECS Worker Guide
- Prefect Deployment Docs: For a deeper dive into Prefect deployments including workers and work pools, refer to the deployment documentation.
- Prefect Deployment Documentation
By enabling CloudWatch logging and configuring your environment properly, you should be able to diagnose why your ECS tasks are failing. This will give you detailed insights into the execution of your tasks and help in troubleshooting.David
09/26/2024, 3:51 PMpull:
- prefect.deployments.steps.set_working_directory:
directory: /opt/prefect/prefect/
do i need to set the working directory to be the same as it is in the Dockerfile here
Step 1/6 : FROM prefecthq/prefect:3.0.3-python3.11
---> e74b4bd49a5a
Step 2/6 : COPY requirements.txt /opt/prefect/prefect/requirements.txt
---> Using cache
---> 2224b500ad76
Step 3/6 : RUN python -m pip install -r /opt/prefect/prefect/requirements.txt
---> Using cache
---> e3935c14c152
Step 4/6 : COPY . /opt/prefect/prefect/
---> 4e6b6f6e4760
Step 5/6 : WORKDIR /opt/prefect/prefect/
---> Running in f53590b71b81
---> 7ec141aba2cb
Step 6/6 : LABEL io.prefect.version=3.0.3
---> Running in 4b641ec30ef9
---> 0af150f8dd4a
Successfully built 0af150f8dd4a
Marvin
09/26/2024, 3:51 PMin the prefect.yaml in the pull stage
pull:
- prefect.deployments.steps.set_working_directory:
directory: /opt/prefect/prefect/
do i need to set the working directory to be the same as it is in the Dockerfile here
```Step 1/6 : FROM prefecthq/prefect:3.0.3-python3.11
---> e74b4bd49a5a
Step 2/6 : COPY requirements.txt /opt/prefect/prefect/requirements.txt
---> Using cache
---> 2224b500ad76
Step 3/6 : RUN python -m pip install -r /opt/prefect/prefect/requirements.txt
---> Using cache
---> e3935c14c152
Step 4/6 : COPY . /opt/prefect/prefect/
---> 4e6b6f6e4760
Step 5/6 : WORKDIR /opt/prefect/prefect/
---> Running in f53590b71b81
---> 7ec141aba2cb
Step 6/6 : LABEL io.prefect.version=3.0.3
---> Running in 4b641ec30ef9
---> 0af150f8ddDavid
09/26/2024, 3:52 PMpull:
- prefect.deployments.steps.set_working_directory:
directory: /opt/prefect/prefect/
do i need to set the working directory to be the same as it is in the Dockerfile here
Step 5/6 : WORKDIR /opt/prefect/prefect/
Marvin
09/26/2024, 3:52 PMpull
stage in your prefect.yaml
file and the WORKDIR
defined in your Dockerfile should indeed be consistent. This consistency ensures that the working directory set during the Docker image build is the same directory where Prefect will look for your files when running tasks.
If in your Dockerfile you have:
Dockerfile
WORKDIR /opt/prefect/prefect/
Then, in your prefect.yaml
you should also set that same directory in the pull
step:
yaml
pull:
- prefect.deployments.steps.set_working_directory:
directory: /opt/prefect/prefect/
### Why Consistency Matters
- Build Time vs. Run Time: The WORKDIR
command in Docker sets the working directory during the build process. Conversely, the pull
step in prefect.yaml
sets the working directory during the runtime, ensuring that any commands or scripts run by Prefect will execute in the correct context.
- File Accessibility: Ensuring that both the build and runtime directories are the same helps avoid issues related to file paths, making sure that your flow has access to all the necessary files and directories expected within the container.
### Further Reading
For more details on configuring the working directory and other deployment settings in Prefect, refer to the Prefect Deployment Documentation and Prefect YAML Configuration Guide.
By keeping these directories consistent, you ensure that the tasks run smoothly and have access to the expected files and scripts, thus avoiding many common runtime errors.David
09/26/2024, 9:21 PMMarvin
09/26/2024, 9:22 PMprefect.yaml
file, you need to incorporate the correct steps in the pull
section. Here is how you can configure it:
### Example Configuration for prefect.yaml
#### build
and push
Steps:
These steps build the Docker image and push it to ECR.
yaml
build:
- prefect_docker.deployments.steps.build_docker_image:
requires: prefect-docker>=0.3.1
image_name: <your-account-id>.dkr.ecr.<region>.<http://amazonaws.com/<my-ecr-repo|amazonaws.com/<my-ecr-repo>>
tag: latest
dockerfile: auto
push:
- prefect_docker.deployments.steps.push_docker_image:
requires: prefect-docker>=0.3.1
image_name: '{{ build_image.image_name }}'
tag: '{{ build_image.tag }}'
#### pull
Step:
This step ensures that the ECS task pulls the correct image.
yaml
deployments:
- name: my_deployment
entrypoint: flow.py:my_flow
work_pool:
name: my-ecs-pool
job_variables:
image: '{{ build_image.image }}:{{ build_image.tag }}'
pull:
- prefect.deployments.steps.set_working_directory:
directory: /opt/prefect/prefect/
### Key Points:
- Consistent Image Name and Tag: Ensure the image_name
and tag
from the build
and push
steps are correctly referenced in the deployments
section.
- Environment Compatibility: {{ build_image.image }}
and {{ build_image.tag }}
ensure that the correct image and tag are dynamically passed to the ECS task.
### Useful Documentation:
For more information, refer to the Prefect ECS Guide.
By setting up your pull
step to reference the ECR image, your ECS tasks will pull and run the correct Docker image as defined in your build and push steps.David
09/27/2024, 3:36 PMRegisterTaskDefinition operation: Fargate requires task definition to have execution role ARN to support ECR images.
11:00:28 AM
prefect.flow_runs.worker
Marvin
09/27/2024, 3:37 PMRegisterTaskDefinition operation: Fargate requires task definition to have execution role ARN to support ECR images
, you need to specify the executionRoleArn
in your ECS task definition when deploying your flow. This includes setting the AWS IAM roles correctly.
### Steps to Include `executionRoleArn`:
1. Define the executionRoleArn
in Your prefect.yaml
:
You will need to ensure that your ECS worker has the correct IAM role that allows it to execute tasks. Here's how you can adjust your configuration:
yaml
deployments:
- name: my_deployment
entrypoint: flow.py:my_flow
work_pool:
name: my-ecs-pool
job_variables:
image: '{{ build_image.image }}:{{ build_image.tag }}'
executionRoleArn: arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole
2. IAM Role for ECS Task Execution:
Ensure you have created and configured the IAM role as mentioned in your AWS setup. Here's a brief reminder for setting up the IAM role:
sh
aws iam create-role \
--role-name ecsTaskExecutionRole \
--assume-role-policy-document <file://ecs-trust-policy.json>
aws iam attach-role-policy \
--role-name ecsTaskExecutionRole \
--policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
### Using `flow.deploy()`:
When using flow.deploy()
to deploy an ECR image, make sure your deployment script includes the necessary arguments for task role ARN:
python
from prefect import flow
from prefect.deployments import Deployment
from prefect_aws.ecs import ECSTask
@flow(log_prints=True)
def my_flow(name: str = "world"):
print(f"Hello {name}")
if __name__ == "__main__":
ecs_task = ECSTask(
image="<your-account-id>.dkr.ecr.<region>.<http://amazonaws.com/<my-ecr-repo>:latest|amazonaws.com/<my-ecr-repo>:latest>",
executionRoleArn="arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole"
)
deployment = Deployment.build_from_flow(
flow=my_flow,
name="ecs-deployment",
work_pool_name="my-ecs-pool",
infrastructure=ecs_task
)
deployment.apply()
### Further Reading:
For more detailed information on configuring ECS tasks and setting the executionRoleArn
, refer to the following resources:
- Prefect ECS Guide
- AWS ECS Task Definitions
- AWS IAM Roles for Tasks
By correctly setting the executionRoleArn
, your ECS tasks should be able to pull images from ECR and execute without issues.David
09/27/2024, 3:38 PMMarvin
09/27/2024, 3:39 PMECSTask
is deprecated in Prefect 3.x, the recommended approach is to define and manage your ECS task definitions and deployments using the appropriate YAML configuration in prefect.yaml
. Here’s how you can set this up to include the executionRoleArn
in your ECS task definition for Prefect 3.x.
### Updated prefect.yaml
Configuration
1. Define the ECS Task with Required Roles:
yaml
build:
- prefect_docker.deployments.steps.build_docker_image:
requires: prefect-docker>=0.3.1
image_name: <your-account-id>.dkr.ecr.<region>.<http://amazonaws.com/<my-ecr-repo|amazonaws.com/<my-ecr-repo>>
tag: latest
dockerfile: auto
push:
- prefect_docker.deployments.steps.push_docker_image:
requires: prefect-docker>=0.3.1
image_name: '{{ build_image.image_name }}'
tag: '{{ build_image.tag }}'
deployments:
- name: my_deployment
entrypoint: flow.py:my_flow
work_pool:
name: my-ecs-pool
job_variables:
image: '{{ build_image.image }}:{{ build_image.tag }}'
executionRoleArn: arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole
pull:
- prefect.deployments.steps.set_working_directory:
directory: /opt/prefect/prefect/
2. Manual ECS Task Definition JSON:
Create an ECS task definition JSON that Prefect will use. This JSON should include the `executionRoleArn`:
json
{
"family": "prefect-worker-task",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskRole",
"containerDefinitions": [
{
"name": "prefect-worker",
"image": "{{ build_image.image }}:{{ build_image.tag }}",
"essential": true,
"command": [
"prefect",
"worker",
"start",
"--pool",
"my-ecs-pool"
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/prefect",
"awslogs-region": "<your-region>",
"awslogs-stream-prefix": "ecs"
}
}
}
]
}
3. Deploy Function in Python:
If you prefer to deploy using Python directly, you can use the prefect.deployments.Deployment
class:
python
from prefect import flow
from prefect.deployments import Deployment
@flow
def my_flow(name: str = "world"):
print(f"Hello {name}")
deployment = Deployment.build_from_flow(
flow=my_flow,
name="ecs-deployment",
work_pool_name="my-ecs-pool",
parameters={
"executionRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole"
}
)
deployment.apply()
### Useful Documentation
Refer to the following links for more specific details:
- Prefect ECS Guide: https://docs-3.prefect.io/integrations/prefect-aws/ecs_guide
- Prefect Deployment Overview: https://docs-3.prefect.io/3.0/deploy/
By updating the prefect.yaml
to include the executionRoleArn
and ensuring that your ECS task definition JSON includes this role, your ECS tasks should be able to pull the ECR images and execute without issues.David
10/01/2024, 3:48 PMdeployments:
- name: healthcheck
entrypoint: healthcheck.py:healthcheck
work_pool:
name: everglade-dev-workpool
job_variables:
image: '{{ build_image.image }}:{{ build_image.tag }}'
cluster: "mycluster"
schedules: []
enforce_parameter_schema: true
why am i getting this issue
File "/usr/local/lib/python3.12/site-packages/prefect_aws/workers/ecs_worker.py", line 1023, in _register_task_definition
response = ecs_client.register_task_definition(**task_definition)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/botocore/client.py", line 569, in _api_call
return self._make_api_call(operation_name, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/botocore/client.py", line 1023, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.ClientException: An error occurred (ClientException) when calling the RegisterTaskDefinition operation: Fargate requires task definition to have execution role ARN to support ECR images.
Marvin
10/01/2024, 3:49 PMexecutionRoleArn
to support ECR images when using Fargate.
To resolve this issue, you need to include the executionRoleArn
in your deployment configuration.
### Updated Deployment Configuration in prefect.yaml
Modify your deployment configuration to include the executionRoleArn
in the job_variables
section under your work pool definition:
yaml
deployments:
- name: healthcheck
entrypoint: healthcheck.py:healthcheck
work_pool:
name: everglade-dev-workpool
job_variables:
image: '{{ build_image.image }}:{{ build_image.tag }}'
cluster: "mycluster"
executionRoleArn: "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole"
schedules: []
enforce_parameter_schema: true
### Steps to Create the Required IAM Role:
Ensure that you have created the IAM role for ECS task execution as follows:
1. Create a Trust Policy (ecs-trust-policy.json):
json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "<http://ecs-tasks.amazonaws.com|ecs-tasks.amazonaws.com>"
},
"Action": "sts:AssumeRole"
}
]
}
2. Create the IAM Role and Attach Policies:
sh
aws iam create-role \
--role-name ecsTaskExecutionRole \
--assume-role-policy-document <file://ecs-trust-policy.json>
aws iam attach-role-policy \
--role-name ecsTaskExecutionRole \
--policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
aws iam attach-role-policy \
--role-name ecsTaskExecutionRole \
--policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess
### Reference Guides:
For more detailed guidance, refer to:
- Prefect ECS Guide: Prefect ECS Worker Guide
- AWS ECS IAM Roles: AWS ECS Task Execution IAM Role
By including the executionRoleArn
in your prefect.yaml
, your ECS tasks will have the necessary permissions to pull the ECR image and execute successfully.David
10/01/2024, 9:11 PMMarvin
10/01/2024, 9:12 PMprefect.yaml
instead of using the UI, you can follow these steps. Here’s how to set this up in Prefect 3.x:
### Using prefect.yaml
to Configure the ECS Worker
1. Create an ECS Work Pool from the CLI:
First, create an ECS work pool using the Prefect CLI:
sh
prefect work-pool create --type ecs my-ecs-pool
2. Configure Your prefect.yaml
:
Update your prefect.yaml
file to include the necessary ECS configurations, including the executionRoleArn
.
yaml
build:
- prefect_docker.deployments.steps.build_docker_image:
requires: prefect-docker>=0.3.1
image_name: <your-account-id>.dkr.ecr.<region>.<http://amazonaws.com/<my-ecr-repo|amazonaws.com/<my-ecr-repo>>
tag: latest
dockerfile: auto
push:
- prefect_docker.deployments.steps.push_docker_image:
requires: prefect-docker>=0.3.1
image_name: '{{ build_image.image_name }}'
tag: '{{ build_image.tag }}'
deployments:
- name: healthcheck
entrypoint: healthcheck.py:healthcheck
work_pool:
name: my-ecs-pool
job_variables:
image: '{{ build_image.image }}:{{ build_image.tag }}'
cluster: "mycluster"
executionRoleArn: "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole"
schedules: []
enforce_parameter_schema: true
3. Define the ECS Task Definition JSON:
Create an ECS task definition JSON file that includes configuration for the task execution role.
json
{
"family": "prefect-worker-task",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::<your-account-id>:role/ecsTaskRole",
"containerDefinitions": [
{
"name": "prefect-worker",
"image": "{{ build_image.image }}:{{ build_image.tag }}",
"essential": true,
"command": [
"prefect",
"worker",
"start",
"--pool",
"my-ecs-pool"
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/prefect",
"awslogs-region": "<your-region>",
"awslogs-stream-prefix": "ecs"
}
}
}
]
}
4. Deploy Using Prefect CLI or Python Client:
Deploy your flow using either the Prefect CLI or the Python client.
### Using Prefect Python Client
If you prefer to configure the ECS worker and deploy using the Python client, here’s how:
1. Install Prefect and Prefect AWS:
sh
pip install prefect prefect-aws
2. Deploy Script:
Use the Prefect Python client to configure and deploy the ECS worker.
python
from prefect import flow
from prefect.deployments import Deployment
from prefect_aws.ecs import ECSTask
@flow
def healthcheck():
print("Running healthcheck...")
# Define ECS Task with executionRoleArn
ecs_task_definition = ECSTask(
image="<your-account-id>.dkr.ecr.<region>.<http://amazonaws.com/<my-ecr-repo>:latest|amazonaws.com/<my-ecr-repo>:latest>",
executionRoleArn="arn:aws:iam::<your-account-id>:role/ecsTaskExecutionRole",
cluster="mycluster"
)
# Create Deployment
deployment = Deployment.build_from_flow(
flow=healthcheck,
name="healthcheck",
infrastructure=ecs_task_definition,
work_pool_name="my-ecs-pool"
)
if __name__ == "__main__":
deployment.apply()
### Reference Guides:
For more detailed guidance, you can refer to:
- Prefect ECS Guide: Prefect ECS Worker Guide
- Prefect Deployment Documentation: Prefect Deployment
By following these steps, you can configure your ECS worker directly from the prefect.yaml
file or the Python client, avoiding the need for manual setup through the UI.