Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create AWS ECR repository if it doesn't exist

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

like image 378
Ysak Avatar asked Jun 25 '18 17:06

Ysak


People also ask

What is the need for a user to login to the AWS ECR repository if she wants to push a Docker image there?

To push a Docker image to an Amazon ECR repositoryAuthentication tokens must be obtained for each registry used, and the tokens are valid for 12 hours. For more information, see Private registry authentication. To authenticate Docker to an Amazon ECR registry, run the aws ecr get-login-password command.

How do you replicate ECR to another region?

To get started you simply enable replication, choose the destination accounts and regions you want ECR to copy images to. After this, every time you push an image to the private repository, ECR automatically replicates the image.


2 Answers

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} 
like image 142
James Soubry Avatar answered Sep 25 '22 21:09

James Soubry


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 
like image 34
Akif Avatar answered Sep 22 '22 21:09

Akif