Marius Haberstock
07/31/2025, 2:40 PMMarvin
07/31/2025, 2:40 PMMarvin
07/31/2025, 2:41 PMjson
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage"
],
"Resource": "*"
}
]
}
- You can use the AWS-managed policy AmazonEC2ContainerRegistryReadOnly
for simple cases
b) Using AWS credentials directly:
- Configure AWS credentials using aws configure
- Store credentials in ~/.aws/credentials
2. Docker Authentication
Once AWS is configured, you need to authenticate Docker with ECR. Add this command to your EC2 instance's startup script or run it before starting the Prefect worker:
bash
aws ecr get-login-password --region YOUR_REGION | docker login --username AWS --password-stdin <http://YOUR_ACCOUNT_ID.dkr.ecr.YOUR_REGION.amazonaws.com|YOUR_ACCOUNT_ID.dkr.ecr.YOUR_REGION.amazonaws.com>
Replace:
- YOUR_REGION
with your AWS region (e.g., us-east-1
)
- YOUR_ACCOUNT_ID
with your AWS account ID
3. Automatic Token Refresh
ECR authentication tokens expire after 12 hours. You can create a simple script to refresh the authentication periodically:
bash
#!/bin/bash
while true; do
aws ecr get-login-password --region YOUR_REGION | docker login --username AWS --password-stdin <http://YOUR_ACCOUNT_ID.dkr.ecr.YOUR_REGION.amazonaws.com|YOUR_ACCOUNT_ID.dkr.ecr.YOUR_REGION.amazonaws.com>
# Sleep for 11 hours (refresh token before 12-hour expiration)
sleep 39600
done
4. Starting the Worker
After authentication is set up, you can start your Prefect Docker worker as normal. The worker will now be able to pull images from ECR using the established authentication.
Best Practices:
1. Use IAM roles instead of hard-coded credentials whenever possible
2. Use the principle of least privilege - only grant the permissions needed for pulling images
3. Consider using AWS Systems Manager Parameter Store or Secrets Manager for managing any sensitive credentials
4. Monitor ECR authentication logs for any issues
Remember that the authentication needs to be renewed every 12 hours, so ensure your refresh mechanism is reliable if you're running long-term workloads.
Would you like me to provide more specific details about any of these aspects or help with troubleshooting specific issues?