首页 > 解决方案 > 使用相同代码应用第二个 terraform 后,将删除 api-gateway 集成响应设置

问题描述

我正在尝试使用 terraform 在 AWS API Gateway 中创建 REST API。

为了启用 CORS,我的 tf 代码中准备了选项方法和相关的集成设置。当我第一次执行“terraform plan”->“terraform apply”时效果很好。从 AWS 管理控制台检查,我发现在我写的时候创建了一个选项方法。

但是,当我第二次执行“terraform plan”->“terraform apply”而不更改 API 网关时,即使应用已完成,Option 方法的集成响应设置也已删除。(“已删除”表示所有集成响应从管理中消失安慰)。

这是惯常的行为吗?我需要对我的 terraform 代码进行其他设置吗?

我现在的代码如下:

resource "aws_api_gateway_resource" "my_api_resource" {
  rest_api_id = "${aws_api_gateway_rest_api.my_api.id}"
  parent_id   = "${aws_api_gateway_rest_api.my_api.root_resource_id}"
  path_part   = "my_api_resource"
}

resource "aws_api_gateway_method" "my_api_method" {
  rest_api_id   = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id   = "${aws_api_gateway_resource.my_api_resource.id}"
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "integration_request" {
  rest_api_id             = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id             = "${aws_api_gateway_resource.my_api_resource.id}"
  http_method             = "${aws_api_gateway_method.my_api_method.http_method}"
  integration_http_method = "POST"
  type                    = "AWS"
  uri                     = "${var.my_lambda_invocation_arn}"
}

resource "aws_api_gateway_method_response" "http_status_value" {
  rest_api_id = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id = "${aws_api_gateway_resource.my_api_resource.id}"
  http_method = "${aws_api_gateway_method.my_api_method.http_method}"
  status_code = "200"

  response_models = {
    "application/json" = "Empty"
  }

  response_parameters = {
    "method.response.header.Access-Control-Allow-Origin" = true
  }
}

resource "aws_api_gateway_integration_response" "integration_response" {
  rest_api_id = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id = "${aws_api_gateway_resource.my_api_resource.id}"
  http_method = "${aws_api_gateway_method.my_api_method.http_method}"
  status_code = "${aws_api_gateway_method_response.http_status_value.status_code}"

  response_templates = {
    "application/json" = ""
  }

  response_parameters = {
    "method.response.header.Access-Control-Allow-Origin" = "'*'"
  }
}

### enable cors ###
resource "aws_api_gateway_method" "my_api_method_opt" {
  rest_api_id      = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id      = "${aws_api_gateway_resource.my_api_resource.id}"
  http_method      = "OPTIONS"
  authorization    = "NONE"
  api_key_required = false
}

resource "aws_api_gateway_integration" "integration_request_opt" {
  depends_on = ["aws_api_gateway_method.my_api_method_opt"]
  rest_api_id             = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id             = "${aws_api_gateway_resource.my_api_resource.id}"
  http_method             = "${aws_api_gateway_method.my_api_method_opt.http_method}"
  integration_http_method = "OPTIONS"
  type                    = "MOCK"
  request_templates = {
    "application/json" = "${file("./temp.txt")}"
  }
}

resource "aws_api_gateway_method_response" "method_response_opt_200" {
  rest_api_id = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id = "${aws_api_gateway_resource.my_api_resource.id}"
  http_method = "${aws_api_gateway_method.my_api_method_opt.http_method}"
  status_code = "200"

  response_models = {
    "application/json" = "Empty"
  }

  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" = true,
    "method.response.header.Access-Control-Allow-Methods" = true,
    "method.response.header.Access-Control-Allow-Origin"  = true
  }
  depends_on = ["aws_api_gateway_method.my_api_method_opt"]
}

resource "aws_api_gateway_integration_response" "integration_response_opt_200" {
  depends_on = ["aws_api_gateway_method.my_api_method_opt", "aws_api_gateway_method_response.method_response_opt_200"]
  rest_api_id       = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id       = "${aws_api_gateway_resource.my_api_resource.id}"
  http_method       = "${aws_api_gateway_method.my_api_method_opt.http_method}"
  status_code       = "${aws_api_gateway_method_response.method_response_opt_200.status_code}"

  response_templates = {
    "application/json" = ""
  }

  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
    "method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS'",
    "method.response.header.Access-Control-Allow-Origin"  = "'*'"
  }
}

resource "aws_api_gateway_deployment" "my_api_deploy" {
  depends_on  = ["aws_api_gateway_integration_response.integration_response","aws_api_gateway_integration.integration_request", "aws_api_gateway_integration.integration_request_opt","aws_api_gateway_integration_response.integration_response_opt_200"]
  rest_api_id = "${aws_api_gateway_rest_api.my_api.id}"
  stage_name  = "dev"
}

我不知道这对这个问题是否至关重要,我正在使用 s3 存储桶和 dynamoDB 来保存 tfstate 和状态锁。

标签: terraformaws-api-gateway

解决方案


似乎集成会导致integration_request_method某种MOCK形式的冲突,请尝试删除该字段,因为它不是必需的


推荐阅读