forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 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
- Loading branch information
Showing
3 changed files
with
662 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
packages/@aws-cdk/aws-cloudfront/lib/private/invalidation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { IResource, Resource, Names, Lazy } from '@aws-cdk/core'; | ||
import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId } from '@aws-cdk/custom-resources'; | ||
import { Distribution } from '../distribution'; | ||
import { CloudFrontWebDistribution } from '../web-distribution'; | ||
|
||
/** | ||
* Represents a Cloudfront Invalidation | ||
*/ | ||
export interface IInvalidation extends IResource { | ||
/** | ||
* Id of the CloudFront Distribution to associate | ||
* @attribute | ||
*/ | ||
readonly distributionId:string; | ||
|
||
/** | ||
* A list of the paths in the distribution to invalidate | ||
* @attribute | ||
*/ | ||
readonly invalidationPaths:string[]; | ||
} | ||
|
||
/** | ||
* Properties for creating an Invalidation | ||
*/ | ||
export interface InvalidationProps { | ||
/** | ||
* Id of the CloudFront Distribution to associate | ||
*/ | ||
readonly distributionId: string; | ||
|
||
/** | ||
* A list of the paths in the distribution to invalidate | ||
* @default - invalidate all paths: ['/*'] | ||
*/ | ||
readonly invalidationPaths?: string[]; | ||
|
||
/** | ||
* A name to identify the invalidation. | ||
* @default - generated from the `id` | ||
*/ | ||
readonly invalidationName?: string; | ||
} | ||
|
||
/** | ||
* A CloudFront Invalidation configuration | ||
* | ||
* @resource Aws::CloudFormation::CustomResource | ||
*/ | ||
export class Invalidation extends Resource implements IInvalidation { | ||
|
||
private resource:AwsCustomResource; | ||
|
||
static fromDistribution(distribution: Distribution, invalidationPaths?: string[]): Invalidation { | ||
const invalidation = new Invalidation(distribution.distributionId, invalidationPaths); | ||
invalidation.resource.node.addDependency(distribution); | ||
return invalidation; | ||
} | ||
|
||
static fromCloudFrontWebDistribution(webDistribution: CloudFrontWebDistribution, invalidationPaths?: string[]): Invalidation { | ||
const invalidation = new Invalidation(webDistribution.distributionId, invalidationPaths); | ||
invalidation.resource.node.addDependency(webDistribution); | ||
return invalidation; | ||
} | ||
|
||
constructor( | ||
public readonly distributionId: string, | ||
public readonly invalidationPaths: string[] = ['/*'] | ||
) { | ||
super(scope, id, { | ||
physicalName: props.invalidationName || Lazy.string({ produce: () => this.node.uniqueId }), | ||
}); | ||
this.resource = new AwsCustomResource(this, `InvalidationResource${distributionId}`, { | ||
policy: AwsCustomResourcePolicy.fromSdkCalls({ | ||
resources: AwsCustomResourcePolicy.ANY_RESOURCE, | ||
}), | ||
installLatestAwsSdk: true, | ||
resourceType: 'Custom::CloudFrontInvalidation', | ||
onUpdate: { | ||
service: 'CloudFront', | ||
action: 'createInvalidation', | ||
physicalResourceId: PhysicalResourceId.fromResponse('Invalidation.Id'), | ||
parameters: { | ||
DistributionId: distributionId, | ||
InvalidationBatch: { | ||
CallerReference: this.generateName(), | ||
Paths: { | ||
Quantity: invalidationPaths.length, | ||
Items: invalidationPaths, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
private generateName(): string { | ||
const name = Names.uniqueId(this); | ||
if (name.length > 20) { | ||
return name.substring(0, 20); | ||
} | ||
return name; | ||
} | ||
} |
Oops, something went wrong.