Skip to content

Commit

Permalink
chore(ecr): expose registryUri (#32176)
Browse files Browse the repository at this point in the history
### 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",
		"<aws_account_id>.dkr.ecr.<region>.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*
  • Loading branch information
phuhung273 authored Dec 16, 2024
1 parent 4b696bc commit 93ff387
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/aws-cdk-lib/aws-ecr/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*
Expand Down Expand Up @@ -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`.
*
Expand Down
24 changes: 24 additions & 0 deletions packages/aws-cdk-lib/aws-ecr/test/repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 93ff387

Please sign in to comment.