Create AWS ECR repository if it doesn't exist
How can I create an AWS ECR repository if it doesn't already exist?
Solution 1:
One liner to create a repo if it doesn't exist (or the describe command fails for any other reason):
aws ecr describe-repositories --repository-names ${REPO_NAME} || aws ecr create-repository --repository-name ${REPO_NAME}
Solution 2:
AWS makes the repository only if it doesn't exist.
You can simply ignore the error & failure with|| true
in case if same repository exists:
aws ecr create-repository --repository-name <repo_name> || true
Solution 3:
Almost all the answers so far are calling the describe-repositories
and in case of error they assume that repo didn't exists.
This is wrong because there also other kind of errors that can appear (no internet connection, no permission (AccessDeniedException), wrong repo name, ...).
That means if describe-repositories
call ends up with error, then we need to check if the error was RepositoryNotFoundException
. Only in that case we should call the create-repository
.
This is how bash code for this could look like:
output=$(aws ecr describe-repositories --repository-names ${REPO_NAME} 2>&1)
if [ $? -ne 0 ]; then
if echo ${output} | grep -q RepositoryNotFoundException; then
aws ecr create-repository --repository-name ${REPO_NAME}
else
>&2 echo ${output}
fi
fi
Line by line explanation:
output=$(aws ecr describe-repositories --repository-names ${REPO_NAME} 2>&1)
-
This calls the describe-repositories
and stores the output to variable named output
.
if [ $? -ne 0 ]; then
- this line checks if last command (aws ecs describe-repositories ...
) was not successful. If exit code ($?
) was not 0 (-ne 0
) then we need to check what the error was. In case if was successful then there is nothing to do (successful means that repo exists already).
if echo ${output} | grep -q RepositoryNotFoundException; then
- in this line we're checking if error came because repo was not existent. If yes, then we need to create the repo:
aws ecr create-repository --repository-name ${REPO_NAME}
- creating the repo, we know that it didn't exists.
else
- the else case means that describe-repositories
throws error for other reason then not existent repo.
>&2 echo ${output}
- In that case we should not try to create repo but just output error on stderr (>&2
)
Solution 4:
You can do this, but you need to check if the repo exists first. I hacked this bash script together and it does what I need:
#!/bin/bash
aws ecr describe-repositories --repository-names $1 2>&1 > /dev/null
status=$?
if [[ ! "${status}" -eq 0 ]]; then
aws ecr create-repository --repository-name $1
fi
The argument would be some repo name. For this to work in CodeBuild, the job will need an IAM role that permits it to create an ECR repo. If you need to get AWS CLI credentials into your code build job, have a look at this AWS Blog post:
https://aws.amazon.com/blogs/devops/how-to-create-an-ami-builder-with-aws-codebuild-and-hashicorp-packer/
We're doing exactly what is described in the "Create a Build Specification" to use JQ to extract AWS credentials.