Sub topic 8
Authenticating to ECR with get-login-password
This chapter assumes you can build and run a Docker image, use a terminal, and have the AWS CLI (version 2) installed with credentials configured for an IAM user or role. It also assumes you already created an ECR repository, as covered in the previous chapter.
How ECR authentication differs
Amazon ECR does not use a long-lived password. Docker authenticates to the registry with a temporary token that AWS issues in exchange for your AWS credentials. The token is valid for 12 hours. After that, docker push and docker pull fail until you log in again.
The registry host is specific to your account and region:
<account-id>.dkr.ecr.<region>.amazonaws.comA token works only for the registry host it was issued for. If you use two regions, you log in to each one.
Logging in
The aws ecr get-login-password command prints the token. You pipe it to docker login, which reads it from standard input. The username is always the literal string AWS.
aws ecr get-login-password --region us-east-1 \
| docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.comDocker prints Login Succeeded. Piping the token keeps it out of your shell history and process list.
Then tag and push the image:
docker tag my-app:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0Permissions your identity needs
Logging in and pushing are separate permissions. Your IAM identity needs ecr:GetAuthorizationToken to get a token. That action cannot be limited to one repository, so it uses "Resource": "*". Pushing needs repository-level actions on the specific repository. These include ecr:BatchCheckLayerAvailability, ecr:InitiateLayerUpload, ecr:UploadLayerPart, ecr:CompleteLayerUpload, and ecr:PutImage. Pulling needs ecr:BatchGetImage and ecr:GetDownloadUrlForLayer.
The token carries the permissions of the identity that requested it. A token does not grant more access than your IAM policy allows.
Automating the refresh
Because the token expires, scripts and long-lived workstations need a fresh login. For a script, run the login command at the start of each run. On a workstation, the Amazon ECR Docker credential helper can fetch tokens on demand using your AWS credentials. You install docker-credential-ecr-login and map the registry in ~/.docker/config.json:
{
"credHelpers": {
"123456789012.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
}
}With this in place you skip docker login entirely.
Common errors
| Symptom | Likely cause |
|---|---|
| `no basic auth credentials` | The token expired, or you never logged in to this registry host. |
| `denied: User is not authorized to perform: ecr:...` | The IAM identity lacks the repository action named in the message. |
| `name unknown: The repository ... does not exist` | The repository was not created, or the region or account in the host is wrong. |
| `tag invalid: ... cannot be overwritten` | The repository is immutable and the tag already exists. Use a new tag. |
Exercise
Your CI job logged in to ECR in the morning. A push at 11 p.m. fails with "no basic auth credentials". What happened, and what is the fix?
Show answer
The 12-hour token expired. Run aws ecr get-login-password and pipe it to docker login --username AWS --password-stdin <registry host> at the start of each job, or use the ECR credential helper so tokens are fetched on demand.
Exercise
Why does ecr:GetAuthorizationToken use "Resource": "*" while push actions can be limited to one repository?
Show answer
The token request is made against the registry, not a single repository, so it cannot be scoped to a repository ARN. Push and pull actions act on a specific repository, so you can limit them to that repository's ARN.