forked from benmarch/update-lambda-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
239 lines (197 loc) · 7.29 KB
/
index.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const fs = require('fs')
const AWS = require('aws-sdk')
const getLatestLambdaVersion = async (functionName, region) => {
const lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region
})
const versions = []
let nextMarker
do {
const versionData = await lambda.listVersionsByFunction({
FunctionName: functionName,
Marker: nextMarker,
}).promise()
nextMarker = versionData.NextMarker
versions.push(...versionData.Versions)
} while (nextMarker)
if (!versions.length) {
return ''
}
return versions.filter(version => version.Version !== '$LATEST').sort((a, b) => Number(b.Version) - Number(a.Version))[0]
}
const getCloudFrontDistributionConfig = async (distributionID, region) => {
const cloudfront = new AWS.CloudFront({
apiVersion: '2020-05-31',
region
})
return cloudfront.getDistributionConfig({
Id: distributionID
}).promise()
}
const changeCloudFrontDistributionLambdaARN = (distributionConfig, lambdaARN, triggerName) => {
try {
const lambdaFunction = distributionConfig.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items.find(item => item.EventType === triggerName)
if (lambdaFunction.LambdaFunctionARN !== lambdaARN) {
lambdaFunction.LambdaFunctionARN = lambdaARN
}
} catch (e) {}
return distributionConfig
}
const updateCloudFrontDistribution = async (distributionID, distributionConfig, region) => {
const cloudfront = new AWS.CloudFront({
apiVersion: '2020-05-31',
region
})
return cloudfront.updateDistribution({
Id: distributionID,
IfMatch: distributionConfig.ETag,
DistributionConfig: distributionConfig.DistributionConfig
}).promise()
}
/**
* Pushes a ZIP file containing Lambda code to S3
*
* @param {string} bucket The name of the S3 bucket that the ZIP file resides in
* @param {string} key The path to the ZIP file in the S3 bucket (must end in .zip)
* @param {string} filePath Absolute path to ZIP file (must end in .zip)
* @param {string} functionName The name of the function to update
* @param {boolean} autoIncrement If true, will append the next Lambda version number to the ZIP file name (functionName is required if true)
* @param {string} version A version to append to the ZIP file name (ignored if autoIncrement is true)
* @param {string} region The region the Lambda lives
* @param {boolean} dryRun If true, will not push to S3
*
* @return {Promise<void>}
*/
const pushNewCodeBundle = async ({bucket, key, filePath, functionName, autoIncrement, version, region, dryRun}) => {
const s3 = new AWS.S3({
apiVersion: '2006-03-01',
region
})
if (functionName && autoIncrement) {
version = `${Number((await getLatestLambdaVersion(functionName, region)).Version) + 1}`
}
if (version) {
key = key.replace(/\.zip$/, `-${version}.zip`)
}
const config = {
Bucket: bucket,
Key: key,
Body: fs.createReadStream(filePath)
}
console.log('Pushing to S3 with the following config:', config)
if (dryRun) {
return
}
await s3.upload(config).promise()
console.log('Successfully pushed to S3.')
}
/**
* Updates a Lambda function's code with a ZIP file in S3
*
* @param {string} bucket The name of the S3 bucket that the ZIP file resides in
* @param {string} key The path to the ZIP file in the S3 bucket (must end in ".zip")
* @param {string} functionName The name of the function to update
* @param {boolean} useNextVersion If true, will append the next Lambda version number to the key
* @param {string} version A version to append to the key (ignored if useNextVersion is true)
* @param {string} region The region the Lambda lives
* @param {boolean} dryRun If true, will not update the Lambda code
*
* @return {Promise<void>}
*/
const deployLambda = async ({bucket, key, functionName, useNextVersion, version, region, dryRun}) => {
const lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region
})
if (useNextVersion) {
version = `${Number((await getLatestLambdaVersion(functionName, region)).Version) + 1}`
}
if (version) {
key = key.replace(/\.zip$/, `-${version}.zip`)
}
const config = {
FunctionName: functionName,
S3Bucket: bucket,
S3Key: key
}
console.log('Updating Lambda code with the following config', config)
if (dryRun) {
return
}
await lambda.updateFunctionCode(config).promise()
console.log('Successfully deployed new code.')
}
/**
* Publishes a new version of a Lambda function
*
* @param {string} functionName The name of the function to update
* @param {string} region The region the Lambda lives
* @param {boolean} dryRun If true, will not publish a new Lambda version
*
* @return {Promise<void>}
*/
const publishLambda = async ({functionName, region, dryRun}) => {
const lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region
})
const config = {
FunctionName: functionName
}
console.log('Publishing new Lambda version with the following config:', config)
if (dryRun) {
return
}
await lambda.publishVersion(config).promise()
console.log('Successfully published new Lambda version.')
}
/**
* Sets the Lambda@Edge triggers for the specified Lambda functions on the specified CloudFront distribution
*
* @param {string} distributionId The CloudFront distribution ID
* @param {string} viewerRequest The name of the function to execute when "Viewer Request" is triggered
* @param {string} originRequest The name of the function to execute when "Origin Request" is triggered
* @param {string} originResponse The name of the function to execute when "Origin Response" is triggered
* @param {string} viewerResponse The name of the function to execute when "Viewer Response" is triggered
* @param {string} region The region the Lambda lives
* @param {boolean} dryRun If true, will not update the CloudFront triggers
*
* @return {Promise<void>}
*/
const activateLambdas = async ({distributionId, viewerRequest, originRequest, originResponse, viewerResponse, region, dryRun}) => {
console.log({distributionId, viewerRequest, originRequest, originResponse, viewerResponse, region, dryRun})
// first, get the CF distro
const distroConfig = await getCloudFrontDistributionConfig(distributionId, region)
// then, get the latest Lambda ARNs
const lambdaConfigs = {
'viewer-request': viewerRequest,
'origin-request': originRequest,
'origin-response': originResponse,
'viewer-response': viewerResponse
}
const lambdaARNs = {}
await Promise.all(Object.entries(lambdaConfigs)
.filter(([, functionName]) => !!functionName)
.map(async ([triggerName, functionName]) => {
lambdaARNs[triggerName] = (await getLatestLambdaVersion(functionName, 'us-east-1')).FunctionArn
}))
console.log('Activating the following ARNs:', lambdaARNs)
// then, set the arns in the config
const updatedConfig = Object.entries(lambdaARNs)
.filter(([, functionName]) => !!functionName)
.reduce((config, [triggerName, arn]) => changeCloudFrontDistributionLambdaARN(config, arn, triggerName), distroConfig)
// do not update if this is a dry run
if (dryRun) {
return
}
// finally, update the distro
await updateCloudFrontDistribution(distributionId, updatedConfig, region)
console.log('Successfully activated new Lambdas.')
}
module.exports = {
pushNewCodeBundle,
deployLambda,
publishLambda,
activateLambdas,
}