具有步骤功能的AWS API网关

时间:2017-06-17 13:32:49

标签: amazon-web-services aws-lambda aws-api-gateway aws-step-functions

我想要一个示例将AWS API Gateway与Step Function集成。我已阅读本教程Creating a Step Functions API Using API Gateway,但该教程需要我以

的格式发送请求
{    
"input": "{}",    
"name": "PostmanExecution",    
"stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:Custom" 
}

我想发送普通请求并仅在API网关中配置此 stateMachineArn ,以便客户端不需要发送此信息。

3 个答案:

答案 0 :(得分:8)

创建API网关资源和方法。然后在“方法执行”设置的“集成请求”中,使用以下设置:

  • 集成类型:AWS服务
  • AWS地区:您所在的地区
  • AWS服务:步骤功能
  • AWS子域名:您的子域名(如果有的话) - 我将其留空
  • HTTP方法:POST
  • 操作:StartExecution
  • 执行角色:需要成为StepFunction启动执行策略的角色,例如arn:aws:iam :: aws:policy / AWSStepFunctionsFullAccess
  • 凭据缓存:我将此保留为默认
  • 内容处理:直通

然后神奇。再下来,在Body Mapping Templates下:

  • 请求正文直通:从不
  • 添加映射模板:application / json

在模板文本框中向下:

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

这会将发布到API Gateway的json有效负载传递给步骤函数。

省略执行名称,以便每次调用API Gateway时都会创建一个新的执行。

答案 1 :(得分:1)

不是通过选择API网关集成类型" AWS服务"直接通过API网关公开您的步骤功能,而是创建一个Lambda函数,该函数调用您的步骤函数并使用Integration Type&通过API网关公开该Lambda函数#34; LAMBDA&#34 ;.然后,您可以将API网关和Lambda函数配置为接受所需的任何输入格式,并且可以对Lambda函数中的stateMacheArn值进行硬编码或将其设置为Lambda环境变量,以便API网关消费者不会这样做。需要了解有关ARN的任何信息。

答案 2 :(得分:1)

我设法使用API​​ Gateway公开了2个端点,一个端点开始执行,第二个端点检查执行状态。

无需为此操作使用lambda,也无需通过参数指示步进函数arn。

解决方案CloudFormation模板为:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: >
  The turtle calculator, a dummy slow calculator for comprehensive code example
Parameters:
  OpenAPIS3File:
    Description: '"openapi.yaml" file location'
    Default: ./openapi.yaml
    Type: String

Resources:
  Workflow:
    Type: AWS::StepFunctions::StateMachine
    Properties:
      StateMachineName: !Sub ${AWS::StackName}-state-machine
      RoleArn: !GetAtt StateExecutionRole.Arn
      DefinitionString: !Sub |
        {
          ...
        }

  Api:
    Type: AWS::Serverless::Api
    Properties:
      StageName: random-workflow
      Name: !Sub ${AWS::StackName}-api
      DefinitionBody:
        'Fn::Transform':
          Name: AWS::Include
          Parameters:
            Location: !Ref OpenAPIS3File

OpenAPI配置为:

openapi: 3.0.1
info:
  title: Api Mocker
  description: Automated testing application for TGR
  contact:
    name: Patagonia-IT
    url: http://www.patagonia-it.com
    email: contacto@patagonia-it.com
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0

x-amazon-apigateway-request-validators:
  params:
    validateRequestParameters: true
    validateRequestBody: false

x-amazon-apigateway-request-validator: params

paths:
  /workflow:
    post:
      x-amazon-apigateway-integration:
        credentials:
          Fn::GetAtt: [ ApiGatewayStepFunctionsRole, Arn ]
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/StartExecution
        httpMethod: POST
        type: aws
        responses:
          default:
            statusCode: 200
            responseTemplates:
              application/json: |
                '{ "executionId": "$input.json('executionArn').split(':').get(7) }'
        requestTemplates:
          application/json:
            Fn::Sub: |-
              {
                "input": "$util.escapeJavaScript($input.json('$'))",
                "name": "$context.requestId",
                "stateMachineArn": "${Workflow}"
              }
      summary: Start workflow instance
      responses:
        200:
          $ref: '#/components/responses/200Execution'
        403:
          $ref: '#/components/responses/Error'
  /workflow/{executionId}:
    get:
      x-amazon-apigateway-integration:
        credentials:
          Fn::GetAtt: [ ApiGatewayStepFunctionsRole, Arn ]
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/DescribeExecution
        httpMethod: POST
        type: aws
        responses:
          default:
            statusCode: 200
            responseTemplates:
              application/json: |-
                #set ($status = $input.json('status'))
                {
                #if($status == '"SUCCEEDED"')
                  "output": $util.parseJson($input.json('output')),
                #end
                  "status": $status
                }
        requestTemplates:
          application/json:
            Fn::Sub: |-
              {
                "executionArn": "arn:aws:states:${AWS::Region}:${AWS::AccountId}:execution:${Workflow.Name}:$input.params().path.get('executionId')"
              }
      summary: Workflow execution status
      responses:
        200:
          $ref: '#/components/responses/200ExecutionDetails'

我在https://github.com/jvillane/aws-sam-step-functions-lambda的github中提交了一个有效的示例