Skip to content

Commit 7c7aaea

Browse files
authored
Merge pull request #71 from HyperBrain/function-deploy
Support serverless deploy function
2 parents e184c3d + 8dd0743 commit 7c7aaea

File tree

4 files changed

+3449
-1
lines changed

4 files changed

+3449
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ function parameters (memory, etc.) cannot be changed for a deployed version
4040
by accident, as it can be done with the `$LATEST` qualifier.
4141
This adds an additional level of stability to your deployment process.
4242

43+
## Deploy a single function
44+
45+
The plugin supports `serverless deploy function` and moves the alias to the
46+
updated function version. However you must specify the `--force` switch on the
47+
commandline to enforce Serverless to deploy a new function ZIP regardless, if the
48+
code has changed or not. This is necessary to prevent setting the alias to a
49+
version of the function that has been deployed by another developer.
50+
4351
## Deploy an alias
4452

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

461469
## Version history
462470

471+
* 1.5.0
472+
* Support `serverless deploy function` [#29](https://github.com/HyperBrain/serverless-aws-alias/issues/29)
473+
463474
* 1.4.1
464475
* Fixed crash when using logs --tail
465476

index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const BbPromise = require('bluebird')
1717
, removeAlias = require('./lib/removeAlias')
1818
, logs = require('./lib/logs')
1919
, collectUserResources = require('./lib/collectUserResources')
20-
, uploadAliasArtifacts = require('./lib/uploadAliasArtifacts');
20+
, uploadAliasArtifacts = require('./lib/uploadAliasArtifacts')
21+
, updateFunctionAlias = require('./lib/updateFunctionAlias');
2122

2223
class AwsAlias {
2324

@@ -66,6 +67,7 @@ class AwsAlias {
6667
aliasRestructureStack,
6768
stackInformation,
6869
uploadAliasArtifacts,
70+
updateFunctionAlias,
6971
setBucketName,
7072
monitorStack
7173
);
@@ -120,6 +122,19 @@ class AwsAlias {
120122
'after:aws:deploy:deploy:updateStack': () => BbPromise.bind(this)
121123
.then(this.updateAliasStack),
122124

125+
'before:deploy:function:initialize': () => BbPromise.bind(this)
126+
.then(this.validate)
127+
.then(() => {
128+
// Force forced deploy
129+
if (!this._options.force) {
130+
return BbPromise.reject(new this.serverless.classes.Error("You must deploy single functions using --force with the alias plugin."));
131+
}
132+
return BbPromise.resolve();
133+
}),
134+
135+
'after:deploy:function:deploy': () => BbPromise.bind(this)
136+
.then(this.updateFunctionAlias),
137+
123138
'after:info:info': () => BbPromise.bind(this)
124139
.then(this.validate)
125140
.then(this.listAliases),

lib/updateFunctionAlias.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
const BbPromise = require("bluebird");
4+
5+
module.exports = {
6+
7+
updateFunctionAlias() {
8+
this._serverless.cli.log('Updating function alias...');
9+
10+
const func = this.serverless.service.getFunction(this.options.function);
11+
12+
// Publish the yet deployed $LATEST uploaded by Serverless and label it.
13+
14+
return BbPromise.try(() => {
15+
// Get the hash of the deployed function package
16+
const params = {
17+
FunctionName: func.name,
18+
Qualifier: "$LATEST"
19+
};
20+
21+
return this.provider.request(
22+
'Lambda',
23+
'getFunction',
24+
params,
25+
this.options.stage, this.options.region
26+
);
27+
})
28+
.then(result => {
29+
// Publish $LATEST
30+
const sha256 = result.Configuration.CodeSha256;
31+
const params = {
32+
FunctionName: func.name,
33+
CodeSha256: sha256,
34+
Description: "Deployed manually"
35+
};
36+
return this.provider.request(
37+
'Lambda',
38+
'publishVersion',
39+
params,
40+
this.options.stage, this.options.region
41+
);
42+
})
43+
.then(result => {
44+
// Label it
45+
const version = result.Version;
46+
const params = {
47+
FunctionName: func.name,
48+
Name: this._alias,
49+
FunctionVersion: version,
50+
Description: 'Deployed manually'
51+
};
52+
return this.provider.request(
53+
'Lambda',
54+
'updateAlias',
55+
params,
56+
this.options.stage, this.options.region
57+
);
58+
})
59+
.then(result => {
60+
this.serverless.cli.log(`Successfully updated alias: ${this.options.function}@${this._alias} -> ${result.FunctionVersion}`);
61+
return BbPromise.resolve();
62+
});
63+
}
64+
65+
};

0 commit comments

Comments
 (0)