通过 Terraform Null 资源配置 PostgreSQL 架构的问题

Posted

技术标签:

【中文标题】通过 Terraform Null 资源配置 PostgreSQL 架构的问题【英文标题】:Issues configuring PostgreSQL Schema via Terraform Null Resource 【发布时间】:2019-07-25 09:04:15 【问题描述】:

BLUF:我需要能够通过 terraform 以全自动方式将 SCHEMA 添加到 PostgreSQL 10.4 RDS 数据库实例。

我正在尝试全自动安装 Private Terraform Enterprise (PTFE)。部署指南需要 PostgreSQL v9.4 或更高版本。此部署在 AWS 上,因此我选择通过 RDS 部署 psql。 RDS 目前只有 10.5 版本。

我没有使用 psql 的经验,并且使用 bash 的经验有限。我尝试使用 terraform 的“空资源”并运行本地配置程序来执行一些命令。见下文:

resource "null_resource" "db_setup" 
  depends_on = ["aws_db_instance.pes", "aws_security_group.db_main", "random_pet.db-pwd"]

    provisioner "local-exec" 
      command = <<-EOF
       ssh -i '~/example.pem' ubuntu@$var.ec2_instance
       sudo apt-get -y install postgresql 
       psql --host=$aws_db_instance.pes.address --port=5432 --username=example_username --dbname=example_name -c 'CREATE SCHEMA rails;' -c 'CREATE SCHEMA vault;' -c 'CREATE SCHEMA registry;' 
       EOF

      environment 
        PGPASSWORD = "$random_pet.db-pwd.id"
      
    

当我在“terraform apply”中运行它时,数据库和所有其他资源都会正确构建。当 terraform 去执行空资源“db-setup”时,它看起来好像没有在提示时传递密码并且还得到一个 “不会分配伪终端,因为 stdin 不是终端。”

module.pes.null_resource.db_setup: Creating...
module.pes.null_resource.db_setup: Provisioning with 'local-exec'...
module.pes.null_resource.db_setup (local-exec): Executing: ["/bin/sh" "-c" "ssh -i '~/example.pem' ubuntu@<redacted>\nsudo apt-get -y install postgresql \npsql --host=<redacted> --port=5432 --username=example_username --dbname=example_name -c 'CREATE SCHEMA rails;' -c 'CREATE SCHEMA vault;' -c 'CREATE SCHEMA registry;' \n"]
module.pes.null_resource.db_setup (local-exec): Pseudo-terminal will not be allocated because stdin is not a terminal.
module.pes.null_resource.db_setup (local-exec): Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-1032-aws x86_64)

module.pes.null_resource.db_setup (local-exec):  * Documentation:  https://help.ubuntu.com
module.pes.null_resource.db_setup (local-exec):  * Management:     https://landscape.canonical.com
module.pes.null_resource.db_setup (local-exec):  * Support:        https://ubuntu.com/advantage
......

Password:module.pes.null_resource.db_setup: Still creating... (10s elapsed)

正如您在上面看到的,它显示了您在运行命令时通常会看到的密码提示。创建最终会超时。我已经通过正在运行命令的实例手动测试了命令字符串并且它是成功的。被插值的 I 变量是正确的,并且密码生成有效。我认为“local-exec”没有通过 PGPASSWORD 有问题。

操作系统:Ubuntu 18.04 PostreSQL:10.4

我愿意接受任何通过 terraform 和工作实现自动化的想法。提前致谢。

【问题讨论】:

【参考方案1】:

通过 terraform 和工作实现自动化”的最佳方法是利用 PostgreSQL provider。

您需要像这样指定提供程序配置(使用 0.12 语法):

provider "postgresql" 
  host             = var.pg_server.fqdn
  database         = var.pg_db.name
  username         = "$var.pg_admin@$var.pg_server.name"
  password         = var.pg_password
  sslmode          = "require"
  connect_timeout  = 15
  expected_version = var.pg_version

注意:以上要求如here 所述配置实际数据库。除了链接中提到的 2 个建议解决方法之外,如果您使用 terraform 包装器(如 terragrunt),您可以通过将架构创建拆分到单独的模块中并配置模块依赖项来轻松解决此限制。

要实际创建 TFE 所需的架构,您需要:

resource "postgresql_schema" "rails" 
  name = "rails"


resource "postgresql_schema" "vault" 
  name = "vault"


resource "postgresql_schema" "registry" 
  name = "registry"

我只需要在 Azure 中做同样的事情,我就成功地使用了这个组合。

希望对您有所帮助。

【讨论】:

以上是关于通过 Terraform Null 资源配置 PostgreSQL 架构的问题的主要内容,如果未能解决你的问题,请参考以下文章

有条件地使用 terraform 配置 gcp vm 实例

在 EKS 上,我如何验证我通过 Terraform 配置了 Spot 实例

当 DLQ 尚不存在时,如何通过 Terraform 配置 SQS?

如何在脚本开始时在 terraform 中运行 null_resource

将 terraform 0.12.6 切换到 0.13.0 给我 provider["registry.terraform.io/-/null"] 是必需的,但它已被删除

如何让 Terraform 用默认值替换空值?