Skip to content

Commit 309fde5

Browse files
author
horike37
committed
Merge branch 'master' of github.com:serverless-operations/serverless-step-functions
2 parents 8f32a39 + ca340cb commit 309fde5

File tree

6 files changed

+139
-42
lines changed

6 files changed

+139
-42
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Serverless Framework v2.32.0 or later is required.
4242
- [Enabling / Disabling](#enabling--disabling)
4343
- [Specify Name and Description](#specify-name-and-description)
4444
- [Scheduled Events IAM Role](#scheduled-events-iam-role)
45+
- [Specify InputTransformer](#specify-inputtransformer)
4546
- [CloudWatch Event](#cloudwatch-event)
4647
- [Simple event definition](#simple-event-definition)
4748
- [Enabling / Disabling](#enabling--disabling-1)
@@ -929,6 +930,26 @@ events:
929930
role: arn:aws:iam::xxxxxxxx:role/yourRole
930931
```
931932

933+
#### Specify InputTransformer
934+
935+
You can specify input values ​​to the Lambda function.
936+
937+
```yml
938+
stepFunctions:
939+
stateMachines:
940+
stateMachineScheduled:
941+
events:
942+
- schedule:
943+
rate: cron(30 12 ? * 1-5 *)
944+
inputTransformer:
945+
inputPathsMap:
946+
time: '$.time'
947+
stage: '$.stageVariables'
948+
inputTemplate: '{"time": <time>, "stage" : <stage> }'
949+
definition:
950+
...
951+
```
952+
932953
### CloudWatch Event / EventBridge
933954

934955
#### Simple event definition

lib/deploy/events/schedule/compileScheduledEvents.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ module.exports = {
1717
let State;
1818
let Input;
1919
let InputPath;
20+
let InputTransformer;
21+
let InputTemplate;
22+
let InputPathsMap;
2023
let Name;
2124
let Description;
2225

@@ -39,10 +42,13 @@ module.exports = {
3942
}
4043
Input = event.schedule.input;
4144
InputPath = event.schedule.inputPath;
45+
InputTransformer = event.schedule.inputTransformer;
46+
InputPathsMap = InputTransformer && event.schedule.inputTransformer.inputPathsMap;
47+
InputTemplate = InputTransformer && event.schedule.inputTransformer.inputTemplate;
4248
Name = event.schedule.name;
4349
Description = event.schedule.description;
4450

45-
if (Input && InputPath) {
51+
if ([Input, InputPath, InputTransformer].filter(Boolean).length > 1) {
4652
const errorMessage = [
4753
'You can\'t set both input & inputPath properties at the',
4854
'same time for schedule events.',
@@ -59,6 +65,15 @@ module.exports = {
5965
// escape quotes to favor JSON.parse
6066
Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line
6167
}
68+
// no need to escape quotes in inputPathsMap
69+
// because we add it as an object to the template
70+
if (InputPathsMap && typeof InputPathsMap === 'object') {
71+
InputPathsMap = JSON.stringify(InputPathsMap);
72+
}
73+
if (InputTemplate && typeof InputTemplate === 'string') {
74+
// escape quotes to favor JSON.parse
75+
InputTemplate = InputTemplate.replace(/\"/g, '\\"'); // eslint-disable-line
76+
}
6277
} else if (typeof event.schedule === 'string') {
6378
ScheduleExpression = event.schedule;
6479
State = 'ENABLED';
@@ -104,6 +119,10 @@ module.exports = {
104119
"Targets": [{
105120
${Input ? `"Input": "${Input}",` : ''}
106121
${InputPath ? `"InputPath": "${InputPath}",` : ''}
122+
${InputTransformer ? `"InputTransformer": {
123+
"InputPathsMap": ${InputPathsMap},
124+
"InputTemplate": "${InputTemplate}"
125+
},` : ''}
107126
"Arn": { "Ref": "${stateMachineLogicalId}" },
108127
"Id": "${scheduleId}",
109128
"RoleArn": ${roleArn}

lib/deploy/events/schedule/compileScheduledEvents.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -360,5 +360,67 @@ describe('#httpValidate()', () => {
360360
.Resources,
361361
).to.deep.equal({});
362362
});
363+
364+
365+
it('should respect inputTransformer variable', () => {
366+
serverlessStepFunctions.serverless.service.stepFunctions = {
367+
stateMachines: {
368+
first: {
369+
events: [
370+
{
371+
schedule: {
372+
rate: 'rate(10 minutes)',
373+
enabled: false,
374+
inputTransformer: {
375+
inputPathsMap: {
376+
stage: '$.stageVariables',
377+
},
378+
inputTemplate: '{ "stage": <stage> }',
379+
},
380+
},
381+
},
382+
],
383+
},
384+
},
385+
};
386+
387+
serverlessStepFunctions.compileScheduledEvents();
388+
389+
expect(serverlessStepFunctions.serverless.service
390+
.provider.compiledCloudFormationTemplate.Resources.FirstStepFunctionsEventsRuleSchedule1
391+
.Properties.Targets[0].InputTransformer.InputPathsMap).to.have.property('stage', '$.stageVariables');
392+
expect(serverlessStepFunctions.serverless.service
393+
.provider.compiledCloudFormationTemplate.Resources.FirstStepFunctionsEventsRuleSchedule1
394+
.Properties.Targets[0].InputTransformer.InputTemplate).to.equal('{ "stage": <stage> }');
395+
});
396+
397+
it('should throw an error when Input and InputPath and InputTransformer are set', () => {
398+
serverlessStepFunctions.serverless.service.stepFunctions = {
399+
stateMachines: {
400+
first: {
401+
events: [
402+
{
403+
schedule: {
404+
rate: 'rate(10 minutes)',
405+
enabled: false,
406+
input: {
407+
key: 'value',
408+
},
409+
inputPath: '$.stageVariables',
410+
inputTransformer: {
411+
inputPathsMap: {
412+
stage: '$.stageVariables',
413+
},
414+
inputTemplate: '{ "stage": <stage> }',
415+
},
416+
},
417+
},
418+
],
419+
},
420+
},
421+
};
422+
423+
expect(() => serverlessStepFunctions.compileScheduledEvents()).to.throw(Error);
424+
});
363425
});
364426
});

lib/deploy/stepFunctions/compileIamRole.js

+2
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ function getIamPermissions(taskStates) {
433433
return getDynamoDBPermissions('dynamodb:GetItem', state);
434434
case 'arn:aws:states:::dynamodb:deleteItem':
435435
return getDynamoDBPermissions('dynamodb:DeleteItem', state);
436+
case 'arn:aws:states:::aws-sdk:dynamodb:updateTable':
437+
return getDynamoDBPermissions('dynamodb:UpdateTable', state);
436438

437439
case 'arn:aws:states:::batch:submitJob.sync':
438440
case 'arn:aws:states:::batch:submitJob':

lib/deploy/stepFunctions/compileIamRole.test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,17 @@ describe('#compileIamRole', () => {
549549
},
550550
End: true,
551551
},
552+
E: {
553+
Type: 'Task',
554+
Resource: 'arn:aws:states:::aws-sdk:dynamodb:updateTable',
555+
Parameters: {
556+
TableName: tableName,
557+
},
558+
End: true,
559+
},
552560
},
553561
},
554562
});
555-
556563
serverless.service.stepFunctions = {
557564
stateMachines: {
558565
myStateMachine1: genStateMachine('StateMachine1', helloTable, ['arn:aws:states:::dynamodb:updateItem', 'arn:aws:states:::dynamodb:putItem']),
@@ -577,6 +584,7 @@ describe('#compileIamRole', () => {
577584
'dynamodb:PutItem',
578585
'dynamodb:GetItem',
579586
'dynamodb:DeleteItem',
587+
'dynamodb:UpdateTable',
580588
]);
581589
});
582590

package-lock.json

+25-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)