Skip to content

Instantly share code, notes, and snippets.

@jsleeio
Created November 28, 2021 02:38
Show Gist options
  • Save jsleeio/6ccafb5e2c82a264de03d754864d5b36 to your computer and use it in GitHub Desktop.
Save jsleeio/6ccafb5e2c82a264de03d754864d5b36 to your computer and use it in GitHub Desktop.
automatic Docker login to ECR via AWS configuration data
docker-login-ecr() {
local ecr_account_profile=${ECR_ACCOUNT_PROFILE:-utility}
local ecr_region
local ecr_account_id
ecr_account_id="$(aws configure get "$ecr_account_profile.role_arn" | awk -F: '{ print $5 }')"
if [[ -z "$ecr_account_id" ]] ; then
return 1
fi
ecr_region=$(aws configure get "$ecr_account_profile.region")
if [[ -z "$ecr_region" ]] ; then
return 1
fi
aws ecr get-login-password \
--profile="$ecr_account_profile" \
--region="$ecr_region" \
| docker login \
--username=AWS \
--password-stdin \
"${ecr_account_id}.dkr.ecr.${ecr_region}.amazonaws.com"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment