forked from Droplr/serverless-api-cloudfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (99 loc) · 3.64 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
121
122
123
124
125
126
const path = require('path');
const _ = require('lodash');
const chalk = require('chalk');
class ServerlessApiCloudFrontPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'before:deploy:createDeploymentArtifacts': this.createDeploymentArtifacts.bind(this),
'aws:info:displayStackOutputs': this.printSummary.bind(this),
};
}
createDeploymentArtifacts() {
const baseResources = this.serverless.service.provider.compiledCloudFormationTemplate;
return this.serverless.yamlParser.parse(
path.resolve(__dirname, 'resources.yml')
).then((resources) => {
this.prepareResources(resources);
return _.merge(baseResources, resources);
});
}
printSummary() {
const cloudTemplate = this.serverless;
const awsInfo = _.find(this.serverless.pluginManager.getPlugins(), (plugin) => {
return plugin.constructor.name === 'AwsInfo';
});
if (!awsInfo || !awsInfo.gatheredData) {
return;
}
const outputs = awsInfo.gatheredData.outputs;
const apiDistributionDomain = _.find(outputs, (output) => {
return output.OutputKey === 'ApiDistribution';
});
if (!apiDistributionDomain || !apiDistributionDomain.OutputValue) {
return ;
}
const cnameDomain = this.getConfig('domain', '-');
this.serverless.cli.consoleLog(chalk.yellow('CloudFront domain name'));
this.serverless.cli.consoleLog(` ${apiDistributionDomain.OutputValue} (CNAME: ${cnameDomain})`);
}
prepareResources(resources) {
const distributionConfig = resources.Resources.ApiDistribution.Properties.DistributionConfig;
this.prepareLogging(distributionConfig);
this.prepareDomain(distributionConfig);
this.preparePriceClass(distributionConfig);
this.prepareOrigins(distributionConfig);
this.prepareComment(distributionConfig);
this.prepareCertificate(distributionConfig);
this.prepareWaf(distributionConfig);
}
prepareLogging(distributionConfig) {
const loggingBucket = this.getConfig('logging.bucket', null);
if (loggingBucket !== null) {
distributionConfig.Logging.Bucket = loggingBucket;
distributionConfig.Logging.Prefix = this.getConfig('logging.prefix', '');
} else {
delete distributionConfig.Logging;
}
}
prepareDomain(distributionConfig) {
const domain = this.getConfig('domain', null);
if (domain !== null) {
distributionConfig.Aliases = [ domain ];
} else {
delete distributionConfig.Aliases;
}
}
preparePriceClass(distributionConfig) {
const priceClass = this.getConfig('priceClass', 'PriceClass_All');
distributionConfig.PriceClass = priceClass;
}
prepareOrigins(distributionConfig) {
distributionConfig.Origins[0].OriginPath = `/${this.options.stage}`;
}
prepareComment(distributionConfig) {
const name = this.serverless.getProvider('aws').naming.getApiGatewayName();
distributionConfig.Comment = `Serverless Managed ${name}`;
}
prepareCertificate(distributionConfig) {
const certificate = this.getConfig('certificate', null);
if (certificate !== null) {
distributionConfig.ViewerCertificate.AcmCertificateArn = certificate;
} else {
delete distributionConfig.ViewerCertificate;
}
}
prepareWaf(distributionConfig) {
const waf = this.getConfig('waf', null);
if (waf !== null) {
distributionConfig.WebACLId = waf;
} else {
delete distributionConfig.WebACLId;
}
}
getConfig(field, defaultValue) {
return _.get(this.serverless, `service.custom.apiCloudFront.${field}`, defaultValue)
}
}
module.exports = ServerlessApiCloudFrontPlugin;