-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
212 lines (166 loc) · 5.6 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
'use strict';
/**
* This Serverless plugin replaces 'latest' pseudo version tag to actual latest version
*/
const traverse = require('traverse');
const util = require('util');
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.provider = serverless.getProvider("aws");
this.options = options;
this.resolvedLayers = new Set();
this.hooks = {
'after:aws:package:finalize:mergeCustomProviderResources': this.updateCFNLayerVersion.bind(this),
'before:deploy:function:deploy': this.updateSLSLayerVersion.bind(this),
};
}
updateSLSLayerVersion() {
// Find All Lambda Layer associations from compiled serverless configuration
return this.update(this.listSLSLayerAssociations());
}
updateCFNLayerVersion() {
// Find All Lambda Layer associations from compiled CFN template
return this.update(this.listCFNLayerAssociations());
}
async update(layerAssociations) {
// Collect target Layer ARNs
const collectedLayerARNs = this.collectLayerARNs(layerAssociations);
// Resolve actual Layer ARNs
const resolvedLayerARNs = await this.fetchLatestVersions(collectedLayerARNs);
// Recursively replace layer ARNs
this.replaceLayerVersions(layerAssociations, resolvedLayerARNs);
}
listCFNLayerAssociations() {
// Lookup compiled CFN template to support individual deployments
const compiledTemplate = this.serverless.service.provider.compiledCloudFormationTemplate;
const resources = compiledTemplate.Resources;
return Object.keys(resources).reduce((collection, key) => {
const resource = resources[key];
if (resource.Type === 'AWS::Lambda::Function') {
const layers = resource.Properties && resource.Properties.Layers;
if (Array.isArray(layers) && layers.length > 0) {
collection.push({ name: key, layers });
}
}
return collection;
}, []);
}
listSLSLayerAssociations() {
const { functions } = this.serverless.service;
return Object.keys(functions).reduce((collection, name) => {
const fn = functions[name];
const layers = fn.layers;
if (Array.isArray(layers) && layers.length > 0) {
collection.push({ name, layers });
}
return collection;
}, []);
}
async lookupLatestLayerVersionArn(layerArn) {
const layer = this.extractLayerArn(layerArn);
if (!layer) {
return null;
}
const versions = [];
let marker;
do {
const result = await this.provider.request("Lambda", "listLayerVersions", {
LayerName: layer.layerName,
Marker: marker,
});
versions.push(...result.LayerVersions);
marker = result.NextMarker;
} while (marker);
const sortedVersions = versions.sort((a, b) => {
if (a.Version > b.Version) {
return -1;
} else if (a.Version < b.Version) {
return 1
} else {
return 0;
}
});
return sortedVersions.length > 0 ?
sortedVersions[0].LayerVersionArn :
null;
}
extractLayerArn(arn) {
const SEPARATOR = "__SEPARATOR__";
// arn:aws:lambda:REGION:ACCOUNT_ID:layer:LAYER_NAME:LAYER_VERSION
const tokens = arn.replace(/([^:]):([^:])/g, (match, prev, next) => `${prev}${SEPARATOR}${next}`).split(SEPARATOR);
if (tokens.length !== 8) {
return null;
}
let region = tokens[3];
if (/AWS::Region/i.test(region)) {
region = this.serverless.service.provider.region;
}
const accountId = tokens[4];
const layerName = tokens[6];
return {
region,
layerName: /AWS::AccountId/i.test(accountId) ?
layerName :
`arn:aws:lambda:${region}:${accountId}:layer:${layerName}`,
};
}
collectLayerARNs(layerAssociations) {
const set = new Set();
for (const layerAssociation of layerAssociations) {
traverse(layerAssociation.layers).forEach(function (node) {
const matched = this.isLeaf
&& typeof node === "string"
&& /^arn:/i.test(node)
&& /latest$/i.test(node);
if (matched) {
set.add(node);
}
});
}
return set;
}
async fetchLatestVersions(layerARNs) {
const dict = new Map();
for (const layerARN of layerARNs) {
const version = await this.lookupLatestLayerVersionArn(layerARN);
dict.set(layerARN, version);
}
return dict;
}
replaceLayerVersions(layerAssociations, arnVersionMap) {
const self = this;
for (const layerAssociation of layerAssociations) {
traverse(layerAssociation.layers).forEach(function (node) {
const matched = this.isLeaf
&& typeof node === "string"
&& /^arn:/i.test(node)
&& /latest$/i.test(node);
if (matched) {
const resolvedLayerArn = arnVersionMap.get(node);
if (resolvedLayerArn) {
this.update(resolvedLayerArn);
// Avoid logging the same resolved layer multiple times
if (self.resolvedLayers.has(resolvedLayerArn)) {
return;
}
self.resolvedLayers.add(resolvedLayerArn);
self.log("Resolved %s to %s", node, resolvedLayerArn);
} else {
self.log("Detected unknown Layer ARN %s. Please create a new issue to github.com/mooyoul/serverless-latest-layer-version", node);
}
}
});
}
}
log(...args) {
const TAG = '[serverless-latest-layer-version]';
if (typeof args[0] === 'string') {
args[0] = `${TAG} ${args[0]}`;
} else {
args.unshift(TAG);
}
this.serverless.cli.log(util.format(...args));
}
}
module.exports = ServerlessPlugin;