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

标签 amazon-web-services terraform aws-api-gateway terraform-provider-aws

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

我使用 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"
}

UI API Gateway integration

最佳答案

您缺少几个组件。最重要的是,您的 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  
}

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

enter image description here

关于amazon-web-services - Terraform 无法将创建的集成附加到 API 网关的路由中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63295113/

相关文章:

wordpress - 从 SSL 上的裸域重定向失败

amazon-web-services - 创建 S3 存储桶策略时出错 - 属性 PolicyDocument 的值必须是对象

azure - 解析 JSON 时出错 : invalid character 'Â' looking for beginning of value while creating azure data factory source dataset

terraform - 有没有办法防止 Terraform google_container_cluster 在没有任何变化时被破坏和重新创建?

azure - Windows 代理池只能使用 Azure-CNI 添加到 AKS 群集

javascript - AWS Cognito 注册方法上的异步执行错误

amazon-web-services - 在 SAM 模板的第二个参数中使用 !Ref

amazon-web-services - 如何找到api网关阶段的arn?

java - 访问阶段变量 - AWS API Gateway

aws-lambda - 使用 Serverless 和 API Gateway 解析 React 中的表单数据