微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 API 网关调用调用状态机

如何解决从 API 网关调用调用状态机

目前,我很难成功部署包含 3 个 lambda、API 网关和状态机的 CloudFormation 堆栈。我在尝试调试这个问题时不知所措,并且会喜欢第二组眼睛。部署在尝试构建 AWS::ApiGateway::Method 时失败并返回:

apiGatewayRootMethod: entered status CREATE_Failed,reason: Invalid ARN specified in the request (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: a2ba00f1-179b-400f-a42a-f3a4e90231af; Proxy: null)

以下是用于部署的模板片段,

apiGatewayRootMethod:
    Type: "AWS::ApiGateway::Method"
    Properties:
      AuthorizationType: "NONE"
      HttpMethod: "POST"
      Integration:
        CacheKeyParameters:
          - 'method.request.path.proxy'      
        Credentials: !Ref 'apiGatewayRole'
        IntegrationHttpMethod: "POST"
        PassthroughBehavior: "NEVER"
        RequestParameters:
          integration.request.path.proxy: 'method.request.path.proxy'   
        RequestTemplates:
          application/json: !Sub
            - "{\"input\": \"$util.escapeJavaScript($input.json('$'))\",\"stateMachineArn\": \"${SPIStateMachine.Arn}\"}"
            - StateMachineArn: !GetAtt [ SPIStateMachine,Arn ]
        Type: "AWS"
        Uri: !Sub
          "arn:aws:apigateway:${AWS::Region}:states:action/StartExecution"         
      ResourceId: !GetAtt "apiGateway.RootResourceId"
      RequestParameters:
        method.request.path.proxy: true    
      RestApiId: !Ref "apiGateway"



SPIStateMachine:
    Type: "AWS::StepFunctions::StateMachine"
    Properties:
      StateMachineName: !Sub ${AWS::StackName}-state-machine
      RoleArn: !GetAtt [ StatesExecutionRole,Arn ]
      DeFinitionString: 
        Fn::Sub: 
          |-
            {
                "Comment": "Calling Step Functions from Lambda SQS","StartAt": "APIGatewayLambda","States": {
                    "APIGatewayLambda": {            
                        "Type": "Task","Resource": "${SPIApiFunction.Arn}","Catch": [
                            {
                                "ErrorEquals": ["CustomError"],"Next": "PythonLambda"
                            }
                        ],"End": false
                    },"PythonLambda": {
                        "Type": "Task","Comment": "This is the mandatory lambda which will process the information and possible call the PowerShell script","Resource": "${SPILambdaFunctionOne.Arn}","Next": "Needs Additional Language?"  
                    },"Needs Additional Language?": {
                        "Type": "Choice","Choices": [
                            {
                                "Variable": "$.platform","StringEquals": "powerbi","Next": "PowerShellLambda"
                            },{
                                "Not": {
                                    "Variable": "$.platform","StringEquals": "powerbi"
                                },"Next": "Passthrough"
                            }
                        ]
                    },"PowerShellLambda": {
                        "Type": "Task","Comment": "This is the PowerShell Lambda,to be run for PowerBI updates","Resource": "${SPILambdaFunctionTwo.Arn}","End": true
                    },"Passthrough": {
                        "Type": "Pass","End": true
                    }    
                }
            }

编辑:

application/json: !Sub

- "{\"input\": \"$util.escapeJavaScript($input.json('$'))\",\"stateMachineArn\": \"${StateMachineArn}\"}"

- StateMachineArn: !Sub arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:SPIStateMachine

这会导致相同的 apiGatewayRootMethod: entered status CREATE_Failed,reason: Invalid ARN specified in the request (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 9585e311-69e1-4809-bc3c-f59a67a4546e; Proxy: null)

解决方法

这个好像不行

   - StateMachineArn: !GetAtt [ SPIStateMachine,Arn ]

引用自 documentation

Fn::GetAtt Fn::GetAtt 返回此类型的指定属性的值。以下是可用的属性和示例返回值。

阿恩 AWS CloudFormation 目前不支持。

您可以使用 Fn::Sub

构建 ARN
 Fn::Sub: arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:mystatemachine

编辑

所以 uri 应该是这种形式

arn:aws:apigateway:{region}:{subdomain.service|service}:path|action/{service_api}

在阶跃函数的情况下翻译:

Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/StartExecution

这应该在 arn 的 cloudformation 模板中

    #set($input = $input.json('$'))
{
  "input": "$util.escapeJavaScript($input)","stateMachineArn": "<STATE_MACHINE_ARN>"
}

这会将发布到 API Gateway 的 json 负载传递给 Step Function。

您可以为 describing the execution 创建另一个 URI

Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/DescribeExecution

为此,请求模板如下

    requestTemplates:
      application/json:
        Fn::Sub: |-
          {
            "executionArn": "arn:aws:states:${AWS::Region}:${AWS::AccountId}:execution:${Workflow.Name}:$input.params().path.get('executionId')"
          }

正如我所描述的一样,here 您可以找到工作示例。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。