Sub topic 14
Using ECR images from ECS and EKS
This chapter assumes you can push an image to ECR, you understand IAM roles and policies, and you have met ECS task definitions and Kubernetes pod specs. Here you will see which identity pulls the image, which permissions it needs, and how to diagnose the usual pull failures.
How a pull works
ECS and EKS do not use your docker login session. The service pulls the image on its own, using an IAM role attached to the task or node. That role requests an authorization token from ECR, then downloads the manifest and layers.
ECS: the task execution role
In ECS, the image pull uses the task execution role, not the task role. The task role is only for your application code.
The execution role needs these actions:
ecr:GetAuthorizationTokenecr:BatchCheckLayerAvailabilityecr:GetDownloadUrlForLayerecr:BatchGetImage
The AWS managed policy AmazonECSTaskExecutionRolePolicy includes all four. It also grants CloudWatch Logs permissions.
Reference the image by its full URI in the task definition. Using a digest keeps the deployment reproducible, as in the previous chapter on tags and digests.
{
"family": "my-app",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "my-app",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app@sha256:<digest>",
"essential": true
}
]
}Replace <digest> with a value from aws ecr describe-images.
EKS: the node role
On EKS with managed or self-managed nodes, the kubelet pulls images using the node's IAM role. Attach AmazonEC2ContainerRegistryReadOnly to that role, or the newer pull-only managed policy AmazonEC2ContainerRegistryPullOnly. The kubelet fetches and refreshes ECR tokens automatically, so you do not create an imagePullSecret.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-app
image: 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0On EKS Fargate, the pod execution role plays the same part as the node role.
Network path
Tasks and pods in private subnets need a route to ECR. You can use a NAT gateway, or VPC endpoints for these services:
| Endpoint | Purpose |
|---|---|
| `com.amazonaws.<region>.ecr.api` | ECR API calls such as token requests |
| `com.amazonaws.<region>.ecr.dkr` | Docker registry protocol |
| `com.amazonaws.<region>.s3` (gateway) | Layer downloads, because ECR stores layers in S3 |
If your tasks send logs with the awslogs driver, they also need a CloudWatch Logs endpoint.
Common errors
| Symptom | Likely cause |
|---|---|
| ECS `CannotPullContainerError` with an authorization or "no basic auth credentials" message | The execution role lacks `ecr:GetAuthorizationToken`, or the role is not set on the task definition |
| ECS `CannotPullContainerError` with a timeout | No route to ECR or S3 from the subnet |
| EKS `ImagePullBackOff` with a 401 or 403 | The node role lacks ECR read permissions, or the repository policy blocks the account |
| `manifest unknown` or "not found" | Wrong tag, digest, Region, or account ID in the URI. The tag may also have been expired by a lifecycle policy |
Check the Region in the image URI first. ECR is regional, so an image pushed to us-east-1 is not visible in eu-west-1 unless you replicate it.
Exercise
An ECS Fargate task in a private subnet fails with a CannotPullContainerError timeout. The execution role has AmazonECSTaskExecutionRolePolicy attached. Which network resources should you check or add?
Show answer
The subnet needs a route to ECR and S3. Use a NAT gateway, or create VPC endpoints for ecr.api and ecr.dkr plus an S3 gateway endpoint. Add a CloudWatch Logs endpoint if the task uses the awslogs driver. The IAM permissions are already covered by the managed policy, so the timeout points to the network.
Exercise
Your application code inside an ECS task needs to read from S3. Which role gets that permission, and which role pulls the image from ECR?
Show answer
The task role gets the S3 permission because it is the identity your application code uses. The task execution role pulls the image from ECR.