forked from aghadiry/serverless-cloudfront-invalidate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (111 loc) · 3.86 KB
/
index.js
File metadata and controls
131 lines (111 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const randomstring = require('randomstring');
const chalk = require('chalk');
const fs = require('fs');
const https = require('https');
const proxy = require('proxy-agent');
class CloudfrontInvalidate {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.proxyURL =
process.env.proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy ||
process.env.HTTPS_PROXY ||
process.env.https_proxy;
this.provider = 'aws';
this.aws = this.serverless.getProvider('aws');
if (this.proxyURL) {
this.setProxy(this.proxyURL);
}
if (this.options.cacert) {
this.handleCaCert(this.options.cacert);
}
this.commands = {
cloudfrontInvalidate: {
usage: "Invalidate Cloudfront Cache",
lifecycleEvents: [
'invalidate'
]
}
};
this.hooks = {
'after:deploy:deploy': this.invalidate.bind(this),
'cloudfrontInvalidate:invalidate': this.invalidate.bind(this),
};
}
setProxy(proxyURL) {
this.aws.sdk.config.update({
httpOptions: { agent: proxy(proxyURL) },
});
}
handleCaCert(caCert) {
const cli = this.serverless.cli;
if (!fs.existsSync(caCert)) {
throw new Error("Supplied cacert option to a file that does not exist: " + caCert);
}
this.aws.sdk.config.update({
httpOptions: { agent: new https.Agent({ ca: fs.readFileSync(caCert)}) }
});
cli.consoleLog(`CloudfrontInvalidate: ${chalk.yellow('ca cert handling enabled')}`);
}
createInvalidation(distributionId, reference) {
const cli = this.serverless.cli;
const cloudfrontInvalidateItems = this.serverless.service.custom.cloudfrontInvalidate.items;
const params = {
DistributionId: distributionId, /* required */
InvalidationBatch: { /* required */
CallerReference: reference, /* required */
Paths: { /* required */
Quantity: cloudfrontInvalidateItems.length, /* required */
Items: cloudfrontInvalidateItems
}
}
};
return this.aws.request('CloudFront', 'createInvalidation', params).then(
() => {
cli.consoleLog(`CloudfrontInvalidate: ${chalk.yellow('Invalidation started')}`);
},
err => {
console.log(JSON.stringify(err));
cli.consoleLog(`CloudfrontInvalidate: ${chalk.yellow('Invalidation failed')}`);
throw err;
}
);
}
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)}`);
// 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))
.catch(error => {
cli.consoleLog('Failed to get DistributionId from stack output. Please check your serverless template.');
return;
});
}
}
module.exports = CloudfrontInvalidate;