Skip to content

Commit

Permalink
feat(cloudfront): distribution ARN property (#32531)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
nmussy authored Dec 16, 2024
1 parent d1bb1ca commit b7e6141
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
14 changes: 14 additions & 0 deletions packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)] });
}
Expand Down Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-cloudfront/lib/web-distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)] });
}
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 15 additions & 2 deletions packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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`);

});
});
Expand Down

0 comments on commit b7e6141

Please sign in to comment.