How do I use JSON Lambda output in Step Functions
I have a (Python) Lambda function which returns a JSON object. I'd like to join that object on to my Step Functions state, and access its sub-fields in the next state... Something like:
# Lambda Function
def handler(event, context):
return json.dumps({ "ThingARN": "...", "AnotherField": "..." })
{
"StartAt": "LambdaStuff",
"States": {
"LambdaStuff": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "MyCoolLambdaFunction",
"Payload.$": "$"
},
"ResultPath": "$.LambdaResult",
"Next": "NextActivity"
},
"NextActivity": {
"Type": "Task",
"Resource": "...",
"Parameters": {
"ARN.$": "$.LambdaResult.ThingARN"
},
"ResultPath": "$.AnotherResult",
"End": true
}
}
}
...But my state machine is failing (cancelling the NextActivity
task) because what gets populated to $.LambdaResult
is a big object with Lambda metadata and the stringified result payload - like this:
{
"ExecutedVersion": "$LATEST",
"Payload": "{\"ThingARN\": \"...\", \"AnotherField\": \"...\"}",
"SdkHttpMetadata": {
"HttpHeaders": { ... }, // All kinds'a stuff
"HttpStatusCode": 200
},
"SdkResponseMetadata": {
"RequestId": "..."
},
"StatusCode": 200
}
This docs page seems to imply that what I'm trying to do is possible, but the example is clearly very simplified/stripped-down.
All the other worked examples I've found (e.g. "InputPath, OutputPath and ResultPath Example") just seem to have Lambdas returning simple strings, rather than structured objects.
I guess I'm missing something obvious?
If you invoke your lambda function from SFN using the full function ARN in the Resource attribute, you will get back only what was returned from the Lambda function. Also, you do not want to json.dump the object that you return. Just return a map and that will be included in your output.
For instance, the following function:
def lambda_handler(event, context):
# TODO implement
body = {
'Test': 'abc',
'Value': 123
}
return body
When invoked like this:
"Hello": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-1:xxxxxxxxx:function:HelloWorld:$LATEST",
"ResultPath": "$.LambdaResult",
"Next": "World"
},
With the following input:
{
"Input": "My input"
}
returned the following:
{
"Input": "My input",
"LambdaResult": {
"Test": "abc",
"Value": 123
}
}
Relevant questions
How do I use Step Functions to create EMR clusters with different specifications?
Accepted Answerasked 2 years agoaws step functions dynamodb service integration & updating dynamic items
Accepted Answerasked 5 months agoCustomise emitted SNS message in state machine
Accepted Answerasked 6 days agoCan I specify GET URL path parameter in step function?
asked 2 months agoS3 object and lambda function in step machine with the asynchronous express workflows
Accepted Answerasked 4 months agoHow to pass float numbers to Lambda event object
Accepted Answerasked 2 months agoAWS StepFunctions: sum() function returns error about not finding path
asked 2 months agohow to trigger a step function from a s3 object notification?
asked a month agoCoordinating Step functions from App Flow -> Event Bridge -> DynamoDB
asked 4 months agoHow do I use JSON Lambda output in Step Functions
Accepted Answer