如何使用 Terraform 在 RDS 上应用 SQL 脚本 [英] How to apply SQL Scripts on RDS with Terraform

查看:47
本文介绍了如何使用 Terraform 在 RDS 上应用 SQL 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Terraform 创建一个脚本,用于构建一些 EC2 服务器和一个 MySQL RDS(使用 AWS Amazon Provider).

I´m using Terraform to create a script that builds some EC2 Servers and a MySQL RDS (using AWS Amazon Provider).

有没有办法在这个创建的 RDS 上执行 SQL 脚本(我想创建用户、表等)?

Is there a way to execute a SQL script on this created RDS (i want to create users, tables, etc)?

提前致谢,

态度,

推荐答案

点赞 此解决方案,如果您的 RDS 数据库是公开可用的并且您已将 ingress 设置为允许您的机器连接.然后,将凭据安全地存储在您的环境中,您只需执行以下操作:

Like this solution, You can also avoid instance setup time/cost by using your own machine with local-exec IF your RDS database is publicly available and you have setup ingress to allow your machine to connect. Then, with credentials stored securely in your environment, you would just do something like:

resource "null_resource" "db_setup" {

  # runs after database and security group providing external access is created
  depends_on = ["aws_db_instance.your_database_instance", "aws_security_group.sg_allowing_external_access"]

    provisioner "local-exec" {
        command = "database connection command goes here"
        environment {
          # for instance, postgres would need the password here:
          PGPASSWORD = "${var.database_admin_password}"
        }
    }
}

请记住,密码和其他敏感变量可以是 输入分别进入 terraform .

Keep in mind that passwords and other sensitive variables can be input into terraform separately.

这篇关于如何使用 Terraform 在 RDS 上应用 SQL 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆