Terraform 无法将创建的集成附加到 API 网关的路由中

Posted

技术标签:

【中文标题】Terraform 无法将创建的集成附加到 API 网关的路由中【英文标题】:Terraform cannot attach created integrations into routes for API Gateway 【发布时间】:2020-11-27 09:31:24 【问题描述】:

我正在尝试使用 Terraform 部署 API 网关,以将流量路由到我的域 google.com 作为示例。

我使用aws_apigatewayv2_api 为集成创建 HTTP_PROXY 类型。我浏览了文档,但仍然找不到将集成附加到路由GET /sitemap.xml 的方法。如何处理?

resource "aws_api_gateway_deployment" "_" 
  rest_api_id = aws_api_gateway_rest_api._.id
  stage_name  = ""

  lifecycle 
    create_before_destroy = true
  


resource "aws_apigatewayv2_api" "_" 
  name          = "example"
  protocol_type = "HTTP"


resource "aws_apigatewayv2_route" "apigateway_route" 
  api_id    = aws_apigatewayv2_api._.id
  route_key = "GET /sitemap.xml"


resource "aws_apigatewayv2_integration" "apigateway_intergration" 
  api_id           = aws_apigatewayv2_api._.id
  integration_type = "HTTP_PROXY"

  connection_type      = "INTERNET"
  description          = "Gateway intergration for EC2"
  integration_method   = "ANY"
  integration_uri      = "https://www.google.com"
  passthrough_behavior = "WHEN_NO_MATCH"


# resource "aws_apigatewayv2_deployment" "apigateway_deployment" 
#   api_id      = aws_apigatewayv2_route.apigateway_route.api_id
#   description = "Example deployment"

#   lifecycle 
#     create_before_destroy = true
#   
# 

resource "aws_apigatewayv2_stage" "apigateway_stage" 
  api_id = aws_apigatewayv2_api._.id
  name   = "example-stage"

【问题讨论】:

【参考方案1】:

您缺少几个组件。最重要的是,您的aws_apigatewayv2_routeaws_apigatewayv2_integration 之间没有链接

链接是使用target 参数建立的。

同样,aws_apigatewayv2_stageaws_apigatewayv2_deployment 之间也没有链接。

你可以看看以下版本的代码:


resource "aws_apigatewayv2_deployment" "example" 
  api_id      = aws_apigatewayv2_api._.id
  description = "Example deployment"

  lifecycle 
    create_before_destroy = true
  
  
  depends_on = [
    aws_apigatewayv2_route.apigateway_route
  ]



resource "aws_apigatewayv2_api" "_" 
  name          = "example"
  protocol_type = "HTTP"


resource "aws_apigatewayv2_route" "apigateway_route" 
  api_id    = aws_apigatewayv2_api._.id
  route_key = "GET /sitemap.xml"
  
  target = "integrations/$aws_apigatewayv2_integration.apigateway_intergration.id"
  


resource "aws_apigatewayv2_integration" "apigateway_intergration" 
  api_id           = aws_apigatewayv2_api._.id
  integration_type = "HTTP_PROXY"

  connection_type      = "INTERNET"
  description          = "Gateway intergration for EC2"
  integration_method   = "ANY"
  integration_uri      = "https://www.google.com"
  passthrough_behavior = "WHEN_NO_MATCH"


resource "aws_apigatewayv2_stage" "apigateway_stage" 
  api_id = aws_apigatewayv2_api._.id
  name   = "example-stage"
  deployment_id = aws_apigatewayv2_deployment.example.id  

上面的代码正确创建了集成:

【讨论】:

以上是关于Terraform 无法将创建的集成附加到 API 网关的路由中的主要内容,如果未能解决你的问题,请参考以下文章

azure terraform 将 azure 文件共享附加到 Windows 机器

Terraform - 创建 EBS 的快照,然后将快照转换为 EBS 并附加到 EC2

使用 terraform 创建 IAM 角色并将其附加到 EC2

如何使用 Terraform 将资源动态附加到内联策略?

将安全组附加到我的 aws 实例的 Terraform 问题

terraform api 网关与 openapi 规范的集成