Skip to content

Commit d0f6c20

Browse files
committed
# This is a combination of 7 commits.
# This is the 1st commit message: Add Identity Pool construct # This is the commit message #2: Bug fixes # This is the commit message #3: Bug fixes # This is the commit message #4: Formatting # This is the commit message #5: Add construct methods # This is the commit message #6: Remove flat # This is the commit message #7: Fix issues
1 parent 31ffe1f commit d0f6c20

File tree

3 files changed

+662
-0
lines changed

3 files changed

+662
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { IResource, Resource, Names, Lazy } from '@aws-cdk/core';
2+
import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId } from '@aws-cdk/custom-resources';
3+
import { Distribution } from '../distribution';
4+
import { CloudFrontWebDistribution } from '../web-distribution';
5+
6+
/**
7+
* Represents a Cloudfront Invalidation
8+
*/
9+
export interface IInvalidation extends IResource {
10+
/**
11+
* Id of the CloudFront Distribution to associate
12+
* @attribute
13+
*/
14+
readonly distributionId:string;
15+
16+
/**
17+
* A list of the paths in the distribution to invalidate
18+
* @attribute
19+
*/
20+
readonly invalidationPaths:string[];
21+
}
22+
23+
/**
24+
* Properties for creating an Invalidation
25+
*/
26+
export interface InvalidationProps {
27+
/**
28+
* Id of the CloudFront Distribution to associate
29+
*/
30+
readonly distributionId: string;
31+
32+
/**
33+
* A list of the paths in the distribution to invalidate
34+
* @default - invalidate all paths: ['/*']
35+
*/
36+
readonly invalidationPaths?: string[];
37+
38+
/**
39+
* A name to identify the invalidation.
40+
* @default - generated from the `id`
41+
*/
42+
readonly invalidationName?: string;
43+
}
44+
45+
/**
46+
* A CloudFront Invalidation configuration
47+
*
48+
* @resource Aws::CloudFormation::CustomResource
49+
*/
50+
export class Invalidation extends Resource implements IInvalidation {
51+
52+
private resource:AwsCustomResource;
53+
54+
static fromDistribution(distribution: Distribution, invalidationPaths?: string[]): Invalidation {
55+
const invalidation = new Invalidation(distribution.distributionId, invalidationPaths);
56+
invalidation.resource.node.addDependency(distribution);
57+
return invalidation;
58+
}
59+
60+
static fromCloudFrontWebDistribution(webDistribution: CloudFrontWebDistribution, invalidationPaths?: string[]): Invalidation {
61+
const invalidation = new Invalidation(webDistribution.distributionId, invalidationPaths);
62+
invalidation.resource.node.addDependency(webDistribution);
63+
return invalidation;
64+
}
65+
66+
constructor(
67+
public readonly distributionId: string,
68+
public readonly invalidationPaths: string[] = ['/*']
69+
) {
70+
super(scope, id, {
71+
physicalName: props.invalidationName || Lazy.string({ produce: () => this.node.uniqueId }),
72+
});
73+
this.resource = new AwsCustomResource(this, `InvalidationResource${distributionId}`, {
74+
policy: AwsCustomResourcePolicy.fromSdkCalls({
75+
resources: AwsCustomResourcePolicy.ANY_RESOURCE,
76+
}),
77+
installLatestAwsSdk: true,
78+
resourceType: 'Custom::CloudFrontInvalidation',
79+
onUpdate: {
80+
service: 'CloudFront',
81+
action: 'createInvalidation',
82+
physicalResourceId: PhysicalResourceId.fromResponse('Invalidation.Id'),
83+
parameters: {
84+
DistributionId: distributionId,
85+
InvalidationBatch: {
86+
CallerReference: this.generateName(),
87+
Paths: {
88+
Quantity: invalidationPaths.length,
89+
Items: invalidationPaths,
90+
},
91+
},
92+
},
93+
},
94+
});
95+
}
96+
97+
private generateName(): string {
98+
const name = Names.uniqueId(this);
99+
if (name.length > 20) {
100+
return name.substring(0, 20);
101+
}
102+
return name;
103+
}
104+
}

0 commit comments

Comments
 (0)