gpt4 book ai didi

amazon-web-services - 从 API 网关调用调用状态机

转载 作者:行者123 更新时间:2023-12-03 07:18:46 26 4
gpt4 key购买 nike

目前,我很难成功部署包含 3 个 lambda、一个 API 网关和一个状态机的 CloudFormation Stack。我对尝试调试这个问题束手无策,并且希望有第二双眼睛。当尝试构建 AWS::ApiGateway::Method 时,部署失败并返回:

apiGatewayRootMethod:进入状态 CREATE_FAILED,原因:请求中指定的 ARN 无效(服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求 ID:a2ba00f1-179b-400f-a42a-f3a4e90231af;代理:空)

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

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:输入状态CREATE_FAILED,原因:请求中指定的ARN无效(服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求ID:9585e311-69e1-4809-bc3c -f59a67a4546e;代理:空)

最佳答案

这个好像不行

   - StateMachineArn: !GetAtt [ SPIStateMachine, Arn ]

引用自documentation

Fn::GetAttFn::GetAtt returns a value for a specified attribute of this type. The following are the available attributes and sample return values.

ArnNot currently supported by 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您可以找到工作示例。

关于amazon-web-services - 从 API 网关调用调用状态机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65541549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com