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();