Skip to content

Instantly share code, notes, and snippets.

@ssaavedra
Created May 10, 2017 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaavedra/b1270d672c1190171f014d09d4ac3018 to your computer and use it in GitHub Desktop.
Save ssaavedra/b1270d672c1190171f014d09d4ac3018 to your computer and use it in GitHub Desktop.
Create an AWS ECR repository and add a policy to it from an already existing repo.
#!/bin/bash
# Author: Santiago Saavedra
# License: CC0
# Usage: $0 --help
POLICY_REPOSITORY=${AWS_DEFAULT_ECR_POLICY_REPOSITORY:-policy-repo}
usage () {
cat <<-EOF | awk 'NR==1 && match($0, /^ +/){n=RLENGTH} {print substr($0, n+1)}'
Usage: $0 <aws-repository-name>
Creates a repository in your default account for AWS with the
given name.
ENVIRONMENT VARIABLES:
AWS_DEFAULT_ECR_POLICY_REPOSITORY
Sets a policy for the new repository according to a previously
existing repository.
(default: "policy-repo")
(current: ${AWS_DEFAULT_ECR_POLICY_REPOSITORY})
EOF
}
nag () {
echo "You are about to create a repository named: '$repo'."
echo "You have one second to press Ctrl+C."
sleep 1
}
get-policy () {
aws ecr get-repository-policy --repository-name ${POLICY_REPOSITORY} | jq -r .policyText
}
create-repo () {
aws ecr create-repository --repository-name "$1" || echo "Repository \"$1\" already exists"
}
set-policy () {
aws ecr set-repository-policy --repository-name "$1" --policy-text file://<(cat)
}
if [[ "$*" < 2 || "$1" == "--help" || "$1" == "-h" ]]; then
usage
exit 0
fi
create-repo "$1"
get-policy | set-policy "$1"
# Local Variables:
# tab-width: 4
# sh-indentation: 4
# indent-tabs-mode: nil
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment