Skip to content

Instantly share code, notes, and snippets.

@praveenc
Created March 15, 2021 17:49
Show Gist options
  • Save praveenc/e965095082be2a0cf050c41bdcfff0ee to your computer and use it in GitHub Desktop.
Save praveenc/e965095082be2a0cf050c41bdcfff0ee to your computer and use it in GitHub Desktop.
AWS ECR Migration Script (authenticate, pull, re-tag and push)
#!/bin/bash
set -xe
# The Enviroment where this script will be ran needs to have jq, aws cli and docker installed
# Set parameters
region=$1
original_aws_profile=$2
new_aws_profile=$3
original_aws_account_number=$4
new_aws_account_number=$5
# (1) Create ECR repositories in 'new account'
for repo in `aws ecr --region=$region describe-repositories --profile $original_aws_profile | jq -r 'map(.[] | .repositoryName ) | join(" ")'`;
do echo `aws ecr --region $region create-repository --repository-name --profile $new_aws_profile $repo`;done
# (2) Authenticating to the 'original account' and 'new account' ECR registry
##################################################################################################################################################################
## If your AWS CLI version is v1.17.10 or later or the latest version of AWS CLI version 2, ##
## you should use `aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com` ##
## Please refer to https://docs.aws.amazon.com/cli/latest/reference/ecr/get-login-password.html ##
##################################################################################################################################################################
eval $(aws ecr get-login --region $region --no-include-email --profile $original_aws_profile | sed 's|https://||')
eval $(aws ecr get-login --region $region --no-include-email --profile $new_aws_profile | sed 's|https://||')
# (3) Pull the docker images from 'original account' to your local machine [2]
# (4) Pushing the docker images to 'new account' ECR [3]
for repo in `aws ecr --region=$region describe-repositories --profile $original_aws_profile | jq -r 'map(.[] | .repositoryName ) | join(" ")'`; do
for image in `aws ecr --region $region list-images --repository-name $repo --profile $original_aws_profile | jq -r 'map(.[] | .imageTag) | join(" ")'`;
do docker pull $original_aws_account_number.dkr.ecr.$region.amazonaws.com/$repo:$image;
docker tag $original_aws_account_number.dkr.ecr.$region.amazonaws.com/$repo:$image $new_aws_account_number.dkr.ecr.$region.amazonaws.com/$repo:$image;
docker push $new_aws_account_number.dkr.ecr.$region.amazonaws.com/$repo:$image; done; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment