Search code examples
jsonamazon-web-servicesamazon-s3aws-lambdaaws-step-functions

AWS stepfunctions - passing and reading variables from lambda function


I am trying to read output from my lambda function into a variable in my step function. The lambdas default output is

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

What I would like to return is a json object like this

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

where the version and bucket name gets passed from the lambda. in my step function, I am trying to capture this and insert it into an s3 url like this:

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

However, I am struggling with having the correct output from the lambda and how to grab the value in step functions. I've tried

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

And various ways of constructing the json object as a string to the body, eg

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

But I can't find the right combo, and the job keeps failing. Here is an example error message

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\"}"}'"

How should I construct the lambda output, and the corresponding step function variable to have the values pass through? Again, I want the lambda to tell step functions what bucket and version was used, and then have step functions insert these values into an s3 url string.

EDIT: here is the full error message for one of the attempts

{
  "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\\\"}\"}'"
}

Solution

  • As in this answer the trick was just to get rid of json dumps.

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

    and I could access them fine with $.bucket, $.version