AWS Serverless框架中的apigateway--> step function proxy应该怎么配置?

How should we configure apigateway--> step function proxy in AWS serverless framework?

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

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

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 连接到 step-function 而无需 使用插件?

要将 AWS API 网关集成配置为步骤函数,您必须执行以下步骤:

  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  
      .....
      .....
      .....