首页 > 解决方案 > 我们应该如何在 AWS 无服务器框架中配置 apigateway--> step function proxy?

问题描述

我们正在使用无服务器框架 ( AWS ) 创建:

这是使用插件的当前代码:serverless-step-functions

stepFunctions:
  stateMachines:
    fetchAndCombineDataStateMachine:
      type: EXPRESS
      role: ...  
      events:
        - http:
            path: /
            method: POST 
            action: StartSyncExecution 
            request:
              template:
                application/json: |
                  #set( $body = $util.escapeJavaScript($input.json('$')) )
                  #set( $stepFunctionName = '*************************-${opt:stage}') 
                  {
                    "input": "$body", 
                    "stateMachineArn":"*************************:stateMachine:$stepFunctionName"
                  }

但是,在我们的 CICD 管道中,我们不能使用这个插件。所以我们必须在apigateway--> step function proxy没有插件的情况下配置()它。

问题:

我们应该如何配置 YML 文件以允许在使用插件的情况下Apigateway连接到 a ?step-function

标签: amazon-web-servicesaws-api-gatewayserverless-framework

解决方案


要将 AWS API Gateway 集成配置为步进功能,您必须执行以下步骤:

  1. 配置Api网关资源
    StepFunctionApiProxy:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: YourApiName
        EndpointConfiguration:
          Types:
            - REGIONAL
        Policy:
          Version: '2012-10-17'
          Statement:
          - Effect: Deny
            Principal: "*"
            Action:
            - execute-api:Invoke
            Resource: execute-api:/*/*/*
            Condition:
              NotIpAddress:
                aws:sourceIp:
                - Whitelisted Ip address
                - Whitelisted Ip address
          - Effect: Allow
            Principal: "*"
            Action:
            - execute-api:Invoke
            Resource: execute-api:/*/*/*   
  1. 配置部署资源(它会将阶段附加到您的 API 部署)
    ApiGatewayDeployment:
      Type: AWS::ApiGateway::Deployment
      Properties:
        RestApiId:
          Ref: StepFunctionApiProxy
        StageName: Int
      DependsOn:
      - ApiGatewayMethodPost 
  1. 配置 API 方法(请求、响应和转换模板)
    ApiGatewayMethodPost:
      Type: AWS::ApiGateway::Method
      Properties:
        HttpMethod: POST 
        AuthorizationType: NONE
        ApiKeyRequired: false
        ResourceId:
          Fn::GetAtt:
          - StepFunctionApiProxy
          - RootResourceId
        RestApiId:
          Ref: StepFunctionApiProxy
        Integration:
          IntegrationHttpMethod: POST 
          Type: AWS
          Credentials: [ApiGatewayRole, should have permission to run step function]
          Uri:
            Fn::Join:
            - ''
            - - 'arn:'
              - Ref: AWS::Partition
              - ":apigateway:"
              - Ref: AWS::Region
              - ":states:action/StartSyncExecution"  [Here you can specify action: StartSyncExecution - wait for result]                
          PassthroughBehavior: WHEN_NO_TEMPLATES 
          RequestTemplates:
            application/json: |-
              #set( $body = $util.escapeJavaScript($input.json('$')) )
              #set( $stepFunctionName = '[YourStepFunctionName]') 
              {
                "input": "$body", 
                "stateMachineArn":"arn:aws:states:eu-west-1:[YourAmazonAccountId]:stateMachine:[stepFunctionName]"
              } 
            application/x-www-form-urlencoded: |-
              #set( $body = $util.escapeJavaScript($input.json('$')) )
              #set( $stepFunctionName = '[YourStepFunctionName]') 
              {
                "input": "$body", 
                "stateMachineArn":"arn:aws:states:eu-west-1:YourAmazonAccountId:stateMachine:[stepFunctionName]"
              }
          IntegrationResponses:
          - StatusCode: 200
            SelectionPattern: 200
            ResponseParameters: {}
            ResponseTemplates:
              application/json: |
                #set($inputJSON = $input.json('$'))
                #set($isSuccess = !$inputJSON.toString().contains("error") && !$inputJSON.toString().contains("Exception")) 
                #set($xField = ($t.substring($pos1, $pos2)))   
                {
                  "payload": { 
                      "services": $util.parseJson($inputJSON).output 
                  },      
                  "requestId": "$util.parseJson($util.parseJson($inputJSON).input).requestId",
                  "isSuccess":$isSuccess,
                  "xField":"$xField" 
                  #if($inputJSON.toString().contains("error") || $inputJSON.toString().contains("Exception"))
                  ,"error": {
                      "message": "$util.parseJson($inputJSON).error"
                  }
                  #end 
                }
          - StatusCode: 400
            SelectionPattern: 400
            ResponseParameters: {}
            ResponseTemplates: {}
        MethodResponses:
        - ResponseParameters: {}
          ResponseModels: {}
          StatusCode: 200
        - ResponseParameters: {}
          ResponseModels: {}
          StatusCode: 400     
  1. 配置步进功能
    fetchAndCombineMapDataStateMachine:
      Type: AWS::StepFunctions::StateMachine  
      .....
      .....
      .....


推荐阅读