aws-cloudformation - 使用 SAM 将 lambda 集成添加到 HttpApi 路由

标签 aws-cloudformation aws-sam

我目前正在尝试将 AWS::Serverless::HttpApi 与一组 AWS::Serverless::Function 集成。目标是在 SAM 模板中定义这些资源,并使用 swagger 文件定义实际的 API。

我的 SAM 模板定义如下:


Resources:
  apiPing:
    Type: AWS::Serverless::Function
    Properties:
      Description: 'Ping'
      CodeUri: ../bin/cmd-api-ping.zip
      Handler: cmd-api-ping
      Runtime: go1.x
      Role:
        Fn::GetAtt: apiLambdaRole.Arn
      Events:
        PingEvent:
          Type: HttpApi
          Properties:
            ApiId: !Ref api
            Path: /ping
            Method: post
  api:
    Type: AWS::Serverless::HttpApi
    Properties:
      StageName: prod
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location: swagger.yaml
      AccessLogSettings:
        DestinationArn: !GetAtt accessLogs.Arn
        Format: $context.requestId

还有我的招摇文件:

openapi: 3.0.1
info:
  title: 'API'
  version: 2019-10-13

paths:
  /ping:
    post:
      summary: 'invoke ping'
      operationId: 'apiPing'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PingRequest'
        required: true
      responses:
        '200':
          description: 'Successful'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PongResponse'
      x-amazon-apigateway-integration:
        httpMethod: "POST"
        type: aws_proxy
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${apiPing.Arn}/invocations
        responses:
          default:
            statusCode: "200"
        contentHandling: "CONVERT_TO_TEXT"
        passthroughBehavior: "when_no_match"
components:
  schemas:
    PingRequest:
      description: 'a ping request'
      type: object
      properties:
        ping:
          description: 'some text'
          type: string
    PongResponse:
      description: 'a pong response'
      type: object
      properties:
        pong:
          description: 'some text'
          type: string

此模板部署时没有任何错误,但是没有集成附加到 /ping POST 路由。

CloudFormation 中转换后的模板确实显示了已加载的 swagger 文件:

    "api": {
      "Type": "AWS::ApiGatewayV2::Api",
      "Properties": {
        "Body": {
          "info": {
            "version": 1570924800000,
            "title": "API"
          },
          "paths": {
            "/ping": {
              "post": {
                "requestBody": {
                  "content": {
                    "application/json": {
                      "schema": {
                        "$ref": "#/components/schemas/PingRequest"
                      }
                    }
                  },
                  "required": true
                },
                "x-amazon-apigateway-integration": {
                  "contentHandling": "CONVERT_TO_TEXT",
                  "responses": {
                    "default": {
                      "statusCode": "200"
                    }
                  },
                  "uri": {
                    "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${apiPing.Arn}/invocations"
                  },
                  "httpMethod": "POST",
                  "passthroughBehavior": "when_no_match",
                  "type": "aws_proxy"
                },
                "summary": "invoke ping",
                "responses": {
                  "200": {
                    "content": {
                      "application/json": {
                        "schema": {
                          "$ref": "#/components/schemas/PongResponse"
                        }
                      }
                    },
                    "description": "Successful"
                  }
                },
                "operationId": "apiPing"
              }
            }
          },
          "openapi": "3.0.1",
          "components": {
            "schemas": {
              "PingRequest": {
                "type": "object",
                "description": "a ping request",
                "properties": {
                  "ping": {
                    "type": "string",
                    "description": "some text"
                  }
                }
              },
              "PongResponse": {
                "type": "object",
                "description": "a pong response",
                "properties": {
                  "pong": {
                    "type": "string",
                    "description": "some text"
                  }
                }
              }
            }
          },
          "tags": [
            {
              "name": "httpapi:createdBy",
              "x-amazon-apigateway-tag-value": "SAM"
            }
          ]
        }
      }
    }

我试图了解我可能需要更改或添加哪些内容才能将集成添加到 http api。我在aws文档中找不到任何明确的解释。

最佳答案

我已经成功解决了这个问题。 aws::serverless::httpapi 创建 AWS::ApiGatewayV2::Api 资源。这需要与之前版本的 ApiGateway 不同的集成。

x-amazon-apigateway-integration 定义了一个键 payloadFormatVersion。尽管documentation提示1.0和2.0都支持,看来必须使用2.0。因此,我的 x-amazon-apigateway-integration 已变为以下内容(我确实对其进行了一些清理):

x-amazon-apigateway-integration:
  payloadFormatVersion: "2.0"
  httpMethod: "POST"
  type: "aws_proxy"
  uri:
    Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${apiPing.Arn}/invocations
  responses:
    default:
      statusCode: "200"
  connectionType: "INTERNET"

这样,集成就会在部署时应用。

关于aws-cloudformation - 使用 SAM 将 lambda 集成添加到 HttpApi 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63877914/

相关文章:

ruby - 如何重构 Ruby block 并将重复调用提取到方法中?

amazon-web-services - 跨多个AWS账户构建nodejs应用程序

amazon-web-services - 重命名 CloudFormation 模板中的资源

amazon-web-services - AWS AppSync Lambda 授权者

swift - 部署自定义运行时 lambda 时 sam 包太大

aws-cloudformation - 在 Serverless 中仅更新一项功能

java - API Gateway Lambda 集成 SelectionPattern 不匹配

amazon-web-services - 在 AWS::Serverless::Api SAM 模板中定义 URL 查询字符串参数

amazon-web-services - 无法创建变更集:转换 AWS::Serverless-2016-10-31 失败并显示:无效的无服务器应用程序规范文档

没有为 aws sam cli 配置 Python