Kahu 22
09/25/2025, 1:44 AMMarvin
09/25/2025, 1:44 AMMarvin
09/25/2025, 1:51 AMpip install -U prefect prefect-aws
- Log in to Prefect Cloud:
prefect cloud login --key <YOUR_API_KEY> --workspace <account>/<workspace>
Docs:
- ECS guide
- prefect.yaml deployments
2) Create an ECS Work Pool
- Create a pool of type ecs:
prefect work-pool create my-ecs-pool --type ecs
- Get the default ECS base job template to customize:
prefect work-pool get-default-base-job-template --type ecs > ecs-template.json
This template includes all the fields the worker needs (image, cluster, cpu/memory, task/execution roles, network config, CloudWatch logs, etc.).
3) Customize the base job template for your environment
- Edit ecs-template.json to specify:
- image: ECR image for your flow container
- cluster: your ECS cluster name/ARN
- launch_type: typically "FARGATE" (or configure capacity providers)
- container_name: container in your task definition (e.g., "flow")
- cpu/memory: task sizes, e.g., "512"/"1024"
- execution_role_arn and task_role_arn
- network_configuration (VPC/Subnets/SecurityGroups)
- optionally task_definition_arn if you want to use a pre-registered definition instead of letting Prefect register one
Example snippet (minimal):
{
"job_configuration": {
"task_definition": {
"containerDefinitions": [
{
"image": "<http://123456789012.dkr.ecr.us-east-1.amazonaws.com/prefect-flows:latest|123456789012.dkr.ecr.us-east-1.amazonaws.com/prefect-flows:latest>",
"name": "flow"
}
],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"family": "prefect-flows"
},
"task_run_request": {
"launchType": "FARGATE",
"cluster": "my-ecs-cluster",
"overrides": {
"containerOverrides": [
{
"name": "flow"
}
],
"taskRoleArn": "arn:aws:iam::123456789012:role/prefect-task-role"
},
"networkConfiguration": {
"awsvpcConfiguration": {
"subnets": [
"subnet-abc",
"subnet-def"
],
"securityGroups": [
"sg-123456"
],
"assignPublicIp": "ENABLED"
}
}
},
"configure_cloudwatch_logs": true,
"cloudwatch_logs_prefix": "/prefect/flows",
"container_name": "flow"
}
}
- Save and apply it to the work pool:
prefect work-pool update my-ecs-pool --base-job-template ecs-template.json
IAM and permissions (important)
- The worker’s AWS credentials must be able to:
- ecs:RunTask, ecs:DescribeTasks, ecs:StopTask
- ecs:RegisterTaskDefinition, ecs:DeregisterTaskDefinition (if you let Prefect register task defs)
- iam:PassRole for the task role and execution role used by your tasks
- Your task execution role should have the AmazonECSTaskExecutionRolePolicy (pulls from ECR, writes CloudWatch logs).
- If pulling from ECR private repos, ensure the execution role/ECS is allowed to pull images.
4) Run a Prefect ECS worker
- Run the worker anywhere that has AWS access (commonly as its own ECS service with a task role), then:
prefect worker start --pool my-ecs-pool
Marvin
09/25/2025, 1:51 AMprefect init
- Edit prefect.yaml to define a deployment targeting your ECS pool and (optionally) override any job_variables from the base job template:
name: my-project
prefect-version: ">=3.0.0"
deployments:
- name: etl
entrypoint: flows/etl.py:etl
work_pool:
name: my-ecs-pool
job_variables:
image: <http://123456789012.dkr.ecr.us-east-1.amazonaws.com/prefect-flows:latest|123456789012.dkr.ecr.us-east-1.amazonaws.com/prefect-flows:latest>
cluster: my-ecs-cluster
launch_type: FARGATE
container_name: flow
cpu: "512"
memory: "1024"
task_role_arn: arn:aws:iam::123456789012:role/prefect-task-role
execution_role_arn: arn:aws:iam::123456789012:role/ecsTaskExecutionRole
network_configuration:
awsvpcConfiguration:
subnets: ["subnet-abc", "subnet-def"]
securityGroups: ["sg-123456"]
assignPublicIp: ENABLED
- Deploy:
prefect deploy
Option B: Source-based deployments (clone your repo at run time)
- You can do:
from prefect import flow
flow.from_source(
"<https://github.com/your-org/your-repo>",
entrypoint="flows/etl.py:etl",
).deploy(name="etl", work_pool_name="my-ecs-pool")
- In this case, your base image must include your Python dependencies; the worker will pull code at runtime.
6) Kick off a flow run
- From the CLI:
prefect deployment ls
prefect deployment run "etl/etl"
- From your backend (if you want to trigger from your app running on ECS), add Prefect as a dependency and call:
from prefect.deployments import run_deployment
run = run_deployment(name="etl/etl", parameters={"param": "value"})
Make sure your backend container has PREFECT_API_URL and PREFECT_API_KEY set to reach Prefect Cloud.
Common gotchas
- Make sure the container_name in your template matches the container in your task definition.
- If you see permission errors, add iam:PassRole for both task and execution roles to the worker’s principal.
- For Fargate, use awsvpc networking with subnets/SGs and assignPublicIp if needed for outbound internet.
- In Prefect 3.x, use prefect deploy
and work pools; do not use deprecated 2.x commands like prefect deployment build
or Deployment.build_from_flow()
.
Helpful docs
- ECS guide: Run flows on AWS ECS
- Prefect YAML: prefect.yaml reference
A few questions to tailor this for you
- Are you using Fargate or EC2-backed ECS?
- Do you already have an ECR image for your flow code, or do you prefer cloning from a Git repo at runtime?
- Do you want the Prefect worker to run as its own ECS service in your cluster?
- Which VPC, subnets, and security groups should tasks run in?
- Do you want logs in CloudWatch in addition to Prefect Cloud logs?
If you share those, I can provide a ready-to-use ecs-template.json and prefect.yaml for your setup.Kahu 22
09/25/2025, 1:54 AMMarvin
09/25/2025, 1:54 AMMarvin
09/25/2025, 1:56 AMpip install -U prefect
prefect server start --host 0.0.0.0 --ui
- Expose it via your load balancer/reverse proxy with TLS in front. Set on every client (your dev machine, worker containers, etc.):
prefect config set PREFECT_API_URL=http(s)://<your-server-host>:<port>/api
- For production, run Server as a service (and consider Postgres, backups, and restricted network access).
CLI help we verified:
- prefect server start --help
2) Install AWS integration on the machine that will run your worker
pip install -U prefect prefect-aws
3) Create an ECS Work Pool
- Create:
prefect work-pool create my-ecs-pool --type ecs
- Get and customize the ECS base job template:
prefect work-pool get-default-base-job-template --type ecs > ecs-template.json
Edit ecs-template.json with your details. Since you’re using EC2-backed ECS capacity, set launch_type to EC2 and reference your ECR image and roles:
Example ecs-template.json (minimal EC2 example)
{
"job_configuration": {
"task_definition": {
"family": "prefect-flows",
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "flow",
"image": "<http://123456789012.dkr.ecr.us-east-1.amazonaws.com/prefect-flows:latest|123456789012.dkr.ecr.us-east-1.amazonaws.com/prefect-flows:latest>",
"essential": true
}
]
},
"task_run_request": {
"launchType": "EC2",
"cluster": "my-ecs-cluster",
"overrides": {
"taskRoleArn": "arn:aws:iam::123456789012:role/prefect-task-role",
"containerOverrides": [
{
"name": "flow"
}
]
}
/* For EC2 launch type, only include networkConfiguration if you're using awsvpc on your container instances.
If your tasks use bridge/host networking, omit networkConfiguration here. */
},
"container_name": "flow",
"configure_cloudwatch_logs": true,
"cloudwatch_logs_prefix": "/prefect/flows"
}
}
Apply it:
prefect work-pool update my-ecs-pool --base-job-template ecs-template.json
IAM requirements (common)
- The worker’s AWS principal needs: ecs:RunTask, ecs:DescribeTasks, ecs:StopTask; iam:PassRole for your task/execution roles; and if Prefect should register task definitions: ecs:RegisterTaskDefinition and ecs:DeregisterTaskDefinition.
- The execution role should have AmazonECSTaskExecutionRolePolicy (ECR pulls, CloudWatch logs).
4) Start a Prefect ECS worker
Run this as a long-lived process (systemd on EC2 or an ECS service) with AWS access and PREFECT_API_URL pointing at your Server:
prefect worker start --pool my-ecs-pool
5) Package your flow and create a deployment
Since you already have an ECR image with your code, point deployments at that image and the ECS pool.
- Initialize a Prefect project (in your repo):
prefect init
- Edit prefect.yaml to target your pool and (optionally) override per-deployment job variables:
```
name: my-project
prefect-version: ">=3.0.0"
deployments:
- name: etl
entrypoint: flows/etl.py:etl
work_pool:
name: my-ecs-pool
job_variables:
image: 123456789012.dkr.ecr.us-east-1.amazonaws.com/prefect-flows:latest
cluster: my-ecs-cluster
launch_type: EC2
container_name: flowMarvin
09/25/2025, 1:56 AM- Create the deployment:
prefect deploy
6) Run your flows
- Trigger from CLI:
prefect deployment run "etl/etl"
- Or from code (pointing to your Server via PREFECT_API_URL):
from prefect.deployments import run_deployment
run_deployment(name="etl/etl", parameters={"foo": "bar"})
Optional but recommended
- Persist results/logs to AWS services:
- Results to S3 (configure in your flow/deployment or via settings).
- Logs to CloudWatch (set in ecs-template.json as shown).
- Network posture:
- For ECS EC2 with awsvpc networking, ensure your container instances and tasks are in the proper subnets/SGs (and NAT/IGW for outbound if needed).
Docs and references
- ECS guide: <https://docs-3.prefect.io/integrations/prefect-aws/ecs_guide|Run flows on AWS ECS>
- prefect.yaml reference: <https://docs-3.prefect.io/v3/how-to-guides/deployments/prefect-yaml|prefect.yaml deployments>
- CLI we used: ```prefect server start
, prefect work-pool create --type ecs
, prefect work-pool get-default-base-job-template --type ecs
, prefect work-pool update --base-job-template
, prefect worker start
, prefect deploy
A couple of quick questions to tailor this precisely:
- Do you want to run the Prefect worker as an ECS service or as a systemd service on an EC2 instance?
- Are your ECS tasks using awsvpc networking on EC2 container instances, or bridge/host?
- Do you prefer Prefect to register task definitions automatically, or will you provide a fixed taskDefinition ARN?
If you share those, I can provide a ready-to-paste ecs-template.json and prefect.yaml matching your exact setup.