-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathcreateAliasStack.js
107 lines (84 loc) · 2.98 KB
/
createAliasStack.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
'use strict';
/**
* Create the alias stack for the service.
*
* The alias stack contains the function definition and exposes the functions
* as CF output variables that are referenced in the stage dependent CF stacks.
*/
const BbPromise = require('bluebird');
const _ = require('lodash');
const path = require('path');
module.exports = {
createAlias() {
this._serverless.cli.log(`Creating Alias Stack '${this._alias}' ...`);
const stackName = `${this._provider.naming.getStackName()}-${this._alias}`;
let stackTags = { STAGE: this._options.stage, ALIAS: this._alias };
// Merge additional stack tags
if (_.isObject(this._serverless.service.provider.stackTags)) {
stackTags = _.extend(stackTags, this._serverless.service.provider.stackTags);
}
const params = {
StackName: stackName,
OnFailure: 'DELETE',
Capabilities: [
'CAPABILITY_IAM',
'CAPABILITY_NAMED_IAM',
],
Parameters: [],
TemplateBody: JSON.stringify(this._serverless.service.provider.compiledCloudFormationAliasCreateTemplate),
Tags: _.map(_.keys(stackTags), key => ({ Key: key, Value: stackTags[key] }))
};
return this._provider.request(
'CloudFormation',
'createStack',
params
).then(cfData => this.monitorStack('create', cfData));
},
createAliasStack() {
this._aliasStackName = `${this._provider.naming.getStackName()}-${this._alias}`;
if (/^[^a-zA-Z].+|.*[^a-zA-Z0-9-].*/.test(this._aliasStackName) || this._aliasStackName.length > 128) {
const errorMessage = [
`The stack alias name "${this._aliasStackName}" is not valid. `,
'A service name should only contain alphanumeric',
' (case sensitive) and hyphens. It should start',
' with an alphabetic character and shouldn\'t',
' exceed 128 characters.',
].join('');
throw new this._serverless.classes.Error(errorMessage);
}
return BbPromise.bind(this)
// always write the template to disk, whether we are deploying or not
.then(this.writeAliasTemplateToDisk)
.then(this.checkAliasStack);
},
checkAliasStack() {
if (this._options.noDeploy) {
return BbPromise.resolve();
}
return this._provider.request('CloudFormation',
'describeStackResources',
{ StackName: this._aliasStackName })
.then(() => BbPromise.resolve('alreadyCreated'))
.catch(e => {
if (_.includes(e.message, 'does not exist')) {
if (this._serverless.service.provider.deploymentBucket) {
this._createLater = true;
return BbPromise.resolve();
}
return BbPromise.bind(this)
.then(this.createAlias);
}
return BbPromise.reject(e);
});
},
writeAliasTemplateToDisk() {
if (this._serverless.service.provider.deploymentBucket) {
return BbPromise.resolve();
}
const cfTemplateFilePath = path.join(this._serverless.config.servicePath,
'.serverless', 'cloudformation-template-create-alias-stack.json');
this._serverless.utils.writeFileSync(cfTemplateFilePath,
this._serverless.service.provider.compiledCloudFormationAliasCreateTemplate);
return BbPromise.resolve();
}
};