Created
May 10, 2017 12:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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