From 93ff3874722e2f4d924891c08ff40ae45a66e53e Mon Sep 17 00:00:00 2001 From: Hung Tran <40334379+phuhung273@users.noreply.github.com> Date: Tue, 17 Dec 2024 06:34:03 +0700 Subject: [PATCH] chore(ecr): expose registryUri (#32176) ### Issue # (if applicable) Closes #31631 ### Reason for this change The Docker credentials helper (e.g. [docker-credential-ecr-login](https://github.com/awslabs/amazon-ecr-credential-helper)) expects a hostname in the configuration. ```json { "credHelpers": { "public.ecr.aws": "ecr-login", ".dkr.ecr..amazonaws.com": "ecr-login" } } ``` Docs: https://github.com/awslabs/amazon-ecr-credential-helper?tab=readme-ov-file#docker ### Description of changes - Expose registryUri ### Description of how you validated changes - Unit test ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../aws-cdk-lib/aws-ecr/lib/repository.ts | 20 ++++++++++++++++ .../aws-ecr/test/repository.test.ts | 24 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/aws-cdk-lib/aws-ecr/lib/repository.ts b/packages/aws-cdk-lib/aws-ecr/lib/repository.ts index f86b2d11b5b9f..9416905ef17bc 100644 --- a/packages/aws-cdk-lib/aws-ecr/lib/repository.ts +++ b/packages/aws-cdk-lib/aws-ecr/lib/repository.ts @@ -49,6 +49,15 @@ export interface IRepository extends IResource { */ readonly repositoryUri: string; + /** + * The URI of this repository's registry: + * + * ACCOUNT.dkr.ecr.REGION.amazonaws.com + * + * @attribute + */ + readonly registryUri: string; + /** * Returns the URI of the repository for a certain tag. Can be used in `docker push/pull`. * @@ -191,6 +200,17 @@ export abstract class RepositoryBase extends Resource implements IRepository { return this.repositoryUriForTag(); } + /** + * The URI of this repository's registry: + * + * ACCOUNT.dkr.ecr.REGION.amazonaws.com + * + */ + public get registryUri(): string { + const parts = this.stack.splitArn(this.repositoryArn, ArnFormat.SLASH_RESOURCE_NAME); + return `${parts.account}.dkr.ecr.${parts.region}.${this.stack.urlSuffix}`; + } + /** * Returns the URL of the repository. Can be used in `docker push/pull`. * diff --git a/packages/aws-cdk-lib/aws-ecr/test/repository.test.ts b/packages/aws-cdk-lib/aws-ecr/test/repository.test.ts index 0c703ad7763da..e5422fc17531a 100644 --- a/packages/aws-cdk-lib/aws-ecr/test/repository.test.ts +++ b/packages/aws-cdk-lib/aws-ecr/test/repository.test.ts @@ -308,6 +308,30 @@ describe('repository', () => { }); }); + test('calculate registry URI', () => { + // GIVEN + const stack = new cdk.Stack(); + const repo = new ecr.Repository(stack, 'Repo'); + + new cdk.CfnOutput(stack, 'RegistryUri', { + value: repo.registryUri, + }); + + // THEN + const arnSplit = { 'Fn::Split': [':', { 'Fn::GetAtt': ['Repo02AC86CF', 'Arn'] }] }; + Template.fromStack(stack).hasOutput('*', { + 'Value': { + 'Fn::Join': ['', [ + { 'Fn::Select': [4, arnSplit] }, + '.dkr.ecr.', + { 'Fn::Select': [3, arnSplit] }, + '.', + { Ref: 'AWS::URLSuffix' }, + ]], + }, + }); + }); + test('import with concrete arn', () => { // GIVEN const stack = new cdk.Stack();