Because of the additional setup required, we recommend deploying workers with Docker Hub or GitHub Container Registry if ECR-specific features aren’t required.
Requirements
Before integrating ECR with Runpod, make sure that you have:- An AWS account with ECR access.
- The AWS CLI configured on your local machine with appropriate permissions.
- A Runpod account with API access.
- A local folder containing all the necessary components to build a worker image: a Dockerfile, handler function, and requirements.txt file.
If you want to test this workflow but haven’t yet created the necessary files, you can download this basic worker template.
Step 1: Create an ECR repository and push your container image
First, create an ECR repository and push your container image.AWS_ACCOUNT_ID with your actual AWS account ID number and adjust the region as needed.
Step 2: Create initial container registry credentials
Generate ECR credentials and add them to Runpod:AWS:password. Use these to create container registry authentication in Runpod:
- REST API
- Python
id value - you’ll need it for the automation script and endpoint configuration.
Step 3: Set up automated credential refresh
Create an AWS Lambda function to automatically refresh ECR credentials in Runpod:Lambda function code
Lambda configuration
- Create a new Lambda function in the AWS Console
- Set the runtime to Python 3.9 or later.
- Add the following environment variables:
RUNPOD_API_KEY: Your Runpod API key.REGISTRY_AUTH_ID: The container registry auth ID from Step 2.
- Attach an IAM role with the
AmazonEC2ContainerRegistryReadOnlypolicy. - Set up an EventBridge (CloudWatch Events) rule to trigger the function every 6 hours:
Step 4: Deploy Serverless endpoint with ECR image
Once credential automation is set up, you can create your Serverless endpoint using the Runpod console or the REST API.- Runpod Console
- REST API
Follow these steps to create your Serverless endpoint in the Runpod console:
- Navigate to the Serverless section.
- Click New Endpoint.
- Select Docker Image as your source.
- Enter your ECR image URL:
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-serverless-worker:latest. - In the Advanced section, select your ECR credentials from the Container Registry Auth dropdown.
- Configure other endpoint settings as needed.
- Click Create Endpoint.
Step 5: Monitor credential refresh
Check your Lambda function logs in CloudWatch to ensure credentials are being refreshed successfully:Common issues and solutions
Image pull failures: If your endpoint fails to pull the ECR image:- Verify the ECR image URL is correct
- Check that the Lambda function is running successfully
- Ensure the container registry auth ID is properly configured
- Confirm your ECR repository permissions allow the necessary access
- Manually trigger the Lambda function to refresh credentials immediately
- Check that the CloudWatch Events rule is properly configured
- Verify the Lambda function has the correct IAM permissions
- Consider using smaller base images to reduce pull times
- Enable FlashBoot in your endpoint configuration for faster cold starts
- Use network volumes to cache frequently accessed data
Best practices
- Monitor Lambda execution: Set up CloudWatch alarms for Lambda function failures.
- Use specific image tags: Avoid using
:latesttags in production deployments. - Implement retry logic: Add error handling and retry mechanisms to your Lambda function.
- Regional considerations: Deploy Lambda functions in the same region as your ECR repositories.
- Security: Use least-privilege IAM policies and rotate Runpod API keys regularly.
Alternative approaches
If automated credential refresh adds complexity to your workflow, consider these alternatives:- Docker Hub: Simpler authentication with longer-lived credentials.
- GitHub Container Registry: Integrated with GitHub workflows and repositories.
- Public ECR: Use Amazon’s public ECR for open-source projects without authentication.