/
getStackInfo.js
123 lines (110 loc) · 3.95 KB
/
getStackInfo.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
const BbPromise = require('bluebird');
const resolveCfImportValue = require('../utils/resolveCfImportValue');
module.exports = {
getStackInfo() {
// NOTE: this is the global gatheredData object which will be passed around
this.gatheredData = {
info: {
functions: [],
layers: [],
endpoints: [],
service: this.serverless.service.service,
stage: this.provider.getStage(),
region: this.provider.getRegion(),
stack: this.provider.naming.getStackName(),
},
outputs: [],
};
const stackName = this.provider.naming.getStackName();
const stackData = {};
const sdkRequests = [
this.provider
.request('CloudFormation', 'describeStacks', { StackName: stackName })
.then(result => {
if (result) stackData.outputs = result.Stacks[0].Outputs;
}),
];
const httpApiId =
this.serverless.service.provider.httpApi && this.serverless.service.provider.httpApi.id;
if (httpApiId) {
sdkRequests.push(
(httpApiId['Fn::ImportValue']
? resolveCfImportValue(this.provider, httpApiId['Fn::ImportValue'])
: BbPromise.resolve(httpApiId)
)
.then(id => {
return this.provider.request('ApiGatewayV2', 'getApi', { ApiId: id });
})
.then(
result => {
stackData.externalHttpApiEndpoint = result.ApiEndpoint;
},
error => {
throw new this.serverless.classes.Error(
`Could not resolve provider.httpApi.id parameter. ${error.message}`
);
}
)
);
}
// Get info from CloudFormation Outputs
return BbPromise.all(sdkRequests).then(() => {
let outputs;
if (stackData.outputs) {
({ outputs } = stackData);
const serviceEndpointOutputRegex = this.provider.naming.getServiceEndpointRegex();
// Outputs
this.gatheredData.outputs = outputs;
// Functions
this.serverless.service.getAllFunctions().forEach(func => {
const functionInfo = {};
functionInfo.name = func;
functionInfo.deployedName = this.serverless.service.getFunction(func).name;
this.gatheredData.info.functions.push(functionInfo);
});
// Layers
this.serverless.service.getAllLayers().forEach(layer => {
const layerInfo = {};
layerInfo.name = layer;
const layerOutputId = this.provider.naming.getLambdaLayerOutputLogicalId(layer);
for (const output of outputs) {
if (output.OutputKey === layerOutputId) {
layerInfo.arn = output.OutputValue;
break;
}
}
this.gatheredData.info.layers.push(layerInfo);
});
// CloudFront
const cloudFrontDomainName = outputs.find(
output =>
output.OutputKey === this.provider.naming.getCloudFrontDistributionDomainNameLogicalId()
);
if (cloudFrontDomainName) {
this.gatheredData.info.cloudFront = cloudFrontDomainName.OutputValue;
}
// Endpoints
outputs
.filter(x => x.OutputKey.match(serviceEndpointOutputRegex))
.forEach(x => {
if (x.OutputKey === 'HttpApiUrl') {
this.gatheredData.info.endpoints.push(`httpApi: ${x.OutputValue}`);
} else {
this.gatheredData.info.endpoints.push(x.OutputValue);
}
if (
this.serverless.service.deployment &&
this.serverless.service.deployment.deploymentId
) {
this.serverless.service.deployment.apiId = x.OutputValue.split('//')[1].split('.')[0];
}
});
}
if (stackData.externalHttpApiEndpoint) {
this.gatheredData.info.endpoints.push(`httpApi: ${stackData.externalHttpApiEndpoint}`);
}
return BbPromise.resolve();
});
},
};