首页 > 解决方案 > 为什么 terraform 偶尔无法创建此 api 网关资源?

问题描述

我有一个 terraform 模块,它创建一个 API 网关实例,并将几个 lambda 连接到它。它构建并可靠地创建 API,但在构建中经常会出现故障,需要通过简单重复 terraform apply 命令来解决,有时重复两次。

这是模块的相关部分:

locals {
  lambda_endpoints_by_path = { for e in var.lambda_endpoints : e.api_path => e }
}

resource "aws_api_gateway_resource" "gateway_resource" {
  for_each    = local.lambda_endpoints_by_path
  rest_api_id = aws_api_gateway_rest_api.api_gateway.id
  parent_id   = aws_api_gateway_rest_api.api_gateway.root_resource_id
  path_part   = each.value.api_path
}

resource "aws_api_gateway_method" "gateway_method" {
  for_each             = local.lambda_endpoints_by_path
  rest_api_id          = aws_api_gateway_rest_api.api_gateway.id
  resource_id          = aws_api_gateway_resource.gateway_resource[each.key].id
  http_method          = each.value.method
  authorization        = "COGNITO_USER_POOLS"
  authorizer_id        = aws_api_gateway_authorizer.authorizer.id
  authorization_scopes = each.value.scopes
}

resource "aws_api_gateway_method_response" "gateway_response_200" {
  for_each    = local.lambda_endpoints_by_path
  rest_api_id = aws_api_gateway_rest_api.api_gateway.id
  resource_id = aws_api_gateway_resource.gateway_resource[each.key].id
  http_method = aws_api_gateway_method.gateway_method[each.key].http_method
  status_code = "200"
}

resource "aws_api_gateway_integration_response" "integration_response" {
  for_each    = local.lambda_endpoints_by_path
  rest_api_id = aws_api_gateway_rest_api.api_gateway.id
  resource_id = aws_api_gateway_resource.gateway_resource[each.key].id
  http_method = aws_api_gateway_method.gateway_method[each.key].http_method
  status_code = aws_api_gateway_method_response.gateway_response_200[each.key].status_code
}

resource "aws_api_gateway_integration" "proxy_integration" {
  for_each                = local.lambda_endpoints_by_path
  rest_api_id             = aws_api_gateway_rest_api.api_gateway.id
  resource_id             = aws_api_gateway_resource.gateway_resource[each.key].id
  http_method             = each.value.method
  integration_http_method = each.value.method
  uri                     = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${each.value.lambda_arn}:$${stageVariables.env}/invocations"
  type                    = "AWS_PROXY"
}

var.lambda_endpoints是一个对象列表,其中包含有关每个 lambda 端点的相关信息。

当它失败时,这是错误:

Error: Error creating API Gateway Integration: NotFoundException: Invalid Method identifier specified
Error: Error creating API Gateway Integration Response: NotFoundException: Invalid Integration identifier specified

当它失败时,第一次重新运行会清除创建集成的错误,第二次会清除创建集成响应的错误。terraform 在第一遍时没有弄清楚构建顺序是否有原因?如何避免这些重建步骤?

标签: terraformaws-api-gateway

解决方案


推荐阅读