Create AWS ECR repository if it doesn't exist

Amazon EcsDocker RegistryAws Ecr

Amazon Ecs Problem Overview


How can I create an AWS ECR repository if it doesn't already exist?

Amazon Ecs Solutions


Solution 1 - Amazon Ecs

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 - Amazon Ecs

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 - Amazon Ecs

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 - Amazon Ecs

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.

Solution 5 - Amazon Ecs

If you want this to be automated in Jenkins scripted pipeline, just use this code-snippet:

def ensureRegistry(accountId, region, repoName) {
    Logger log = new Logger(this)
    def accId = shell.output("aws --region ${region} ecr describe-repositories --repository-names \"${repoName}\" | jq .repositories[].registryId | tr -d '\"'")
    if (accId == accountId) {
        log.info("Docker repository ${repoName} exists for account ${accId}")
    } else {
        log.info("Docker repository ${repoName} doesn't exist for account ${accId}")
        shell.status("aws --region ${region} ecr create-repository --repository-name \"${repoName}\"")
        log.info("Docker repository ${repoName} was just created for account ${accId}")
    }
}

shell.groovy is:

def output(cmd) {
    sh(script: cmd, returnStdout: true)
}

def status(cmd) {
    sh(script: cmd, returnStatus: true)
}

Solution 6 - Amazon Ecs

In addition to conditionally creating the repo, if you also want to extract the repo URI, consider this multiline bash command:

REPO_URI=$(aws ecr describe-repositories --repository-names "${REPO_NAME}" --query "repositories[0].repositoryUri" --output text 2>/dev/null || \
           aws ecr create-repository --repository-name "${REPO_NAME}"  --query "repository.repositoryUri" --output text)

The repo URI can be useful for the tag and push operations.


Partial credit: answer by JS

Solution 7 - Amazon Ecs

export ECR_REPO=`aws ecr describe-repositories --repository-names $REPO_NAME 2>/dev/null | jq .repositories[0].repositoryUri | tr -d \\\" && aws ecr create-repository --repository-name $REPO_NAME --region us-east-1 2>/dev/null | jq .repository.repositoryUri | tr -d \\\"`

This works in a buildspec.yml file for always grabbing the repo name and storing it in the ECR_REPO var. It will create the repo or fail silently if it already exists. It will grab the repo name if it does exist or fail silently if it does not.

Solution 8 - Amazon Ecs

To check the whether ECR repository exist or not, you can use double. First check the describe the repositories if not exists then create repository always use tags this helps in auditing.

- aws ecr describe-repositories --repository-names ${ECRImage} || aws ecr create-repository --repository-name ${ECRImage} --tags Key=Domain,Value=$Domain Key=AppEnv,Value=$AppEnv Key=ApplicationCI,Value=$ApplicationCI Key=Owner,Value=$Owner Key=Requester,Value=$Requester Key=CostCenter,Value=$CostCenter

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYsakView Question on Stackoverflow
Solution 1 - Amazon EcsJames SoubryView Answer on Stackoverflow
Solution 2 - Amazon EcsAkifView Answer on Stackoverflow
Solution 3 - Amazon EcsWalery StrauchView Answer on Stackoverflow
Solution 4 - Amazon EcsRyan J. McDonoughView Answer on Stackoverflow
Solution 5 - Amazon EcsAleks SidorenkoView Answer on Stackoverflow
Solution 6 - Amazon EcsAsclepiusView Answer on Stackoverflow
Solution 7 - Amazon EcscsimaView Answer on Stackoverflow
Solution 8 - Amazon EcsManohar PView Answer on Stackoverflow