diff --git a/packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts b/packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts index 35f081faafc6b..9bed65f41f48e 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts @@ -44,6 +44,13 @@ export interface IDistribution extends IResource { */ readonly distributionId: string; + /** + * The distribution ARN for this distribution. + * + * @attribute + */ + readonly distributionArn: string; + /** * Adds an IAM policy statement associated with this distribution to an IAM * principal's policy. @@ -291,6 +298,9 @@ export class Distribution extends Resource implements IDistribution { this.distributionId = attrs.distributionId; } + public get distributionArn(): string { + return formatDistributionArn(this); + } public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant { return iam.Grant.addToPrincipal({ grantee, actions, resourceArns: [formatDistributionArn(this)] }); } @@ -389,6 +399,10 @@ export class Distribution extends Resource implements IDistribution { } } + public get distributionArn(): string { + return formatDistributionArn(this); + } + /** * Return the given named metric for this Distribution */ diff --git a/packages/aws-cdk-lib/aws-cloudfront/lib/web-distribution.ts b/packages/aws-cdk-lib/aws-cloudfront/lib/web-distribution.ts index 530d6ac77149c..fccd2fa9e6020 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/lib/web-distribution.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/lib/web-distribution.ts @@ -761,6 +761,9 @@ export class CloudFrontWebDistribution extends cdk.Resource implements IDistribu this.distributionId = attrs.distributionId; } + public get distributionArn(): string { + return formatDistributionArn(this); + } public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant { return iam.Grant.addToPrincipal({ grantee, actions, resourceArns: [formatDistributionArn(this)] }); } @@ -992,6 +995,10 @@ export class CloudFrontWebDistribution extends cdk.Resource implements IDistribu this.distributionId = distribution.ref; } + public get distributionArn(): string { + return formatDistributionArn(this); + } + /** * Adds an IAM policy statement associated with this distribution to an IAM * principal's policy. diff --git a/packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts b/packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts index 470af4c0a0c09..927ba5861b368 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts @@ -6,7 +6,7 @@ import * as iam from '../../aws-iam'; import * as kinesis from '../../aws-kinesis'; import * as lambda from '../../aws-lambda'; import * as s3 from '../../aws-s3'; -import { App, Duration, Stack } from '../../core'; +import { App, Aws, Duration, Stack } from '../../core'; import { CfnDistribution, Distribution, @@ -36,7 +36,7 @@ beforeEach(() => { test('minimal example renders correctly', () => { const origin = defaultOrigin(); - new Distribution(stack, 'MyDist', { defaultBehavior: { origin } }); + const dist = new Distribution(stack, 'MyDist', { defaultBehavior: { origin } }); Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Distribution', { DistributionConfig: { @@ -58,6 +58,19 @@ test('minimal example renders correctly', () => { }], }, }); + + expect(dist.distributionArn).toEqual(`arn:${Aws.PARTITION}:cloudfront::1234:distribution/${dist.distributionId}`); +}); + +test('existing distributions can be imported', () => { + const dist = Distribution.fromDistributionAttributes(stack, 'ImportedDist', { + domainName: 'd111111abcdef8.cloudfront.net', + distributionId: '012345ABCDEF', + }); + + expect(dist.distributionDomainName).toEqual('d111111abcdef8.cloudfront.net'); + expect(dist.distributionId).toEqual('012345ABCDEF'); + expect(dist.distributionArn).toEqual(`arn:${Aws.PARTITION}:cloudfront::1234:distribution/012345ABCDEF`); }); test('exhaustive example of props renders correctly and SSL method sni-only', () => { diff --git a/packages/aws-cdk-lib/aws-cloudfront/test/web-distribution.test.ts b/packages/aws-cdk-lib/aws-cloudfront/test/web-distribution.test.ts index 7202b28052e84..5da37d06e8280 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/test/web-distribution.test.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/test/web-distribution.test.ts @@ -133,7 +133,7 @@ describe('web distribution', () => { const stack = new cdk.Stack(); const sourceBucket = new s3.Bucket(stack, 'Bucket'); - new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', { + const dist = new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', { originConfigs: [ { s3OriginSource: { @@ -204,6 +204,7 @@ describe('web distribution', () => { }, }); + expect(dist.distributionArn).toEqual(`arn:${cdk.Aws.PARTITION}:cloudfront::${cdk.Aws.ACCOUNT_ID}:distribution/${dist.distributionId}`); }); test('can disable distribution', () => { @@ -1722,6 +1723,7 @@ added the ellipsis so a user would know there was more to r...`, expect(dist.distributionDomainName).toEqual('d111111abcdef8.cloudfront.net'); expect(dist.distributionId).toEqual('012345ABCDEF'); + expect(dist.distributionArn).toEqual(`arn:${cdk.Aws.PARTITION}:cloudfront::${cdk.Aws.ACCOUNT_ID}:distribution/012345ABCDEF`); }); });