aws-cloudformation - AWS CloudFormation。从 API Gateway v1 同步调用 Step Function

标签 aws-cloudformation aws-api-gateway aws-step-functions

我正在尝试通过 API Gateway 同步执行 AWS Step Function。问题是,对于 API Gateway V1,我必须使用 OpenAPI 语法(即 swagger)来指定 integrationSubtype 参数,但有些东西不起作用。这是我正在使用的 CloudFormation 模板:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "restApiName": {
            "Type": "String",
            "Default": "stepApi"
        }
    },
    "Resources": {
        "MyStepFunction": {
            "Type": "AWS::StepFunctions::StateMachine",
            "Properties": {
                "StateMachineName": "HelloWorld-StateMachine",
                "StateMachineType": "EXPRESS",
                "DefinitionString": "{\"Comment\": \"A Hello World example of the Amazon States Language using Pass states\", \"StartAt\": \"Hello\", \"States\": {\"Hello\": { \"Type\": \"Pass\", \"Result\": \"Hello\", \"Next\": \"World\" }, \"World\": { \"Type\": \"Pass\", \"Result\": \"World\", \"End\": true } } }",
                "RoleArn": {
                    "Fn::GetAtt": [
                        "StepFunctionRole",
                        "Arn"
                    ]
                }
            }
        },
        "StepFuncGateway": {
            "Type": "AWS::ApiGateway::RestApi",
            "Properties": {
                "Name": {
                    "Ref": "restApiName"
                },
                "Body": {
                    "openapi": "3.0.1",
                    "info": {
                        "title": "processFormExample",
                        "version": "2020-11-06 15:32:29UTC"
                    },
                    "paths": {
                        "/step": {
                            "post": { 
                                "responses": {
                                    "200": {
                                      "description": "Pet updated.",
                                      "content": {
                                        "application/json": {},
                                        "application/xml": {}
                                      }
                                    },
                                    "405": {
                                        "description": "Method Not Allowed",
                                        "content": {
                                            "application/json": {},
                                            "application/xml": {}
                                        }
                                    }
                                },
                                "parameters": [
                                ],
                                "x-amazon-apigateway-integration": {
                                    "integrationSubtype": "StepFunctions-StartSyncExecution",
                                    "credentials": {
                                        "Fn::GetAtt": [
                                            "APIGatewayRole", 
                                            "Arn"
                                        ]
                                    },
                                    "RequestTemplates": {
                                        "application/json": {
                                            "Fn::Join": [
                                                "",
                                                [
                                                    "#set( $body = $util.escapeJavaScript($input.json('$')) ) \n\n{\"input\": \"$body\",\"name\": \"$context.requestId\",\"stateMachineArn\":\"",
                                                    {
                                                        "Ref": "MyStepFunction"
                                                    },
                                                    "\"}"
                                                ]
                                            ]
                                        }
                                    },
                                    "httpMethod": "POST",
                                    "payloadFormatVersion": "1.0",
                                    "passthroughBehavior": "NEVER",
                                    "type": "AWS_PROXY",
                                    "connectionType": "INTERNET"
                                }
                            }
                        }
                    },
                    "x-amazon-apigateway-cors": {
                        "allowMethods": [
                            "*"
                        ],
                        "maxAge": 0,
                        "allowCredentials": false,
                        "allowOrigins": [
                            "*"
                        ]
                    }
                }
            },
            "DependsOn": [
                "APIGatewayRole",
                "MyStepFunction"
            ]
        },
        "APIGatewayRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "apigateway.amazonaws.com"
                                ]
                            },
                            "Action": "sts:AssumeRole"
                        }
                    ]
                },
                "Path": "/",
                "ManagedPolicyArns": [
                    "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs",
                    "arn:aws:iam::aws:policy/AWSStepFunctionsFullAccess"
                ]
            }
        },
        "StepFunctionRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Sid": "",
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "states.amazonaws.com"
                            },
                            "Action": "sts:AssumeRole"
                        }
                    ]
                },
                "Path": "/",
                "ManagedPolicyArns": [
                    "arn:aws:iam::aws:policy/service-role/AWSLambdaRole"
                ]
            }
        }
    },
    "Outputs": {
        "HelloWorldApi": {
            "Description": "Sync WF API endpoint",
            "Value": {
                "Fn::Sub": "https://${StepFuncGateway}.execute-api.${AWS::Region}.amazonaws.com/step"
            }
        }
    }
}

我看到的错误如下:

Errors found during import: Unable to put integration on 'POST' for resource at path '/step': Invalid integration URI specified (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 0c74acf9-147f-4561-9f4f-e457096c5533; Proxy: null)

我没有主意了。请帮我解决这个问题。

更新:

我必须将以下代码添加到 x-amazon-apigateway-integration 部分并将类型更改为 AWS:

"uri": {
    "Fn::Join": [
        "",
        [
            "arn:aws:apigateway:",
            {
                "Ref": "AWS::Region"
            },
            ":states:action/StartSyncExecution"
        ]
    ]
},

我必须修复的另一件事是 RequestTemplates,它应该以小写字母 r 开头。在提到的更改之后,堆栈已正确部署,但现在我有限制问题需要解决。

最佳答案

x-amazon-apigateway-integration 缺少 uri 属性。

来自Amazon Developer Guide ,URI 属性定义为:

The endpoint URI of the backend. For integrations of the aws type, this is an ARN value. For the HTTP integration, this is the URL of the HTTP endpoint including the https or http scheme.

例如:

"x-amazon-apigateway-integration": {
    "type": "AWS_PROXY",
    "httpMethod": "POST",
    "uri": "http://petstore.execute-api.us-west-1.amazonaws.com/petstore/pets",
    "payloadFormatVersion": 1.0,
    "otherPropterties": "go here"
}

Amazon 有有关 URI 定义的其他信息 here 。 (为方便起见,已复制)

For HTTP or HTTP_PROXY integrations, the URI must be a fully formed, encoded HTTP(S) URL according to the RFC-3986 specification, for either standard integration, where connectionType is not VPC_LINK, or private integration, where connectionType is VPC_LINK. For a private HTTP integration, the URI is not used for routing.

For AWS or AWS_PROXY integrations, the URI is of the form arn:aws:apigateway:{region}:{subdomain.service|service}:path|action/{service_api}. Here, {Region} is the API Gateway region (e.g., us-east-1); {service} is the name of the integrated AWS service (e.g., s3); and {subdomain} is a designated subdomain supported by certain AWS service for fast host-name lookup. action can be used for an AWS service action-based API, using an Action={name}&{p1}={v1}&p2={v2}... query string. The ensuing {service_api} refers to a supported action {name} plus any required input parameters. Alternatively, path can be used for an AWS service path-based API. The ensuing service_api refers to the path to an AWS service resource, including the region of the integrated AWS service, if applicable. For example, for integration with the S3 API of GetObject, the uri can be either arn:aws:apigateway:us-west-2:s3:action/GetObject&Bucket={bucket}&Key={key} or arn:aws:apigateway:us-west-2:s3:path/{bucket}/{key}

关于aws-cloudformation - AWS CloudFormation。从 API Gateway v1 同步调用 Step Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66182205/

相关文章:

amazon-web-services - AWS Cloudformation - 对现有资源进行逆向工程

websocket - AWS WebSocket API 网关模板选择表达式示例

aws-cdk - AWS StepFunction CDK 结果路径设置为 null

python - AWS - Step 函数,在 TuningStep 中使用执行输入

amazon-web-services - 是否可以向 InputPath 添加新的键/值?

amazon-web-services - 将自定义域用于 AWS websocket API 网关(使用 CloudFormation)时出现证书错误

amazon-web-services - AWS Bucket 策略允许 cloudformation 加载 Lambda 函数层

amazon-web-services - 使用特定策略创建 Amazon EC2 角色但不断收到错误

node.js - CORS 在 AWS、NodeJs 和其他 Web 服务中如何工作?

amazon-web-services - AWS API Gateway 和 AWS Lambda - 处理客户端证书