Prerequisites
You will need the following to successfully complete this guide:- A Prefect server. You will need either:
- Prefect Cloud account on Starter tier or above
- Prefect self-managed instance
- An AWS account with permissions to create:
- IAM roles
- IAM policies
- Secrets in AWS Secrets Manager or Systems Manager Parameter Store
- ECS task definitions
- ECS services
- The AWS CLI installed on your local machine. You can download it from the AWS website.
- An existing ECS Cluster
- An existing VPC - this guide assumes the use the default VPC.
What if I don't have an ECS cluster?
What if I don't have an ECS cluster?
Create the Prefect ECS work pool
First, create an ECS work pool for your deployments to use. You can do this either from the CLI or the Prefect UI. If doing so from the CLI, be sure to authenticate with Prefect Cloud or run a local Prefect server instance.- From the CLI
- From the Web UI
my-ecs-pool:Create a Secret for the Prefect API key
PREFECT_API_KEY) or Basic Auth string (PREFECT_API_AUTH_STRING) to the Prefect API. As a security best practice, we recommend you store your Prefect API key in AWS Secrets Manager or Systems Manager Parameter Store.
Find your secret
Prefect Cloud - Paid Plans
Prefect Cloud - Paid Plans
Prefect Cloud - Free Plans
Prefect Cloud - Free Plans
Self-hosted Prefect server
Self-hosted Prefect server
PREFECT_API_KEY in a self-hosted Prefect server.Instead, you use the PREFECT_API_AUTH_STRING containing your basic auth credentials (if your server uses basic authentication).You can find this information on the Settings page for your Prefect server.Create a secret
- Secrets Manager
- Systems Manager Parameter Store
aws secretsmanager create-secret command:Create the AWS IAM resources
We will create two IAM roles:ecsTaskExecutionRole: This role will be used by ECS to start ECS tasks.ecsTaskRole: This role will contain the permissions required by Prefect ECS worker in order to run your flows as ECS tasks.
Create a trust policy
The trust policy will allow the ECS service containing the Prefect worker to assume the role required for calling other AWS services. This is called a service-linked role. The trust policy is a JSON document that specifies which AWS service can assume the role. Save this policy to a file, such astrust-policy.json:
Create the IAM roles
Now, we will create the IAM roles that will be used by the ECS worker.Create the ECS task execution role
The ECS task execution role will be used to start the ECS worker task. We will assign it a minimal set of permissions to allow the worker to pull images from ECR and publish logs to CloudWatch.Create the role
aws iam create-role command:Create the Secret Policy
- Secrets Manager
- Systems Manager Parameter Store
secret-policy.json:Using a customer-managed key (CMK)?
Using a customer-managed key (CMK)?
kms:Decrypt permission to the policy. For example:Register the policy
ecsTaskExecutionPolicy using the policy document you just created.Attach the Policies
AmazonECSTaskExecutionRolePolicy managed policy grants the minimum permissions necessary for starting ECS tasks. See here for other common execution role permissions.Attach this policy to your task execution role using the aws iam attach-role-policy:Create the worker ECS task role
The worker ECS task role will be used by the Prefect worker to interact with the AWS API to run flows as ECS containers. This role will require the ability to describe, register, and deregister ECS task definitions, as well as the ability to start and stop ECS tasks.Create the role
Create the task policy
worker-policy.json:Register the policy
ecsTaskPolicy using the policy document you just created.Attach policy to the role
ecsTaskPolicy to the ecsTaskRole so that the Prefect worker can dispatch flows to ECS:<your-account-id> with your AWS account ID.Create an ECS task role for Prefect flows
Depending on the requirements of your flows, it is advised to create a separate role for your ECS tasks. This role will contain the permissions required by the ECS tasks in which your flows will run. For example, if your workflow loads data into an S3 bucket, you would need a role with additional permissions to access S3.Create flow run IAM role
Create flow run IAM role
Create the role
Create the task policy
prefect-demo-bucket. Save this policy to a file, such as runner-task-policy.json:Register the policy
PrefectECSRunnerTaskPolicy using the policy document you just created:Attach policy to the role
PrefectECSRunnerTaskPolicy IAM policy to the PrefectECSRunnerTaskRole IAM role:<your-account-id> with your AWS account ID.Add Task Role ARN to the work pool
PrefectECSRunnerTaskRole to your ECS work pool.This can be configured two ways:- Globally for all flows in the work pool by setting the Task Role ARN (Optional) field in the work pool configuration.
- On a per-deployment basis by specifying the
task_role_arnjob variable in the deployment configuration.
Configure event monitoring infrastructure
To enable the ECS worker to monitor and update the status of flow runs, we need to set up SQS queues and EventBridge rules that capture ECS task state changes. This infrastructure allows the worker to:- Track when ECS tasks (flow runs) start, stop, or fail
- Update flow run states in real-time based on ECS task events
- Provide better observability and status reporting for your workflows
prefect-aws ecs-worker deploy-events command creates automatically. The worker will use the environment variable PREFECT_INTEGRATIONS_AWS_ECS_OBSERVER_SQS_QUEUE_NAME to discover and read from the events queue.Create SQS queues for event monitoring
<dlq-arn> with the ARN of the dead-letter queue from the previous step, and my-ecs-pool with your work pool name.{work-pool-name}-events for consistency with the automated deployment.Configure SQS queue policy
<queue-arn> with the ARN of the queue created in the previous step.Create EventBridge rule for ECS task state changes
<region>with your AWS region<account-id>with your AWS account ID<cluster-name>with your ECS cluster namemy-ecs-poolwith your work pool name
Finding your cluster ARN
Finding your cluster ARN
Add SQS queue as EventBridge rule target
<queue-arn> with the ARN of the queue created in step 1.Update worker task role with SQS permissions
sqs-policy.json:<region>, <account-id>, and my-ecs-pool-events with your values.Apply the policy to the worker task role:Creating the ECS worker service
Now that all the AWS IAM roles and event monitoring infrastructure have been created, we can deploy the Prefect worker to the ECS cluster.Create the task definition
<ecs-task-execution-role-arn>with the ARN of theecsTaskExecutionRoleyou created in Step 2.
Finding your ECS Task Execution Role ARN
Finding your ECS Task Execution Role ARN
ecsTaskExecutionRole using the following command:<ecs-task-role-arn>with the ARN of theecsTaskRoleyou created in Step 2.
Finding your ECS Task Role ARN
Finding your ECS Task Role ARN
ecsTaskRole using the following command:<prefect-api-url>with the URL of your Prefect Server.
Finding your PREFECT_API_URL
Finding your PREFECT_API_URL
Using the CLI
Using the CLI
For Prefect Cloud
For Prefect Cloud
<aws-arn-of-secret>with the ARN of the resource from Secrets Manager or Systems Manager Parameter Store.my-ecs-pool-eventsin thePREFECT_INTEGRATIONS_AWS_ECS_OBSERVER_SQS_QUEUE_NAMEenvironment variable with your actual queue name from the event monitoring setup.
Finding your Secret ARN
Finding your Secret ARN
Secrets Manager
Secrets Manager
Systems Manager Parameter Store
Systems Manager Parameter Store
Self-hosted Prefect server
Self-hosted Prefect server
PREFECT_API_KEY is not used with a self-hosted Prefect server, you will need to replace the PREFECT_API_KEY environment variable in the task definition secrets with PREFECT_API_AUTH_STRING.task-definition.json:Register task definition
register-task-definition command:task-definition.json with the name of your task definition file.Create the ECS service
<ecs-cluster>with the name of your ECS cluster.<task-definition-arn>with the ARN of the task definition you just registered.<subnet-ids>with a comma-separated list of your VPC subnet IDs.- Replace
<security-group-ids>with a comma-separated list of your VPC security group IDs.
Get default VPC info
Get default VPC info
vpc-abcdef01) of the default VPC, which you can use in the next steps in this section.To find the subnets associated with the default VPC:subnet-12345678 subnet-23456789).Finally, we will need the security group ID for the default VPC:sg-12345678) of the default security group.Copy the subnet IDs and security group ID for use in Step 3.aws ecs create-service command to create an ECS service running on Fargate for the Prefect worker:Verify the Prefect worker is running
Configure work pool defaults
Now that your infrastructure is deployed, you should update your ECS work pool configuration with the resource identifiers so they don’t need to be specified on every deployment.Update work pool via the UI
- Cluster ARN: Set to your ECS cluster ARN (e.g.,
arn:aws:ecs:us-east-1:123456789012:cluster/my-cluster) - VPC ID: Set to your VPC ID (e.g.,
vpc-12345678) - Subnets: Add your subnet IDs (e.g.,
subnet-12345678,subnet-87654321) - Execution Role ARN: Set to the task execution role ARN (e.g.,
arn:aws:iam::123456789012:role/ecsTaskExecutionRole)
Alternative: Update work pool via API
Customize the base job template
The ECS work pool’s base job template defines both the available job variables and how they map to the ECS task definition. You can customize this template to expose additional configuration options that can be overridden per-deployment.Add custom variables to the schema
To add a new variable that can be set per-deployment, you need to:- Add the variable to the
variablessection of the base job template - Reference it in the
job_configurationsection using{{ variable_name }}syntax
prefect.yaml:
Deploy a flow run to your ECS work pool
This guide uses the AWS Elastic Container Registry (ECR) to store a Docker image containing your flow code. To do this, we will write a flow, then deploy it using build and push steps that copy flow code into a Docker image and push that image to an ECR repository.Write a simple test flow
Create an ECR repository
aws ecr create-repository command to create an ECR repository. The name you choose for your repository will be reused in the next step when defining your Prefect deployment.Create a `prefect.yaml` file
prefect.yaml file with the following specification:Deploy the flow
Run!
Troubleshooting
If your worker does not appear in the Prefect UI, check the following:- Ensure that the ECS service is running and that the task definition is registered correctly.
- Check the ECS service logs in CloudWatch to see if there are any errors.
- Verify that the IAM roles have the correct permissions.
- Ensure that the
PREFECT_API_URLandPREFECT_API_KEYenvironment variables are set correctly in the task definition. - For self-hosted Prefect servers, ensure that you replaced
PREFECT_API_KEYfrom the example withPREFECT_API_AUTH_STRINGin the task definition. - Ensure your Prefect ECS worker has network connectivity to the Prefect API. If you are using a private VPC, ensure that there is a NAT gateway or internet gateway configured to allow outbound traffic to the Prefect API.
Event monitoring issues
If flow runs are not updating their status properly, check the event monitoring setup:- Verify the SQS queue was created and is receiving messages from EventBridge
- Check that the EventBridge rule is active and properly configured for your ECS cluster
- Ensure the worker task role has the necessary SQS permissions (
sqs:ReceiveMessage,sqs:DeleteMessage, etc.) - Verify the
PREFECT_INTEGRATIONS_AWS_ECS_OBSERVER_SQS_QUEUE_NAMEenvironment variable is set correctly in the worker task definition - Check CloudWatch logs for any SQS-related errors in the worker logs
Next steps
Now that you are confident your ECS worker is healthy, you can experiment with different work pool configurations.- Do your flow runs require higher
CPU? - Would an EC2
Launch Typespeed up your flow run execution?
