拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何使用存盘在s3存盘桶中的template_file部署APIGateway?

如何使用存盘在s3存盘桶中的template_file部署APIGateway?

白鹭 - 2022-01-23 1991 0 0

是否可以使用存盘在 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"]
  }
}

但结果是一样的。

uj5u.com热心网友回复:

  • 对于 REST API 网关,您可以尝试将aws_api_gateway_rest_apiaws_s3_bucket_object的主体引阵列合为资料源。
  • 对于 HTTP API 网关,您可以尝试将aws_apigatewayv2_apiaws_s3_bucket_object的主体引阵列合为资料源。

编辑:

来自 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 标头似乎不清楚,但在这种情况下,application/*对 YAML 使用 Content-Type 会导致 terraform 忽略档案的内容。

uj5u.com热心网友回复:

问题出在 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"]
  }
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *