Bruno
Bruno

Reputation: 497

Step Functions Passing Parameters down to lambdas

I am building a State Machine that launches EC2 instances. Afterwards, I am manipulating the EC2 instance but to do so I need the newly created EC2 instances InstanceId which is returned in the Lambda function. How can I get the return value from my first Lambda to be passed to following Lambda functions?

I have tried "InputPath", "OutputPath", & "ResultPath" but every time I attempt it cancels the Lambda function. Not sure why this is (maybe fixing this would fix my problem).

{
   "Comment": "My State Machine",
   "StartAt": "Launch Instance",
   "States": {
     "Launch Instance": {
     "Type": "Task",
     "Resource": "Lambda",
     "Parameters": {
      "FunctionName": "My lambda",
      "Payload": {
        "Input": {
           "ImageId": "My Image"
         }
       }
     },
     "Next": "wait_ten_seconds"
       },
       "wait_ten_seconds": {
     "Type": "Wait",
     "Seconds": 10,
     "Next": "Create Image"
   },
     "Create Image":{
       "Type": "Task",
       "Resource": "Lambda",
       "Parameters":{
       "FunctionName": "My Lambda"
     },
       "Next": "Terminate Instance"
     },
      "Terminate Instance": {
        "Type": "Task",
        "Resource": "Lambda",
        "Parameters": {
          "FunctionName": "My lambda"
        },
         "End": true
       }
     }
   }

It works if I hard-code the payload in but (obviously) hard-coding is not the goal. Any help is appreciated, thank you.

Upvotes: 1

Views: 4680

Answers (1)

CH Liu
CH Liu

Reputation: 1874

If you don’t specify ResultPath, every task output will overwrite the previous input. Therefore, given that Launch Instance returns {"instance_id": "xxx"}, a possible strategy could be:

  1. In Luanch Instance, "ResultPath": "$.launch_instance.output"
  2. In Create Image, "ResultPath": "$.create_image.output"
  3. In Terminate Instance, "InputPath": "$.launch_instance.output"

Now you can get the value using event["instance_id"]

Upvotes: 3

Related Questions