Skip to content

Commit b7e6141

Browse files
authored
feat(cloudfront): distribution ARN property (#32531)
### Issue # (if applicable) Closes #32530 ### Reason for this change Give the user access to a distribution's ARN via the existing `distributionId` value. ### Description of changes Implemented by proxying the existing `formatDistributionArn` function via a getter method. ### Description of how you validated changes Unit tests were added for both created and imported distributions. Additionally, missing coverage for `Distribution.fromDistributionAttributes` was added, by copying the existing unit test for `CloudFrontWebDistribution.fromDistributionAttributes`. ### 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*
1 parent d1bb1ca commit b7e6141

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts

+14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ export interface IDistribution extends IResource {
4444
*/
4545
readonly distributionId: string;
4646

47+
/**
48+
* The distribution ARN for this distribution.
49+
*
50+
* @attribute
51+
*/
52+
readonly distributionArn: string;
53+
4754
/**
4855
* Adds an IAM policy statement associated with this distribution to an IAM
4956
* principal's policy.
@@ -291,6 +298,9 @@ export class Distribution extends Resource implements IDistribution {
291298
this.distributionId = attrs.distributionId;
292299
}
293300

301+
public get distributionArn(): string {
302+
return formatDistributionArn(this);
303+
}
294304
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
295305
return iam.Grant.addToPrincipal({ grantee, actions, resourceArns: [formatDistributionArn(this)] });
296306
}
@@ -389,6 +399,10 @@ export class Distribution extends Resource implements IDistribution {
389399
}
390400
}
391401

402+
public get distributionArn(): string {
403+
return formatDistributionArn(this);
404+
}
405+
392406
/**
393407
* Return the given named metric for this Distribution
394408
*/

packages/aws-cdk-lib/aws-cloudfront/lib/web-distribution.ts

+7
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ export class CloudFrontWebDistribution extends cdk.Resource implements IDistribu
761761
this.distributionId = attrs.distributionId;
762762
}
763763

764+
public get distributionArn(): string {
765+
return formatDistributionArn(this);
766+
}
764767
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
765768
return iam.Grant.addToPrincipal({ grantee, actions, resourceArns: [formatDistributionArn(this)] });
766769
}
@@ -992,6 +995,10 @@ export class CloudFrontWebDistribution extends cdk.Resource implements IDistribu
992995
this.distributionId = distribution.ref;
993996
}
994997

998+
public get distributionArn(): string {
999+
return formatDistributionArn(this);
1000+
}
1001+
9951002
/**
9961003
* Adds an IAM policy statement associated with this distribution to an IAM
9971004
* principal's policy.

packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as iam from '../../aws-iam';
66
import * as kinesis from '../../aws-kinesis';
77
import * as lambda from '../../aws-lambda';
88
import * as s3 from '../../aws-s3';
9-
import { App, Duration, Stack } from '../../core';
9+
import { App, Aws, Duration, Stack } from '../../core';
1010
import {
1111
CfnDistribution,
1212
Distribution,
@@ -36,7 +36,7 @@ beforeEach(() => {
3636

3737
test('minimal example renders correctly', () => {
3838
const origin = defaultOrigin();
39-
new Distribution(stack, 'MyDist', { defaultBehavior: { origin } });
39+
const dist = new Distribution(stack, 'MyDist', { defaultBehavior: { origin } });
4040

4141
Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Distribution', {
4242
DistributionConfig: {
@@ -58,6 +58,19 @@ test('minimal example renders correctly', () => {
5858
}],
5959
},
6060
});
61+
62+
expect(dist.distributionArn).toEqual(`arn:${Aws.PARTITION}:cloudfront::1234:distribution/${dist.distributionId}`);
63+
});
64+
65+
test('existing distributions can be imported', () => {
66+
const dist = Distribution.fromDistributionAttributes(stack, 'ImportedDist', {
67+
domainName: 'd111111abcdef8.cloudfront.net',
68+
distributionId: '012345ABCDEF',
69+
});
70+
71+
expect(dist.distributionDomainName).toEqual('d111111abcdef8.cloudfront.net');
72+
expect(dist.distributionId).toEqual('012345ABCDEF');
73+
expect(dist.distributionArn).toEqual(`arn:${Aws.PARTITION}:cloudfront::1234:distribution/012345ABCDEF`);
6174
});
6275

6376
test('exhaustive example of props renders correctly and SSL method sni-only', () => {

packages/aws-cdk-lib/aws-cloudfront/test/web-distribution.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('web distribution', () => {
133133
const stack = new cdk.Stack();
134134
const sourceBucket = new s3.Bucket(stack, 'Bucket');
135135

136-
new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
136+
const dist = new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
137137
originConfigs: [
138138
{
139139
s3OriginSource: {
@@ -204,6 +204,7 @@ describe('web distribution', () => {
204204
},
205205
});
206206

207+
expect(dist.distributionArn).toEqual(`arn:${cdk.Aws.PARTITION}:cloudfront::${cdk.Aws.ACCOUNT_ID}:distribution/${dist.distributionId}`);
207208
});
208209

209210
test('can disable distribution', () => {
@@ -1722,6 +1723,7 @@ added the ellipsis so a user would know there was more to r...`,
17221723

17231724
expect(dist.distributionDomainName).toEqual('d111111abcdef8.cloudfront.net');
17241725
expect(dist.distributionId).toEqual('012345ABCDEF');
1726+
expect(dist.distributionArn).toEqual(`arn:${cdk.Aws.PARTITION}:cloudfront::${cdk.Aws.ACCOUNT_ID}:distribution/012345ABCDEF`);
17251727

17261728
});
17271729
});

0 commit comments

Comments
 (0)