【问题标题】:terraform remote backend using postgres使用 postgres 构建远程后端
【发布时间】:2021-02-05 21:17:08
【问题描述】:

我计划使用远程后端作为 postgres 而不是 s3 作为企业标准。

terraform {
  backend "pg" {
   conn_str = "postgres://user:pass@db.example.com/schema_name"
}
}

当我们使用 postgres 远程后端时,当我们运行 terraform init 时,我们必须提供特定于该 terraform 文件夹的架构,因为后端仅支持一个表,并且将使用工作空间名称创建新记录。

我现在卡住了,因为我有 50 个项目,每个项目都有 2 层,维护在不同的文件夹中,那么我们需要在 postgres 中创建 100 个模式。此外,在自动配置中处理如此多的模式也很困难。

我们能否处理类似于 S3 的事情,在 S3 中,我们有一个用于所有项目的存储桶,并且在同一个存储桶中具有多个条目,具有在每个 terraform 脚本中指定的不同键。我们能否根据每个 terraform 文件夹的后端配置中提供的键对所有项目和多个表/记录使用单一模式。

【问题讨论】:

    标签: postgresql terraform terraform-remote-state


    【解决方案1】:

    您可以使用单个数据库pg 提供程序将自动创建指定的架构

    类似这样的:

    terraform {
      backend "pg" {
        conn_str = "postgres://user:pass@db.example.com/terraform_backend"
        schema   = "fooapp"
      }
    }
    

    这至少使项目保持独特性。你也可以在上面附加一个层,或者使用 Terraform 工作区。

    如果您在命令行上指定配置(也称为部分配置),如provider recommends,它可能更容易为您的用例动态设置:

    terraform init \
      -backend-config="conn_str=postgres://user:pass@db.example.com/terraform_backend" \
      -backend-config="schema=fooapp-prod"
    

    这在我的场景中效果很好,类似于你的场景。每个项目在共享数据库中都有一个唯一的模式,除了数据库的初始创建/配置之外,不需要任何任务 - 提供者按照指定创建 schema

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 2016-04-05
      • 2013-11-13
      • 1970-01-01
      相关资源
      最近更新 更多