Skip to content

Commit

Permalink
Merge pull request #19 from AllegraChen/create-multiple-invalidations
Browse files Browse the repository at this point in the history
feat: Add multiple invalidations based on stages
  • Loading branch information
aghadiry authored Jan 18, 2021
2 parents c9dadcb + 7282bb1 commit f573ba8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ If the CDN is created as part of same serverless.yml then you can specify the `d
```yaml
custom:
cloudfrontInvalidate:
distributionId: "CLOUDFRONT_DIST_ID" #conditional, distributionId or distributionIdKey is required.
distributionIdKey: "CDNDistributionId" #conditional, distributionId or distributionIdKey is required.
items: # one or more paths required
- "/index.html"
- distributionId: "CLOUDFRONT_DIST_ID" #conditional, distributionId or distributionIdKey is required.
distributionIdKey: "CDNDistributionId" #conditional, distributionId or distributionIdKey is required.
items: # one or more paths required
- "/index.html"
stage: "dev" # conditional, the stage that this cloudfront invalidation should be created
# this should match the provider's stage you declared, e.g. "dev" but not "prod" in this case
# an invalidation for this distribution will be created when executing `sls deploy --stage dev`
- distributionId: "CLOUDFRONT_DIST_ID" #conditional, distributionId or distributionIdKey is required.
distributionIdKey: "CDNDistributionId" #conditional, distributionId or distributionIdKey is required.
items: # one or more paths required
- "/index.html"
# `stage` is omitted, an invalidation will be created for this distribution at all stages
resources:
Resources:
CDN:
Expand Down
81 changes: 45 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ class CloudfrontInvalidate {
}

this.aws.sdk.config.update({
httpOptions: { agent: new https.Agent({ ca: fs.readFileSync(caCert)}) }
httpOptions: { agent: new https.Agent({ ca: fs.readFileSync(caCert) }) }
});

cli.consoleLog(`CloudfrontInvalidate: ${chalk.yellow('ca cert handling enabled')}`);
}

createInvalidation(distributionId, reference) {
createInvalidation(distributionId, reference, cloudfrontInvalidate) {
const cli = this.serverless.cli;
const cloudfrontInvalidateItems = this.serverless.service.custom.cloudfrontInvalidate.items;
const cloudfrontInvalidateItems = cloudfrontInvalidate.items;

const params = {
DistributionId: distributionId, /* required */
InvalidationBatch: { /* required */
CallerReference: reference, /* required */
Paths: { /* required */
Quantity: cloudfrontInvalidateItems.length, /* required */
Items: cloudfrontInvalidateItems
Quantity: cloudfrontInvalidateItems.length, /* required */
Items: cloudfrontInvalidateItems
}
}
};
Expand All @@ -91,40 +91,49 @@ class CloudfrontInvalidate {

invalidate() {
const cli = this.serverless.cli;
let cloudfrontInvalidate = this.serverless.service.custom.cloudfrontInvalidate;
let reference = randomstring.generate(16);
let distributionId = cloudfrontInvalidate.distributionId;
if (distributionId) {
cli.consoleLog(`DistributionId: ${chalk.yellow(distributionId)}`);
return this.createInvalidation(distributionId, reference);
}

if (!cloudfrontInvalidate.distributionIdKey) {
cli.consoleLog('distributionId or distributionIdKey is required');
return;
}

cli.consoleLog(`DistributionIdKey: ${chalk.yellow(cloudfrontInvalidate.distributionIdKey)}`);

this.serverless.service.custom.cloudfrontInvalidate.forEach(element => {
let cloudfrontInvalidate = element;
let reference = randomstring.generate(16);
let distributionId = cloudfrontInvalidate.distributionId;
let stage = cloudfrontInvalidate.stage;

if (stage !== undefined && stage != `${this.serverless.service.provider.stage}`) {
return;
}

// get the id from the output of stack.
const stackName = this.serverless.getProvider('aws').naming.getStackName()
if (distributionId) {
cli.consoleLog(`DistributionId: ${chalk.yellow(distributionId)}`);
return this.createInvalidation(distributionId, reference, cloudfrontInvalidate);
}

return this.aws.request('CloudFormation', 'describeStacks',{ StackName: stackName })
.then(result => {
if (result) {
const outputs = result.Stacks[0].Outputs;
outputs.forEach(output => {
if (output.OutputKey === cloudfrontInvalidate.distributionIdKey) {
distributionId = output.OutputValue;
}
});
}
})
.then(() => this.createInvalidation(distributionId, reference))
.catch(error => {
cli.consoleLog('Failed to get DistributionId from stack output. Please check your serverless template.');
if (!cloudfrontInvalidate.distributionIdKey) {
cli.consoleLog('distributionId or distributionIdKey is required');
return;
});
}

cli.consoleLog(`DistributionIdKey: ${chalk.yellow(cloudfrontInvalidate.distributionIdKey)}`);

// get the id from the output of stack.
const stackName = this.serverless.getProvider('aws').naming.getStackName()

return this.aws.request('CloudFormation', 'describeStacks', { StackName: stackName })
.then(result => {
if (result) {
const outputs = result.Stacks[0].Outputs;
outputs.forEach(output => {
if (output.OutputKey === cloudfrontInvalidate.distributionIdKey) {
distributionId = output.OutputValue;
}
});
}
})
.then(() => this.createInvalidation(distributionId, reference, cloudfrontInvalidate))
.catch(error => {
cli.consoleLog('Failed to get DistributionId from stack output. Please check your serverless template.');
return;
});
});
}
}

Expand Down

0 comments on commit f573ba8

Please sign in to comment.