Skip to content

Commit cfaa125

Browse files
authored
Merge pull request #448 from zirkelc/inputTransformer
Input transformer for CloudWatch / EventBridge events
2 parents 754c54c + 69dea38 commit cfaa125

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ stepFunctions:
10011001
...
10021002
```
10031003

1004-
#### Specify Input or Inputpath
1004+
#### Specify Input or Inputpath or InputTransformer
10051005

10061006
You can specify input values ​​to the Lambda function.
10071007

@@ -1034,6 +1034,19 @@ stepFunctions:
10341034
state:
10351035
- pending
10361036
inputPath: '$.stageVariables'
1037+
- cloudwatchEvent:
1038+
event:
1039+
source:
1040+
- "aws.ec2"
1041+
detail-type:
1042+
- "EC2 Instance State-change Notification"
1043+
detail:
1044+
state:
1045+
- pending
1046+
inputTransformer:
1047+
inputPathsMap:
1048+
stage: '$.stageVariables'
1049+
inputTemplate: '{ "stage": <stage> }'
10371050
definition:
10381051
...
10391052
```

lib/deploy/events/cloudWatchEvent/compileCloudWatchEventEvents.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module.exports = {
2323
let Name;
2424
let EventBusName;
2525
let IamRole;
26+
let InputTransformer;
27+
let InputPathsMap;
28+
let InputTemplate;
2629
let DeadLetterConfig;
2730

2831
if (typeof eventRule === 'object') {
@@ -42,15 +45,18 @@ module.exports = {
4245
}
4346
Input = eventRule.input;
4447
InputPath = eventRule.inputPath;
48+
InputTransformer = eventRule.inputTransformer;
49+
InputPathsMap = InputTransformer && eventRule.inputTransformer.inputPathsMap;
50+
InputTemplate = InputTransformer && eventRule.inputTransformer.inputTemplate;
4551
Description = eventRule.description;
4652
Name = eventRule.name;
4753
EventBusName = JSON.stringify(eventRule.eventBusName);
4854
IamRole = eventRule.iamRole;
4955
DeadLetterConfig = JSON.stringify(eventRule.deadLetterConfig);
5056

51-
if (Input && InputPath) {
57+
if ([Input, InputPath, InputTransformer].filter(Boolean).length > 1) {
5258
const errorMessage = [
53-
'You can\'t set both input & inputPath properties at the',
59+
'You can\'t set input, inputPath and inputTransformer properties at the',
5460
'same time for cloudwatch events.',
5561
'Please check the AWS docs for more info',
5662
].join('');
@@ -64,6 +70,16 @@ module.exports = {
6470
// escape quotes to favor JSON.parse
6571
Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line
6672
}
73+
74+
// no need to escape quotes in inputPathsMap
75+
// because we add it as an object to the template
76+
if (InputPathsMap && typeof InputPathsMap === 'object') {
77+
InputPathsMap = JSON.stringify(InputPathsMap);
78+
}
79+
if (InputTemplate && typeof InputTemplate === 'string') {
80+
// escape quotes to favor JSON.parse
81+
InputTemplate = InputTemplate.replace(/\"/g, '\\"'); // eslint-disable-line
82+
}
6783
} else {
6884
const errorMessage = [
6985
`CloudWatch event of stateMachine "${stateMachineName}" is not an object`,
@@ -94,6 +110,10 @@ module.exports = {
94110
"Targets": [{
95111
${Input ? `"Input": "${Input.replace(/\\n|\\r/g, '')}",` : ''}
96112
${InputPath ? `"InputPath": "${InputPath.replace(/\r?\n/g, '')}",` : ''}
113+
${InputTransformer ? `"InputTransformer": {
114+
"InputPathsMap": ${InputPathsMap},
115+
"InputTemplate": "${InputTemplate}"
116+
},` : ''}
97117
"Arn": { "Ref": "${stateMachineLogicalId}" },
98118
"Id": "${cloudWatchId}",
99119
${DeadLetterConfig ? `"DeadLetterConfig":{ "Arn" : ${DeadLetterConfig} },` : ''}

lib/deploy/events/cloudWatchEvent/compileCloudWatchEventEvents.test.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,43 @@ describe('awsCompileCloudWatchEventEvents', () => {
419419
.Properties.Targets[0].Input).to.equal('{"key":"value"}');
420420
});
421421

422-
itParam('should throw an error when both Input and InputPath are set', ['cloudwatchEvent', 'eventBridge'], (source) => {
422+
itParam('should respect inputTransformer variable', ['cloudwatchEvent', 'eventBridge'], (source) => {
423+
serverlessStepFunctions.serverless.service.stepFunctions = {
424+
stateMachines: {
425+
first: {
426+
events: [
427+
{
428+
[source]: {
429+
event: {
430+
source: ['aws.ec2'],
431+
'detail-type': ['EC2 Instance State-change Notification'],
432+
detail: { state: ['pending'] },
433+
},
434+
enabled: false,
435+
inputTransformer: {
436+
inputPathsMap: {
437+
stage: '$.stageVariables',
438+
},
439+
inputTemplate: '{ "stage": <stage> }',
440+
},
441+
},
442+
},
443+
],
444+
},
445+
},
446+
};
447+
448+
serverlessStepFunctions.compileCloudWatchEventEvents();
449+
450+
expect(serverlessStepFunctions.serverless.service
451+
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
452+
.Properties.Targets[0].InputTransformer.InputPathsMap).to.have.property('stage', '$.stageVariables');
453+
expect(serverlessStepFunctions.serverless.service
454+
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
455+
.Properties.Targets[0].InputTransformer.InputTemplate).to.equal('{ "stage": <stage> }');
456+
});
457+
458+
itParam('should throw an error when Input and InputPath and InputTransformer are set', ['cloudwatchEvent', 'eventBridge'], (source) => {
423459
serverlessStepFunctions.serverless.service.stepFunctions = {
424460
stateMachines: {
425461
first: {
@@ -436,6 +472,12 @@ describe('awsCompileCloudWatchEventEvents', () => {
436472
key: 'value',
437473
},
438474
inputPath: '$.stageVariables',
475+
inputTransformer: {
476+
inputPathsMap: {
477+
stage: '$.stageVariables',
478+
},
479+
inputTemplate: '{ "stage": <stage> }',
480+
},
439481
},
440482
},
441483
],

0 commit comments

Comments
 (0)