0

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\\\"}\"}'"
}
2
  • what data types are version and bucket ?. The docu states that If the handler returns objects that can't be serialized by json.dumps, the runtime returns an error. Commented Sep 10, 2020 at 19:14
  • they are strings. Updating with the error message Commented Sep 10, 2020 at 19:17

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

This my first ever stackoverflow post, but another thing you can try is using a ResultSelector on your task state that invokes your lambda, and use the intrinsic function States.StringToJson to un-stringify your lambda response.

ref: https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html#asl-intrsc-func-json-manipulate

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.