forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (71 loc) · 2.23 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
'use strict';
const BbPromise = require('bluebird');
const fs = require('fs');
const SDK = require('../');
const validate = require('../lib/validate');
// The Package plugin which is used to zip the service
const Package = require('../../package');
class AwsDeployFunction {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.provider = 'aws';
this.sdk = new SDK(serverless);
this.pkg = new Package(this.serverless, this.options);
Object.assign(this, validate);
this.hooks = {
'deploy:function:deploy': () => BbPromise.bind(this)
.then(this.validate)
.then(this.checkIfFunctionExists)
.then(this.zipFunction)
.then(this.deployFunction)
.then(this.cleanup),
};
}
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,
};
this.sdk.request(
'Lambda',
'getFunction',
params,
this.options.stage, this.options.region
).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);
});
return BbPromise.resolve();
}
zipFunction() {
return this.pkg.zipService();
}
deployFunction() {
const data = fs.readFileSync(this.serverless.service.package.artifact);
const params = {
FunctionName: this.options.functionObj.name,
ZipFile: data,
};
this.sdk.request(
'Lambda',
'updateFunctionCode',
params,
this.options.stage, this.options.region
);
this.serverless.cli.log(`Successfully deployed function "${this.options.function}"`);
return BbPromise.resolve();
}
cleanup() {
return this.pkg.cleanup();
}
}
module.exports = AwsDeployFunction;