首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从API网关调用调用状态机

从API网关调用调用状态机
EN

Stack Overflow用户
提问于 2021-01-02 16:40:22
回答 1查看 468关注 0票数 1

目前,我很难成功地部署包含3个lambdas、一个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)

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

代码语言:javascript
复制
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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-02 17:01:20

好像这不起作用

代码语言:javascript
复制
   - StateMachineArn: !GetAtt [ SPIStateMachine, Arn ]

引用文档的话

Fn::GetAtt::GetAtt返回此类型的指定属性的值。以下是可用属性和示例返回值。当前AWS CloudFormation不支持Arn。

您可以使用Fn::构造ARN。

代码语言:javascript
复制
 Fn::Sub: arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:mystatemachine

编辑

所以uri应该是这样的

代码语言:javascript
复制
arn:aws:apigateway:{region}:{subdomain.service|service}:path|action/{service_api}

在step函数的情况下:

代码语言:javascript
复制
Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/StartExecution

这应该在arn的cloudformation模板中

代码语言:javascript
复制
    #set($input = $input.json('$'))
{
  "input": "$util.escapeJavaScript($input)",
  "stateMachineArn": "<STATE_MACHINE_ARN>"
}

这将传递发布到API的json有效负载以Step函数。

您可以为描述执行创建另一个URI

代码语言:javascript
复制
Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/DescribeExecution

为此,请求模板如下所示

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

正如我所描述的一样,工作原理是这里,您可以找到工作示例。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65541549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档