Setting up an ECR repository

Setting up an ECR repository

Enhance your project Makefile in the project directory with the following content:

.PHONY: ecr
ecr:
	aws cloudformation deploy \
		--no-fail-on-empty-changeset \
		--template-file cloudformation/ecr.yml \
		--stack-name $(SERVICE)-ecr \
		--parameter-overrides RepositoryName=$(SERVICE) \
		--region $(AWS_REGION)

Create a new cloudformation template in cloudformation/ecr.yml and define your ECR repository in there:

---
AWSTemplateFormatVersion: 2010-09-09
Description: ECR repository for '...'

Parameters:
  RepositoryName:
    Type: String
    Description: 'Name of the ECR repository'

Resources:
  Repository:
    Type: 'AWS::ECR::Repository'
    Properties:
      RepositoryName: !Ref RepositoryName

Create the Cloudformation stack with the following command in the ci AWS account

AWS_PROFILE=myorg-ci make ecr

Pushing the docker image to the repository

Enhance the Makefile as follows:

.PHONY: docker-push
docker-push:
	$$(aws ecr get-login --no-include-email --region $(AWS_REGION))
	docker push $(DOCKER_IMAGE)

Now let’s manually build and push the Docker image

AWS_PROFILE=myorg-ci make docker-build docker-push

Log in to the AWS Console, switch to the CI account and check if the image is in your repository.