Skip to content

Commit

Permalink
Merge pull request #71 from HyperBrain/function-deploy
Browse files Browse the repository at this point in the history
Support serverless deploy function
  • Loading branch information
HyperBrain authored Aug 17, 2017
2 parents e184c3d + 8dd0743 commit 7c7aaea
Show file tree
Hide file tree
Showing 4 changed files with 3,449 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ function parameters (memory, etc.) cannot be changed for a deployed version
by accident, as it can be done with the `$LATEST` qualifier.
This adds an additional level of stability to your deployment process.

## Deploy a single function

The plugin supports `serverless deploy function` and moves the alias to the
updated function version. However you must specify the `--force` switch on the
commandline to enforce Serverless to deploy a new function ZIP regardless, if the
code has changed or not. This is necessary to prevent setting the alias to a
version of the function that has been deployed by another developer.

## Deploy an alias

To deploy an alias to a stage, just add the `--alias` option to `serverless deploy`
Expand Down Expand Up @@ -460,6 +468,9 @@ and _serverless.service.provider.deployedAliasTemplates[]_.

## Version history

* 1.5.0
* Support `serverless deploy function` [#29](https://github.com/HyperBrain/serverless-aws-alias/issues/29)

* 1.4.1
* Fixed crash when using logs --tail

Expand Down
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const BbPromise = require('bluebird')
, removeAlias = require('./lib/removeAlias')
, logs = require('./lib/logs')
, collectUserResources = require('./lib/collectUserResources')
, uploadAliasArtifacts = require('./lib/uploadAliasArtifacts');
, uploadAliasArtifacts = require('./lib/uploadAliasArtifacts')
, updateFunctionAlias = require('./lib/updateFunctionAlias');

class AwsAlias {

Expand Down Expand Up @@ -66,6 +67,7 @@ class AwsAlias {
aliasRestructureStack,
stackInformation,
uploadAliasArtifacts,
updateFunctionAlias,
setBucketName,
monitorStack
);
Expand Down Expand Up @@ -120,6 +122,19 @@ class AwsAlias {
'after:aws:deploy:deploy:updateStack': () => BbPromise.bind(this)
.then(this.updateAliasStack),

'before:deploy:function:initialize': () => BbPromise.bind(this)
.then(this.validate)
.then(() => {
// Force forced deploy
if (!this._options.force) {
return BbPromise.reject(new this.serverless.classes.Error("You must deploy single functions using --force with the alias plugin."));
}
return BbPromise.resolve();
}),

'after:deploy:function:deploy': () => BbPromise.bind(this)
.then(this.updateFunctionAlias),

'after:info:info': () => BbPromise.bind(this)
.then(this.validate)
.then(this.listAliases),
Expand Down
65 changes: 65 additions & 0 deletions lib/updateFunctionAlias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const BbPromise = require("bluebird");

module.exports = {

updateFunctionAlias() {
this._serverless.cli.log('Updating function alias...');

const func = this.serverless.service.getFunction(this.options.function);

// Publish the yet deployed $LATEST uploaded by Serverless and label it.

return BbPromise.try(() => {
// Get the hash of the deployed function package
const params = {
FunctionName: func.name,
Qualifier: "$LATEST"
};

return this.provider.request(
'Lambda',
'getFunction',
params,
this.options.stage, this.options.region
);
})
.then(result => {
// Publish $LATEST
const sha256 = result.Configuration.CodeSha256;
const params = {
FunctionName: func.name,
CodeSha256: sha256,
Description: "Deployed manually"
};
return this.provider.request(
'Lambda',
'publishVersion',
params,
this.options.stage, this.options.region
);
})
.then(result => {
// Label it
const version = result.Version;
const params = {
FunctionName: func.name,
Name: this._alias,
FunctionVersion: version,
Description: 'Deployed manually'
};
return this.provider.request(
'Lambda',
'updateAlias',
params,
this.options.stage, this.options.region
);
})
.then(result => {
this.serverless.cli.log(`Successfully updated alias: ${this.options.function}@${this._alias} -> ${result.FunctionVersion}`);
return BbPromise.resolve();
});
}

};
Loading

0 comments on commit 7c7aaea

Please sign in to comment.