首页 > 解决方案 > AWS stepfunctions - 从 lambda 函数传递和读取变量

问题描述

我正在尝试将我的 lambda 函数的输出读取到我的步进函数中的一个变量中。lambdas 默认输出是

return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

我想返回的是这样的 json 对象

{
            "version": version,
            "bucket": bucket
        }

从 lambda 传递版本和存储桶名称的位置。在我的 step 函数中,我试图捕获它并将其插入到 s3 url 中,如下所示:

"S3Uri.$": "States.Format('s3://{}/path/to/script/{}/script.py',$.bucket, $.version)"

但是,我正在努力从 lambda 中获得正确的输出以及如何在阶跃函数中获取值。我试过了

return {
        'statusCode': 200,
        'body': json.dumps({
        "version": version,
        "bucket": bucket
    })       }

以及将 json 对象构造为正文的字符串的各种方法,例如

"{\"version\": \"" + version + "\",\"bucket\": \"" + bucket + "\"}"

但是我找不到合适的组合,而且工作一直失败。这是一个示例错误消息

字段 '$.bucket' 的 JsonPath 参数在输入 '{"statusCode": 200, "body": "{\"version\": \"v0-1\", \"bucket\ ": \"sagemaker-us-west-2-removed\"}"}'"

我应该如何构造 lambda 输出以及相应的阶跃函数变量以使值通过?同样,我希望 lambda 告诉 step 函数使用了哪个桶和版本,然后让 step 函数将这些值插入到 s3 url 字符串中。

编辑:这是其中一次尝试的完整错误消息

{
  "error": "States.Runtime",
  "cause": "An error occurred while executing the state 'Postproc' (entered at the event id #38). The function 'States.Format('s3://{}/AAPM/AAPM_2207/prod/{}/meta/scripts/preproc/aapm-postproc.py',$.bucket, $.version)' had the following error: The JsonPath argument for the field '$.bucket' could not be found in the input '{\"statusCode\": 200, \"body\": \"{\\\"version\\\": \\\"v0-1\\\", \\\"bucket\\\": \\\"sagemaker-us-west-2-removed\\\"}\"}'"
}

标签: jsonamazon-web-servicesamazon-s3aws-lambdaaws-step-functions

解决方案


在这个答案中,诀窍就是摆脱 json 转储。

return {
        'statusCode': 200,
        'body': {
        "version": version,
        "bucket": bucket
                 }       
        }

我可以用 $.bucket, $.version 很好地访问它们


推荐阅读