- Docker on Amazon Web Services
- Justin Menga
- 412字
- 2025-02-17 05:10:19
Creating ECR repositories using AWS CloudFormation
AWS CloudFormation supports the creation of ECR repositories via the AWS::ECR::Repository resource type, and at the time of writing, this allows you to manage ECR resource policies and lifecycle policies, which we will cover later in this chapter.
As a general rule of thumb, given the critical nature of ECR repositories as a distribution mechanism for your Docker images, I typically recommend the various ECR repositories for your account and region are defined in a single, shared CloudFormation stack dedicated solely to the creation and management of ECR repositories.
In keeping with this recommendation and for future chapters, let's create a repository called todobackend-aws that you can use to store the various infrastructure configurations you will create and manage throughout this book. I will leave you to create the corresponding repository on GitHub, after which you can configure your GitHub repository as a remote:
> mkdir todobackend-aws
> touch todobackend-aws/ecr.yml
> cd todobackend-aws
> git init
Initialized empty Git repository in /Users/jmenga/Source/docker-in-aws/todobackend-aws/.git/
> git remote add origin https://github.com/jmenga/todobackend-aws.git
> tree .
.
└── ecr.yml
You can now configure a CloudFormation template file called ecr.yml that defines a single ECR repository for the todobackend Docker image:
AWSTemplateFormatVersion: "2010-09-09"
Description: ECR Repositories
Resources:
TodobackendRepository:
Type: AWS::ECR::Repository
Properties:
RepositoryName: docker-in-aws/todobackend
As you can see in the previous example, defining an ECR repository is very simple using CloudFormation and only requires the RepositoryName property to be defined, which, as you might expect, defines the name of the repository.
Assuming you have deleted the current todobackend ECR repository, as demonstrated earlier, you can now use the aws cloudformation deploy command to create the todobackend repository using CloudFormation:
> aws cloudformation deploy --template-file ecr.yml --stack-name ecr-repositories
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - ecr-repositories
Once the stack deploys successfully, you can view the stack in the CloudFormation console, as shown in the following screenshot:

If you now navigate back to the ECS console and select Resources from the left menu, you should see a single ECR repository called docker-in-aws/todobackend, as defined in your CloudFormation stack. If you click on the repository, you will be taken to the repository detail page, which provides you with the repository URI, a list of images published in the repository, ECR permissions, and lifecycle policy settings.