如何部署存储在 s3 存储桶中的 API 网关和 template_file?

How to deploy API Gateway with template_file stored in s3 bucket?

是否可以使用存储在 s3 存储桶上的 yaml 文件设置 template_file? 是否有任何其他解决方案可以将外部文件附加到 API 网关(例如可以根据存储在 s3 上的文件构建的 lambda 函数)?

更新: 我尝试将 api_gateway 资源与 s3_bucket_object 组合为数据源,但 terraform 可能看不到它。有信息说没有变化

data "aws_s3_bucket_object" "open_api" {
  bucket = aws_s3_bucket.lambda_functions_bucket.bucket
  key    = "openapi-${var.current_api_version}.yaml"
}

resource "aws_api_gateway_rest_api" "default" {
  name    = "main-gateway"
  body    = data.aws_s3_bucket_object.open_api.body
  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

我也试过用template_file

实现
data "aws_s3_bucket_object" "open_api" {
  bucket = aws_s3_bucket.lambda_functions_bucket.bucket
  key    = "openapi-${var.current_api_version}.yaml"
}

data "template_file" "open_api" {
  template = data.aws_s3_bucket_object.open_api.body
  vars     = {
    lambda_invocation_arn_user_post    = aws_lambda_function.user_post.invoke_arn
    lambda_invocation_arn_webhook_post = aws_lambda_function.webhook_post.invoke_arn
  }
}

resource "aws_api_gateway_rest_api" "default" {
  name    = "main-gateway"
  body    = data.template_file.open_api.rendered
  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

但结果是一样的

编辑:

来自 aws_s3_bucket_object 的 terraform 文档:The content of an object (body field) is available only for objects which have a human-readable Content-Type (text/* and application/json). YAML 文件的 Content-Type header 似乎不清楚,但在这种情况下使用 application/* Content-Type 对于 YAML 会导致 terraform 忽略文件的内容。

问题出在 YAML 文件中,看起来 Terraform 不支持它。必须使用JSON格式。

data "aws_s3_bucket_object" "open_api" {
  bucket = aws_s3_bucket.lambda_functions_bucket.bucket
  key    = "openapi-${var.current_api_version}.json"
}

data "template_file" "open_api" {
  template = data.aws_s3_bucket_object.open_api.body
  vars     = {
    lambda_invocation_arn_user_post    = aws_lambda_function.user_post.invoke_arn
    lambda_invocation_arn_webhook_post = aws_lambda_function.webhook_post.invoke_arn
  }
}

resource "aws_api_gateway_rest_api" "default" {
  name    = "main-gateway"
  body    = data.template_file.open_api.rendered
  endpoint_configuration {
    types = ["REGIONAL"]
  }
}