如何将 OpenAPI 用于 aws_api_gateway_rest_api

How do I use OpenAPI for aws_api_gateway_rest_api

我正在尝试使用 OpenAPI,因为它看起来像一个标准,而且比 Terraform 资源更简单。我已将 json 转换为 terraform 可能采用的格式,但出现错误:

module.aws_api_gateway.aws_api_gateway_rest_api.CICDAPI: body must be a single value, not a list

下面是我的代码:

resource "aws_api_gateway_rest_api" "CICDAPI" {
  name        = "cicdapi"
  description = "cicd build pipeline"
  binary_media_types = [
    "application/json"
  ]

  body = {
      swagger = 2
      info {
        title = "AwsServerlessExpressApi"
      }
      basePath = "/prod"
      schemes = [
        "https"
      ]
   ....

我还没有找到关于如何将 OpenAPI 分配给 aws 网关 api 资源的示例。我可以将正文设为 json 字符串吗???文档中的任何地方都没有说。

正如您从错误中看到的那样,它需要是一个字符串,而不是像您的代码中那样的哈希图。

您应该能够简单地将 body 值包装在 heredoc.

所以你想要这样的东西:

resource "aws_api_gateway_rest_api" "CICDAPI" {
  name        = "cicdapi"
  description = "cicd build pipeline"
  binary_media_types = [
    "application/json"
  ]

  body = <<EOF
{
  swagger = 2
  info {
    title = "AwsServerlessExpressApi"
  }
  basePath = "/prod"
  schemes = [
    "https"
  ]
 ....
}
EOF
}

缺少文档来解释这一点,但您也可以在 acceptance test.

中查看它是如何实现的

我还需要检查一下,但我认为您可以使用 body = "${file("path/to/file")}"

从文件加载 OpenAPI 规范