/
index.js
157 lines (141 loc) · 5.59 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const _ = require('lodash');
class AwsCompileCloudWatchEventEvents {
constructor(serverless) {
this.serverless = serverless;
this.provider = this.serverless.getProvider('aws');
this.hooks = {
'package:compileEvents': this.compileCloudWatchEventEvents.bind(this),
};
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'cloudwatchEvent', {
type: 'object',
properties: {
event: { type: 'object' },
input: {
oneOf: [{ type: 'string', maxLength: 8192 }, { type: 'object' }],
},
inputPath: { type: 'string', minLength: 1, maxLength: 256 },
inputTransformer: {
type: 'object',
properties: {
inputPathsMap: {
type: 'object',
additionalProperties: { type: 'string', minLength: 1 },
},
inputTemplate: { type: 'string', minLength: 1, maxLength: 8192 },
},
required: ['inputTemplate'],
additionalProperties: false,
},
description: { type: 'string', maxLength: 512 },
name: { type: 'string', pattern: '[a-zA-Z0-9-_.]+', minLength: 1, maxLength: 64 },
enabled: { type: 'boolean' },
},
oneOf: [
{ required: ['input'] },
{ required: ['inputPath'] },
{ required: ['inputTransformer'] },
{ required: [] },
],
additionalProperties: false,
});
}
compileCloudWatchEventEvents() {
this.serverless.service.getAllFunctions().forEach(functionName => {
const functionObj = this.serverless.service.getFunction(functionName);
let cloudWatchEventNumberInFunction = 0;
if (functionObj.events) {
functionObj.events.forEach(event => {
if (event.cloudwatchEvent) {
cloudWatchEventNumberInFunction++;
let State;
let Input;
let InputTransformer;
const EventPattern = JSON.stringify(event.cloudwatchEvent.event);
State = 'ENABLED';
if (event.cloudwatchEvent.enabled === false) {
State = 'DISABLED';
}
const Name = event.cloudwatchEvent.name;
const InputPath = event.cloudwatchEvent.inputPath;
const Description = event.cloudwatchEvent.description;
Input = event.cloudwatchEvent.input;
InputTransformer = event.cloudwatchEvent.inputTransformer;
if (Input && typeof Input === 'object') {
Input = JSON.stringify(Input);
}
if (Input && typeof Input === 'string') {
// escape quotes to favor JSON.parse
Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line
}
if (InputTransformer) {
InputTransformer = this.formatInputTransformer(InputTransformer);
}
const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(functionName);
const cloudWatchLogicalId = this.provider.naming.getCloudWatchEventLogicalId(
functionName,
cloudWatchEventNumberInFunction
);
const lambdaPermissionLogicalId = this.provider.naming.getLambdaCloudWatchEventPermissionLogicalId(
functionName,
cloudWatchEventNumberInFunction
);
const cloudWatchId = this.provider.naming.getCloudWatchEventId(functionName);
const cloudWatchEventRuleTemplate = `
{
"Type": "AWS::Events::Rule",
"Properties": {
"EventPattern": ${EventPattern.replace(/\\n|\\r/g, '')},
"State": "${State}",
${Description ? `"Description": "${Description}",` : ''}
${Name ? `"Name": "${Name}",` : ''}
"Targets": [{
${Input ? `"Input": "${Input.replace(/\\n|\\r/g, '')}",` : ''}
${InputPath ? `"InputPath": "${InputPath.replace(/\r?\n/g, '')}",` : ''}
${InputTransformer ? `"InputTransformer": ${InputTransformer},` : ''}
"Arn": { "Fn::GetAtt": ["${lambdaLogicalId}", "Arn"] },
"Id": "${cloudWatchId}"
}]
}
}
`;
const permissionTemplate = `
{
"Type": "AWS::Lambda::Permission",
"Properties": {
"FunctionName": { "Fn::GetAtt": ["${lambdaLogicalId}", "Arn"] },
"Action": "lambda:InvokeFunction",
"Principal": "events.amazonaws.com",
"SourceArn": { "Fn::GetAtt": ["${cloudWatchLogicalId}", "Arn"] }
}
}
`;
const newCloudWatchEventRuleObject = {
[cloudWatchLogicalId]: JSON.parse(cloudWatchEventRuleTemplate),
};
const newPermissionObject = {
[lambdaPermissionLogicalId]: JSON.parse(permissionTemplate),
};
_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
newCloudWatchEventRuleObject,
newPermissionObject
);
}
});
}
});
}
formatInputTransformer(inputTransformer) {
const cfmOutput = {
// InputTemplate is required
InputTemplate: inputTransformer.inputTemplate,
};
// InputPathsMap is optional
if (inputTransformer.inputPathsMap) {
cfmOutput.InputPathsMap = inputTransformer.inputPathsMap;
}
return JSON.stringify(cfmOutput);
}
}
module.exports = AwsCompileCloudWatchEventEvents;