mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
104 lines
2.8 KiB
JavaScript
104 lines
2.8 KiB
JavaScript
'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.logStatus)
|
|
.then(this.checkIfFunctionExists)
|
|
.then(this.zipFunction)
|
|
.then(this.deployFunction)
|
|
.then(this.cleanup),
|
|
};
|
|
}
|
|
|
|
logStatus() {
|
|
this.serverless.cli.log(`Deploying function: ${this.options.function}...`);
|
|
BbPromise.resolve();
|
|
}
|
|
|
|
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.packageFunction(this.options.function);
|
|
}
|
|
|
|
deployFunction() {
|
|
const data = fs.readFileSync(this.options.functionObj.artifact);
|
|
|
|
const params = {
|
|
FunctionName: this.options.functionObj.name,
|
|
ZipFile: data,
|
|
};
|
|
|
|
// Get function stats
|
|
const stats = fs.statSync(this.options.functionObj.artifact);
|
|
const fileSizeInBytes = stats.size;
|
|
const fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
|
|
this.serverless.cli.log(
|
|
[
|
|
`Uploading function: ${this.options.function}`,
|
|
`(${Math.round(fileSizeInMegabytes * 100) / 100}MB)...`,
|
|
].join(''));
|
|
|
|
// Perform upload
|
|
return this.sdk.request(
|
|
'Lambda',
|
|
'updateFunctionCode',
|
|
params,
|
|
this.options.stage, this.options.region
|
|
).then(() => {
|
|
this.serverless.cli.log(`Successfully deployed function: ${this.options.function}`);
|
|
});
|
|
}
|
|
|
|
cleanup() {
|
|
return this.pkg.cleanup();
|
|
}
|
|
}
|
|
|
|
module.exports = AwsDeployFunction;
|