Create AWS ECR repository if it doesn't exist

One liner to create a repo if it doesn't exist:

aws ecr describe-repositories --repository-names ${REPO_NAME} || aws ecr create-repository --repository-name ${REPO_NAME}

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

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.