Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Elastic Container Repository URI from Cloud Formation?

I'm trying to create an Elastic Container Service (ECS) setup from Cloud Formation.

However I don't want the ECS repository to have the ugly autogenerated URI:

111111111.dkr.ecr.us-east-1.amazonaws.com/docker-repo.company.com

but instead I want it to have a nice and shiny

docker-repo.company.com

The repository itself does not allow setting the URI or even a CNAME. So I'm trying to setup a S3 bucket to redirect to the repo. However unless I'm missing something, Cloud Formation doesn't support this since using !Ref or !GetAtt there's nothing I can query in the AWS::ECR::Repository object that will give me the repository URI.

Am I missing something? Thanks!

like image 574
oblio Avatar asked Aug 24 '17 09:08

oblio


People also ask

What is an ECR repository?

Amazon Elastic Container Registry (Amazon ECR) provides API operations to create, monitor, and delete image repositories and set permissions that control who can access them. You can perform the same actions in the Repositories section of the Amazon ECR console.

What is elastic container repository?

Amazon Elastic Container Registry (ECR) is a fully-managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images. Amazon ECR is integrated with Amazon Elastic Container Service (ECS), simplifying your development to production workflow.

What is elastic container registry AWS?

Amazon Elastic Container Registry (Amazon ECR) is an AWS managed container image registry service that is secure, scalable, and reliable. Amazon ECR supports private repositories with resource-based permissions using AWS IAM.

What is ECR template?

Embedded Crystal (ECR) is a template language for embedding Crystal code into other text, that includes but is not limited to HTML. The template is read and transformed at compile time and then embedded into the binary.


2 Answers

This is kind of silly, but in the end it seems you cannot refer to the URI of an ECR because Cloud Formation doesn't support it.

There's no attribute for the URI, even though funnily enough their Ruby SDK does support it and even the third-party, cross-cloud Terraform supports it.

However, you can hack around this because the repository URI is stable, it doesn't contain any random part, so you can compose the URI from things you already have:

HostName: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${dockerrepocompanycom}"

or for the less cool, old-school version:

HostName: !Join [ ".", [ !Ref "AWS::AccountId", "dkr.ecr", !Ref "AWS::Region", !Join [ "/", [ "amazonaws.com", !Ref "dockerrepocompanycom" ] ] ] ]

Full working configuration for creating the S3 bucket:

   s3dockerrepo1redirect:
    Type: AWS::S3::Bucket
    Properties:
        BucketName: "docker-repo.company.com"
        WebsiteConfiguration:
            RedirectAllRequestsTo:
               HostName: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${dockerrepocompanycom}"
like image 104
oblio Avatar answered Oct 11 '22 15:10

oblio


HostName: !Join [ ".", [ !Ref "AWS::AccountId", "dkr.ecr", !Ref "AWS::Region", !Join [ "/", [ "amazonaws.com", !Ref "dockerrepocompanycom" ] ] ] ]

A more concise way is as follows:

HostName: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${dockerrepocompanycom}"

(adding as separate answer because I lack reputation points to comment on previous answer)

like image 38
sgeb Avatar answered Oct 11 '22 13:10

sgeb