【问题标题】:Terraform: API Gateway + Lambda - No integration defined for methodTerraform:API Gateway + Lambda - 没有为方法定义集成
【发布时间】:2022-01-27 16:38:41
【问题描述】:

我正在尝试使用 Lambda 集成创建 API 网关,但遇到了错误:

│ Error: Error creating API Gateway Deployment: BadRequestException: No integration defined for method
│ 
│   with aws_api_gateway_deployment.api-gw,
│   on main.tf line 35, in resource "aws_api_gateway_deployment" "api-gw":
│   35: resource "aws_api_gateway_deployment" "api-gw" {

│ 我进行了一些在线研究,并注意到我需要在部署资源和集成/方法之间建立明确的依赖关系,我做到了,但它仍然无法正常工作。

在这里你可以找到我的代码:

data "template_file" "aws_api_swagger" {
  template = file("${path.module}/openapi.yaml")

  vars = {
   version      = "0.1"
   title        = "Whatever"
   url          = "https://api.example.com"
  }
}

resource "aws_api_gateway_rest_api" "api-gw" {
  body = "${data.template_file.aws_api_swagger.rendered}"
  name = "Whatever"
  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

resource "aws_api_gateway_deployment" "api-gw" {
  rest_api_id = aws_api_gateway_rest_api.api-gw.id

  triggers = {
    redeployment = sha1(jsonencode(aws_api_gateway_rest_api.api-gw.body))
  }

  lifecycle {
    create_before_destroy = true
  }

  depends_on = [
      aws_api_gateway_integration.api-gw,
      aws_api_gateway_method.api-gw
      ]
}

resource "aws_api_gateway_stage" "example" {
  deployment_id = aws_api_gateway_deployment.api-gw.id
  rest_api_id   = aws_api_gateway_rest_api.api-gw.id
  stage_name    = "prod"
}

resource "aws_api_gateway_resource" "api-gw" {
  path_part   = "destinations"
  parent_id   = aws_api_gateway_rest_api.api-gw.root_resource_id
  rest_api_id = aws_api_gateway_rest_api.api-gw.id
}

resource "aws_api_gateway_method" "api-gw" {
  rest_api_id   = aws_api_gateway_rest_api.api-gw.id
  resource_id   = aws_api_gateway_resource.api-gw.id
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "api-gw" {
  rest_api_id             = aws_api_gateway_rest_api.api-gw.id
  resource_id             = aws_api_gateway_resource.api-gw.id
  http_method             = aws_api_gateway_method.api-gw.http_method
  integration_http_method = "POST"
  type                    = "AWS_PROXY"
  uri                     = "arn:aws:apigateway:<MYREGION>:lambda:path/2015-03-31/functions/arn:aws:lambda:<MYREGION>:<MYACCOUNT>:function:<WHATEVER>/invocations"
}

resource "aws_lambda_permission" "apigw_lambda" {
  statement_id  = "AllowExecutionFromAPIGateway"
  action        = "lambda:InvokeFunction"
  function_name = var.lambda_function
  principal     = "apigateway.amazonaws.com"
  source_arn    = "arn:aws:execute-api:<MYREGION>:<MYACCOUNT>:<ID>/*/GET/destinations"
  //source_arn = "arn:aws:execute-api:${var.region}:${var.account}:${aws_api_gateway_rest_api.api-gw.id}/*/${aws_api_gateway_method.api-gw.http_method}${aws_api_gateway_resource.api-gw.path}"
}

您可以在此处找到 OPENAPI 定义:

openapi: "3.0.2"

info:
  title: ${title}
  version: ${version}

servers:
  - url: ${url}

paths:
  /flyto:
    get:
      description: Returns a list of destinations JetAir flies to. 
      parameters:
          - name: iata
            in: query
            description: IATA code of the departure city. If no code is provided, it returns all cities.
            schema:
              type: string
            allowEmptyValue: true
      responses:
        "200":
          description: Successfully returned a list of destinations.
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  required:
                    - username
                  properties:
                    id:
                      type: integer
                      example: 5
                    location:
                      type: string
                      example: "Sao Paulo"
                    coordinates:
                      type: object
                      properties:
                        lat:
                          type: number
                          format: double
                          example: -23.45
                        lng:
                          type: number
                          format: double
                          example: -46.53
                    destinations:
                      type: array
                      items:
                        type: number
                        example: [1, 2, 4, 6]
                    visible:
                      type: boolean
        "400":
          description: Invalid request
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: "Your request could not be completed"

Terraform 无法完成的另一件事是将通过 OpenAPI 定义的路径与 Lambda 集成集成。

非常感谢!

【问题讨论】:

  • 完整的错误信息是什么?
  • @Marcin 我已经用完整的错误消息更新了它。谢谢!
  • 我试图复制您的问题,但它对我有用。您确定您提供的代码正确地代表了您的实际代码库吗?
  • @Marcin 你又是绝对正确的,OPENAPI 文件并没有像我想的那样取消注释。我已经相应地更新了它。谢谢!
  • 聂马问题:-)

标签: amazon-web-services aws-lambda terraform aws-api-gateway


【解决方案1】:

基于 cmets。

复制问题的尝试表明提供的代码是正确的。进一步调查松了一口气,OPENAPI 文件在 OP 使用的实际代码中被注释了。

【讨论】:

    猜你喜欢
    • 2017-06-03
    • 2016-12-25
    • 1970-01-01
    • 2021-06-21
    • 2018-02-15
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    相关资源
    最近更新 更多