Skip to content

Instantly share code, notes, and snippets.

@jkinkead
Created January 26, 2020 17:24
Show Gist options
  • Save jkinkead/7a4eb001fa786009778fd31a8cb87035 to your computer and use it in GitHub Desktop.
Save jkinkead/7a4eb001fa786009778fd31a8cb87035 to your computer and use it in GitHub Desktop.
ECR login helper for bash.
# Login to ECR, optionally specifying a profile namd and region.
ecr_login () {
local profile_name="$1"
local region="$2"
local args=()
if [ -n "$profile_name" ]; then
args=("${args[@]}" "--profile=$profile_name")
fi
if [ -n "$region" ]; then
args=("${args[@]}" "--region=$region")
fi
local auth_result=$(aws "${args[@]}" ecr get-authorization-token --output text)
if [ -z "$auth_result" ]; then
return
fi
local token=$(echo "$auth_result" | cut -f2 | base64 --decode | cut -d: -f2)
local url=$(echo "$auth_result" | cut -f4)
echo "$token" | docker login --password-stdin -u AWS "$url"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment