python-3.x 如何使用AWS CDK中的InvokeLambda将JSON传递给AWS StepFunction中的lambda

b5buobof  于 2022-12-20  发布在  Python
关注(0)|答案(3)|浏览(149)

我认为有效负载需要对象,但我找不到任何帮助,也找不到如何在AWS StepFunction中将JSON传递给Lambda

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)
        )

这是我合成时得到的错误

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

k4aesqcs1#

您可以像这样将json传递给LambdaInvoke

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"))
nzrxty8p

nzrxty8p2#

尝试使用json.stringify将其解析为json字符串。我正在使用json.dumps作为我的python实现,它做了同样的事情,并且它工作正常。

uxhixvfz

uxhixvfz3#

CDKv2 JsonPath为我工作:( typescript )

import { JsonPath } from 'aws-cdk-lib/aws-stepfunctions';

{
  value: JsonPath.jsonToString(JsonPath.objectAt("$.Foo.Bar")),
}

当部署到AWS Stepfunctions时:

"Value.$": "States.JsonToString($.Foo.Bar)"

相关问题