简体   繁体   中英

How do I invoke a step function using AWS Lambda ( Python)

I am trying to create a simple step function comprising of two lambda functions. The first lambda function takes two numbers as input from the user (say num1=2, num2 =5), and passes on the sum of the two numbers to the second lambda function. The second lambda function multiplies the output of the first lambda function with a constant (say 10).

I want to be able to get the final answer (which is (2+5)*10 = 70) in this case) as the output of an API invocation.

I am able to execute the Step Function successfully (from the AWS Step Function Console). But the output I get when I invoke the API (integrated with the Step Function) is not a number but as follows:

{
    "executionArn": "arn:aws:states:ap-south-1:123456789012:execution:Test_Math:xxx",
    "startDate": 1560344276.117
}

How do I get the API call to return the answer (which is the number 70 in this case)?

I have gone already gone through the AWS Documentation and AWS API Gateway with Step Function this question but I am not still not clear.

  1. How can I make a new lambda (or use any of the above two) function which invokes/executes this step function to return the answer?
  2. Are there any other ways to return the answer of a step function via an API call?

My question is somewhat similar to this one Api gateway get output results from step function? . I tried adding a second method in API Gateway which will call the Step Function with the DescribeExecution action but it did not work.

The mapping template (application/json) for the POST Method with StartExecution action is as follows:

#set($inputRoot = $input.path('$')) 

#set($data = $util.escapeJavaScript($input.json('$')))

{
  "input": "{ \"num1\": $inputRoot.num1, \"num2\": $inputRoot.num2 }",

  "stateMachineArn": "arn:aws:states:ap-south 1:998338******:stateMachine:Test_Math"
}

I created a new GET Method with DescribeExecution action with the following Integration Request details:

Integration type: AWS Service
AWS Service: Step Functions
HTTP method: GET
Action: Describe Execution
Content Handling: Passthrough

Mapping template : application/json

#set($inputRoot = $input.path('$'))

#set($data = $util.escapeJavaScript($input.json('$')))

{
   "executionArn": "arn:aws:states:ap-south-1:998338321653:execution:Test_Math:3981114a-da51-411d-9533-8571dc976e2d",

  "input": "{ \"num1\": $inputRoot.num1, \"num2\": $inputRoot.num2 }"
}
  1. Please let me know what changes do I need to make in the above to be able to return the answer (70) with an API call.

PS: I want to deploy my machine learning model using step functions and this is just a simple test which I was trying out.

you can use describe_execution method to fetch the final result of your stepfunction. you have to pass the execution arn as an input to this method.

Find more details here, https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/stepfunctions.html#SFN.Client.describe_execution

Below python code will print the output given the execution arn.

import boto3

client = boto3.client('stepfunctions')
executionArn = ''

response = client.describe_execution(
    executionArn=executionArn
)
#print(response)
print(response.get('output'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM