/
index.js
120 lines (101 loc) · 4.39 KB
/
index.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
'use strict';
/* eslint-disable max-len */
// NOTE: --> Keep this file in sync with ./hack/updateStage.js
const _ = require('lodash');
const BbPromise = require('bluebird');
const ensureApiGatewayCloudWatchRole = require('../../../lib/ensureApiGatewayCloudWatchRole');
module.exports = {
compileStage() {
const service = this.serverless.service.service;
const stage = this.options.stage;
const provider = this.serverless.service.provider;
const cfTemplate = this.serverless.service.provider.compiledCloudFormationTemplate;
// logs
const logs = provider.logs && provider.logs.restApi;
// TracingEnabled
const tracing = provider.tracing;
const TracingEnabled = Boolean(tracing && tracing.apiGateway);
// Tags
const tagsMerged = Object.assign({}, provider.stackTags, provider.tags);
const Tags = Object.entries(tagsMerged).map(pair => ({
Key: pair[0],
Value: pair[1],
}));
// NOTE: the DeploymentId is random, therefore we rely on prior usage here
// --- currently commented out since this is done via the SDK in updateStage.js ---
// const deploymentId = this.apiGatewayDeploymentLogicalId;
// --------------------------------------------------------------------------------
const logGroupLogicalId = this.provider.naming.getApiGatewayLogGroupLogicalId();
this.apiGatewayStageLogicalId = this.provider.naming.getStageLogicalId();
// NOTE: right now we're only using a dedicated Stage resource
// - if AWS X-Ray tracing is enabled
// - if Tags are provided
// - if logs are enabled
// We'll change this in the future so that users can
// opt-in for other features as well
if (logs || TracingEnabled || Tags.length > 0) {
// --- currently commented out since this is done via the SDK in updateStage.js ---
// const stageResource = {
// Type: 'AWS::ApiGateway::Stage',
// Properties: {
// DeploymentId: {
// Ref: deploymentId,
// },
// RestApiId: this.provider.getApiGatewayRestApiId(),
// StageName: this.provider.getStage(),
// TracingEnabled,
// Tags,
// },
// };
// --------------------------------------------------------------------------------
// create log-specific resources
if (logs) {
// --- currently commented out since this is done via the SDK in updateStage.js ---
// _.merge(stageResource.Properties, {
// AccessLogSetting: {
// DestinationArn: {
// 'Fn::GetAtt': [
// logGroupLogicalId,
// 'Arn',
// ],
// },
// // eslint-disable-next-line
// Format: 'requestId: $context.requestId, ip: $context.identity.sourceIp, caller: $context.identity.caller, user: $context.identity.user, requestTime: $context.requestTime, httpMethod: $context.httpMethod, resourcePath: $context.resourcePath, status: $context.status, protocol: $context.protocol, responseLength: $context.responseLength',
// },
// MethodSettings: [
// {
// DataTraceEnabled: true,
// HttpMethod: '*',
// ResourcePath: '/*',
// LoggingLevel: 'INFO',
// },
// ],
// });
// --------------------------------------------------------------------------------
_.merge(cfTemplate.Resources, {
[logGroupLogicalId]: getLogGroupResource(service, stage, this.provider),
});
return ensureApiGatewayCloudWatchRole(this.provider);
}
// --- currently commented out since this is done via the SDK in updateStage.js ---
// _.merge(cfTemplate.Resources, { [this.apiGatewayStageLogicalId]: stageResource });
// we need to remove the stage name from the Deployment resource
// delete cfTemplate.Resources[deploymentId].Properties.StageName;
// --------------------------------------------------------------------------------
}
return BbPromise.resolve();
},
};
function getLogGroupResource(service, stage, provider) {
const resource = {
Type: 'AWS::Logs::LogGroup',
Properties: {
LogGroupName: `/aws/api-gateway/${service}-${stage}`,
},
};
const logRetentionInDays = provider.getLogRetentionInDays();
if (logRetentionInDays) {
resource.Properties.RetentionInDays = logRetentionInDays;
}
return resource;
}