Skip to content

Commit

Permalink
fix(stepfunctions-tasks): cannot specify part of execution data or ta…
Browse files Browse the repository at this point in the history
…sk context as input to the service integration

The Lambda service integration requires an input field called `payload`.
We modeled this as a `{[key: string]: any}` which precludes the usage of
execution data or context data as inputs to a Lambda function.

Fix:
Change the type of `payload` to be `TaskInput` which has already modeled
a type union for task classes that accept multiple types of payload.
This will enable usage of literal strings, objects, execution data, and
task context.

Rationale:
Although this is a breaking change, the workarounds for enabling usage of
different types is not user friendly and incomplete as all of the types
above cannot be expressed in the current modeling of `payload`

Fixes #7371

BREAKING CHANGE:
`payload` in RunLambdaTask is now of type `TaskInput`.
You can migrate your current assignment to payload by supplying it to the `TaskInput.fromObject()` API
  • Loading branch information
shivlaks committed Apr 18, 2020
1 parent df2fc4b commit 714c023
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface RunLambdaTaskProps {
*
* @default - No payload
*/
readonly payload?: { [key: string]: any };
readonly payload?: sfn.TaskInput;

/**
* The service integration pattern indicates different ways to invoke Lambda function.
Expand Down Expand Up @@ -92,7 +92,7 @@ export class RunLambdaTask implements sfn.IStepFunctionsTask {
metricDimensions: { LambdaFunctionArn: this.lambdaFunction.functionArn },
parameters: {
FunctionName: this.lambdaFunction.functionName,
Payload: this.props.payload,
Payload: this.props.payload?.value,
InvocationType: this.props.invocationType,
ClientContext: this.props.clientContext,
Qualifier: this.props.qualifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const callbackHandler = new Function(stack, 'CallbackHandler', {
const taskTokenHandler = new sfn.Task(stack, 'Invoke Handler with task token', {
task: new tasks.RunLambdaTask(callbackHandler, {
integrationPattern: sfn.ServiceIntegrationPattern.WAIT_FOR_TASK_TOKEN,
payload: {
payload: sfn.TaskInput.fromObject({
token: sfn.Context.taskToken
}
})
}),
inputPath: '$.guid',
resultPath: '$.status',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"Resources": {
"submitJobLambdaServiceRole4D897ABD": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"submitJobLambdaEFB00F3C": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = async () => {\n return {\n statusCode: '200',\n body: 'hello, world!'\n };\n };"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"submitJobLambdaServiceRole4D897ABD",
"Arn"
]
},
"Runtime": "nodejs10.x"
},
"DependsOn": [
"submitJobLambdaServiceRole4D897ABD"
]
},
"checkJobStateLambdaServiceRoleB8B57B65": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"checkJobStateLambda4618B7B7": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = async function(event, context) {\n return {\n jobState: event.statusCode === 200 ? 'SUCCEEDED' : 'FAILED';\n };\n };"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"checkJobStateLambdaServiceRoleB8B57B65",
"Arn"
]
},
"Runtime": "nodejs10.x"
},
"DependsOn": [
"checkJobStateLambdaServiceRoleB8B57B65"
]
},
"StateMachineRoleB840431D": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": {
"Fn::Join": [
"",
[
"states.",
{
"Ref": "AWS::Region"
},
".amazonaws.com"
]
]
}
}
}
],
"Version": "2012-10-17"
}
}
},
"StateMachineRoleDefaultPolicyDF1E6607": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "lambda:InvokeFunction",
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"submitJobLambdaEFB00F3C",
"Arn"
]
}
},
{
"Action": "lambda:InvokeFunction",
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"checkJobStateLambda4618B7B7",
"Arn"
]
}
}
],
"Version": "2012-10-17"
},
"PolicyName": "StateMachineRoleDefaultPolicyDF1E6607",
"Roles": [
{
"Ref": "StateMachineRoleB840431D"
}
]
}
},
"StateMachine2E01A3A5": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {
"DefinitionString": {
"Fn::Join": [
"",
[
"{\"StartAt\":\"Invoke Handler\",\"States\":{\"Invoke Handler\":{\"Next\":\"Invoke Handler with task token\",\"Parameters\":{\"FunctionName\":\"",
{
"Ref": "submitJobLambdaEFB00F3C"
},
"\"},\"Type\":\"Task\",\"Resource\":\"arn:",
{
"Ref": "AWS::Partition"
},
":states:::lambda:invoke\"},\"Invoke Handler with task token\":{\"Next\":\"Job Complete?\",\"InputPath\":\"$.Payload\",\"Parameters\":{\"FunctionName\":\"",
{
"Ref": "checkJobStateLambda4618B7B7"
},
"\",\"Payload.$\":\"$\"},\"Type\":\"Task\",\"Resource\":\"arn:",
{
"Ref": "AWS::Partition"
},
":states:::lambda:invoke\",\"ResultPath\":\"$.status\"},\"Job Complete?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.status\",\"StringEquals\":\"FAILED\",\"Next\":\"Job Failed\"},{\"Variable\":\"$.status\",\"StringEquals\":\"SUCCEEDED\",\"Next\":\"Final step\"}]},\"Job Failed\":{\"Type\":\"Fail\",\"Error\":\"Received a status that was not 200\",\"Cause\":\"Job Failed\"},\"Final step\":{\"Type\":\"Pass\",\"End\":true}},\"TimeoutSeconds\":30}"
]
]
},
"RoleArn": {
"Fn::GetAtt": [
"StateMachineRoleB840431D",
"Arn"
]
}
},
"DependsOn": [
"StateMachineRoleDefaultPolicyDF1E6607",
"StateMachineRoleB840431D"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as cdk from '@aws-cdk/core';
import * as tasks from '../../lib';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-stepfunctions-tasks-run-lambda-integ');

const submitJobLambda = new Function(stack, 'submitJobLambda', {
code: Code.fromInline(`exports.handler = async () => {
return {
statusCode: '200',
body: 'hello, world!'
};
};`),
runtime: Runtime.NODEJS_10_X,
handler: 'index.handler',
});

const submitJob = new sfn.Task(stack, 'Invoke Handler', {
task: new tasks.RunLambdaTask(submitJobLambda),
outputPath: '$.Payload',
});

const checkJobStateLambda = new Function(stack, 'checkJobStateLambda', {
code: Code.fromInline(`exports.handler = async function(event, context) {
return {
status: event.statusCode === '200' ? 'SUCCEEDED' : 'FAILED';
};
};`),
runtime: Runtime.NODEJS_10_X,
handler: 'index.handler',
});

const checkJobState = new sfn.Task(stack, 'Check the job state', {
task: new tasks.RunLambdaTask(checkJobStateLambda, {
payload: sfn.TaskInput.fromDataAt('$.Payload'),
}),
outputPath: '$.Payload'
});

const isComplete = new sfn.Choice(stack, 'Job Complete?');
const jobFailed = new sfn.Fail(stack, 'Job Failed', {
cause: 'Job Failed',
error: 'Received a status that was not 200',
});
const finalStatus = new sfn.Pass(stack, 'Final step');

const chain = sfn.Chain.start(submitJob)
.next(checkJobState)
.next(
isComplete
.when(sfn.Condition.stringEquals('$.status', 'FAILED'), jobFailed)
.when(sfn.Condition.stringEquals('$.status', 'SUCCEEDED'), finalStatus)
);

new sfn.StateMachine(stack, 'StateMachine', {
definition: chain,
timeout: cdk.Duration.seconds(30),
});

app.synth();
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ beforeEach(() => {
test('Invoke lambda with default magic ARN', () => {
const task = new sfn.Task(stack, 'Task', {
task: new tasks.RunLambdaTask(fn, {
payload: {
payload: sfn.TaskInput.fromObject({
foo: 'bar'
},
}),
invocationType: tasks.InvocationType.REQUEST_RESPONSE,
clientContext: 'eyJoZWxsbyI6IndvcmxkIn0=',
qualifier: '1',
Expand Down Expand Up @@ -63,9 +63,9 @@ test('Lambda function can be used in a Task with Task Token', () => {
const task = new sfn.Task(stack, 'Task', {
task: new tasks.RunLambdaTask(fn, {
integrationPattern: sfn.ServiceIntegrationPattern.WAIT_FOR_TASK_TOKEN,
payload: {
payload: sfn.TaskInput.fromObject({
token: sfn.Context.taskToken
}
})
})
});
new sfn.StateMachine(stack, 'SM', {
Expand Down

0 comments on commit 714c023

Please sign in to comment.