【问题标题】:How should we configure apigateway--> step function proxy in AWS serverless framework?我们应该如何在 AWS 无服务器框架中配置 apigateway--> step function proxy?
【发布时间】:2021-11-16 20:43:56
【问题描述】:

我们正在使用无服务器框架 (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 连接到 step-function 而不使用插件?

【问题讨论】:

    标签: amazon-web-services aws-api-gateway serverless-framework


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 2020-08-21
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多