使用Terraform for RDS

时间:2016-09-23 08:10:28

标签: amazon-web-services docker rds terraform

Terraform allows you使用选项usernamepassword定义Postgres主用户和密码。但是没有选项设置应用程序postgres用户,你会怎么做?

2 个答案:

答案 0 :(得分:2)

AWS RDS resource仅用于使用AWS API创建/更新/删除RDS资源。

要在RDS实例本身上创建用户或数据库,您要么使用其他工具(例如psql - 官方命令行工具或配置管理工具,例如Ansible)或使用Terraform的Postgresql provider

假设您已经创建了RDS实例,那么您将作为主用户连接到该实例,然后使用以下内容创建应用程序用户:

provider "postgresql" {
  host = "postgres_server_ip1"
  username = "postgres_user"
  password = "postgres_password"
}

resource "postgresql_role" "application_role" {
  name = "application"
  login = true
  password = "application-password"
  encrypted = true
}

答案 1 :(得分:0)

除了@ydaetskcoR 的回答,这里是 RDS PostgreSQL 的完整示例;

provider "postgresql" {
  scheme    = "awspostgres"
  host      = "db.domain.name"
  port      = "5432"
  username  = "db_username"
  password  = "db_password"
  superuser = false
}


resource "postgresql_role" "new_db_role" {
    name                = "new_db_role"
    login               = true
    password            = "db_password"
    encrypted_password  = true
}

resource "postgresql_database" "new_db" {
  name              = "new_db"
  owner             = postgresql_role.new_db_role.name
  template          = "template0"
  lc_collate        = "C"
  connection_limit  = -1
  allow_connections = true
}