AWS API Gateway with Step Function

我怕爱的太早我们不能终老 提交于 2019-12-09 01:52:32

问题


I want a sample to integrate AWS API Gateway with Step Function. I have read this tutorial Creating a Step Functions API Using API Gateway but that tutorial needs me to send request in format of

{    
"input": "{}",    
"name": "PostmanExecution",    
"stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:Custom" 
}

I want to send normal request and configure this stateMachineArn in API Gateway only, so that clients dont need to send this.


回答1:


Create your API Gateway resource and method. Then in the "Method Execution" settings, in the Integration Request, use these settings:

  • Integration type: AWS Service
  • AWS Region: your region
  • AWS Service: Step Functions
  • AWS Subdomain: your subdomain if you have one - I left it blank
  • HTTP method: POST
  • Action: StartExecution
  • Execution role: needs to be a role with StepFunction start execute policy, such as arn:aws:iam::aws:policy/AWSStepFunctionsFullAccess
  • Credentials cache: I left this as default
  • Content Handling: Passthrough

Then the magic. Futher down, under Body Mapping Templates:

  • Request body passthrough: Never
  • Add mapping template : application/json

Futher down in the template text box:

#set($input = $input.json('$'))
{
   "input": "$util.escapeJavaScript($input)",
   "stateMachineArn": "arn:aws:states:eu-west-1:123456789012:stateMachine:yourStepFunctionName"
}

This will pass the json payload posted to API Gateway through to the Step Function.

Omit the execution name so that each call to API Gateway creates a new execution.




回答2:


Instead of directly exposing your step function via API Gateway by choosing API Gateway Integration Type "AWS Service", create a Lambda function that calls your Step Function and expose that Lambda function via API Gateway using Integration Type "Lambda". Then you can configure your API Gateway and Lambda function to accept whatever input format that you desire and you could hard-code the stateMacheArn value in your Lambda function or set it as a Lambda environment variable, so that the API Gateway consumer doesn't need to know anything about the ARN.



来源:https://stackoverflow.com/questions/44605228/aws-api-gateway-with-step-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!