How do I pass JSON to lambda in AWS StepFunction using InvokeLambda in AWS CDK

Viewed 769

I think payload expects object but I cant find any help and how to pass JSON to Lambda in AWS StepFunction

create_group = tasks.LambdaInvoke(
            self,
            "CreateGroup",
            lambda_function=notification_lambda,
            payload={
                "Input.$": "$",
                "Stage": "CreateGroup",
                "token.$": "$$.Task.Token",
                "callback": "true"
            },
            result_path='$',
            timeout=Duration.seconds(300)
        )

This is the error I am getting when I synth

jsii.errors.JavaScriptError: 
  Error: Expected object reference, got {"$jsii.map":{"Input.$":"$","Stage":"CreateGroup","token.$":"$$.Task.Token","callback":"true"}}
2 Answers

You can pass json to LambdaInvoke like this

from aws_cdk import (
    core,
    aws_stepfunctions_tasks as tasks,
    aws_stepfunctions as sf
)

hello_task: tasks.LambdaInvoke = tasks.LambdaInvoke(
    self,
    "[Lambda] hello",
    lambda_function=hello_lambda,
    result_path="$.helloLambda")

sample_task: tasks.LambdaInvoke = tasks.LambdaInvoke(
    self,"[Lambda] world",
    lambda_function=world_lambda,
    payload=sf.TaskInput.from_json_path_at("$.helloLambda.Payload"))

Try using JSON.stringify to parse it into a json string. I am using json.dumps for my python implementation which does the same thing and it works.

Related