/
index.js
258 lines (212 loc) · 8.37 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'use strict';
const BbPromise = require('bluebird');
const _ = require('lodash');
const crypto = require('crypto');
const path = require('path');
const fs = require('fs');
const validate = require('../lib/validate');
const filesize = require('filesize');
class AwsDeployFunction {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.packagePath =
this.options.package ||
this.serverless.service.package.path ||
path.join(this.serverless.config.servicePath || '.', '.serverless');
this.provider = this.serverless.getProvider('aws');
Object.assign(this, validate);
this.hooks = {
'deploy:function:initialize': () =>
BbPromise.bind(this)
.then(this.validate)
.then(this.checkIfFunctionExists),
'deploy:function:packageFunction': () =>
this.serverless.pluginManager.spawn('package:function'),
'deploy:function:deploy': () =>
BbPromise.bind(this)
.then(() => {
if (!this.options['update-config']) {
return this.deployFunction();
}
return BbPromise.resolve();
})
.then(this.updateFunctionConfiguration)
.then(() => this.serverless.pluginManager.spawn('aws:common:cleanupTempDir')),
};
}
checkIfFunctionExists() {
// check if the function exists in the service
this.options.functionObj = this.serverless.service.getFunction(this.options.function);
// check if function exists on AWS
const params = {
FunctionName: this.options.functionObj.name,
};
return this.provider
.request('Lambda', 'getFunction', params)
.then(result => {
this.serverless.service.provider.remoteFunctionData = result;
return result;
})
.catch(() => {
const errorMessage = [
`The function "${this.options.function}" you want to update is not yet deployed.`,
' Please run "serverless deploy" to deploy your service.',
' After that you can redeploy your services functions with the',
' "serverless deploy function" command.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
});
}
normalizeArnRole(role) {
if (typeof role === 'string') {
if (role.indexOf(':') === -1) {
const roleResource = this.serverless.service.resources.Resources[role];
if (roleResource.Type !== 'AWS::IAM::Role') {
throw new Error('Provided resource is not IAM Role.');
}
const roleProperties = roleResource.Properties;
const compiledFullRoleName = `${roleProperties.Path || '/'}${roleProperties.RoleName}`;
return this.provider
.getAccountInfo()
.then(
result => `arn:${result.partition}:iam::${result.accountId}:role${compiledFullRoleName}`
);
}
return BbPromise.resolve(role);
}
return this.provider
.request('IAM', 'getRole', {
RoleName: role['Fn::GetAtt'][0],
})
.then(data => data.Arn);
}
callUpdateFunctionConfiguration(params) {
return this.provider.request('Lambda', 'updateFunctionConfiguration', params).then(() => {
this.serverless.cli.log(`Successfully updated function: ${this.options.function}`);
});
}
updateFunctionConfiguration() {
const functionObj = this.options.functionObj;
const serviceObj = this.serverless.service.serviceObject;
const providerObj = this.serverless.service.provider;
const params = {
FunctionName: functionObj.name,
};
if ('awsKmsKeyArn' in functionObj && !_.isObject(functionObj.awsKmsKeyArn)) {
params.KMSKeyArn = functionObj.awsKmsKeyArn;
} else if (serviceObj && 'awsKmsKeyArn' in serviceObj && !_.isObject(serviceObj.awsKmsKeyArn)) {
params.KMSKeyArn = serviceObj.awsKmsKeyArn;
}
if ('description' in functionObj && !_.isObject(functionObj.description)) {
params.Description = functionObj.description;
}
if ('handler' in functionObj && !_.isObject(functionObj.handler)) {
params.Handler = functionObj.handler;
}
if ('memorySize' in functionObj && !_.isObject(functionObj.memorySize)) {
params.MemorySize = functionObj.memorySize;
} else if ('memorySize' in providerObj && !_.isObject(providerObj.memorySize)) {
params.MemorySize = providerObj.memorySize;
}
if ('timeout' in functionObj && !_.isObject(functionObj.timeout)) {
params.Timeout = functionObj.timeout;
} else if ('timeout' in providerObj && !_.isObject(providerObj.timeout)) {
params.Timeout = providerObj.timeout;
}
if (
'layers' in functionObj &&
Array.isArray(functionObj.layers) &&
!functionObj.layers.some(_.isObject)
) {
params.Layers = functionObj.layers;
}
if (functionObj.onError && !_.isObject(functionObj.onError)) {
params.DeadLetterConfig = {
TargetArn: functionObj.onError,
};
}
if (functionObj.environment || providerObj.environment) {
params.Environment = {};
params.Environment.Variables = Object.assign(
{},
providerObj.environment,
functionObj.environment
);
if (Object.values(params.Environment.Variables).some(value => _.isObject(value))) {
delete params.Environment;
} else {
Object.keys(params.Environment.Variables).forEach(key => {
// taken from the bash man pages
if (!key.match(/^[A-Za-z_][a-zA-Z0-9_]*$/)) {
const errorMessage = 'Invalid characters in environment variable';
throw new this.serverless.classes.Error(errorMessage);
}
if (params.Environment.Variables[key] != null) {
params.Environment.Variables[key] = String(params.Environment.Variables[key]);
}
});
}
}
if (functionObj.vpc || providerObj.vpc) {
const vpc = functionObj.vpc || providerObj.vpc;
params.VpcConfig = {};
if (Array.isArray(vpc.securityGroupIds) && !vpc.securityGroupIds.some(_.isObject)) {
params.VpcConfig.SecurityGroupIds = vpc.securityGroupIds;
}
if (Array.isArray(vpc.subnetIds) && !vpc.subnetIds.some(_.isObject)) {
params.VpcConfig.SubnetIds = vpc.subnetIds;
}
if (!Object.keys(params.VpcConfig).length) {
delete params.VpcConfig;
}
}
if ('role' in functionObj && !_.isObject(functionObj.role)) {
return this.normalizeArnRole(functionObj.role).then(roleArn => {
params.Role = roleArn;
return this.callUpdateFunctionConfiguration(params);
});
} else if ('role' in providerObj && !_.isObject(providerObj.role)) {
return this.normalizeArnRole(providerObj.role).then(roleArn => {
params.Role = roleArn;
return this.callUpdateFunctionConfiguration(params);
});
}
if (!Object.keys(_.omit(params, 'FunctionName')).length) {
return BbPromise.resolve();
}
return this.callUpdateFunctionConfiguration(params);
}
deployFunction() {
const artifactFileName = this.provider.naming.getFunctionArtifactName(this.options.function);
let artifactFilePath =
this.serverless.service.package.artifact || path.join(this.packagePath, artifactFileName);
// check if an artifact is used in function package level
const functionObject = this.serverless.service.getFunction(this.options.function);
if (_.get(functionObject, 'package.artifact')) {
artifactFilePath = functionObject.package.artifact;
}
const data = fs.readFileSync(artifactFilePath);
const remoteHash = this.serverless.service.provider.remoteFunctionData.Configuration.CodeSha256;
const localHash = crypto
.createHash('sha256')
.update(data)
.digest('base64');
if (remoteHash === localHash && !this.options.force) {
this.serverless.cli.log('Code not changed. Skipping function deployment.');
return BbPromise.resolve();
}
const params = {
FunctionName: this.options.functionObj.name,
ZipFile: data,
};
const stats = fs.statSync(artifactFilePath);
this.serverless.cli.log(
`Uploading function: ${this.options.function} (${filesize(stats.size)})...`
);
return this.provider.request('Lambda', 'updateFunctionCode', params).then(() => {
this.serverless.cli.log(`Successfully deployed function: ${this.options.function}`);
});
}
}
module.exports = AwsDeployFunction;