用于执行mysql脚本的terraform local-exec命令 [英] terraform local-exec command for executing mysql script
问题描述
我创建了aws_db_instance以使用Terraform配置来配置RDS MySQL数据库.我需要在RDS上执行SQL脚本(CREATE TABLE和INSERT语句).我被困在这里使用什么命令?有人在我的用例中有示例代码吗?请指教.谢谢.
I created the aws_db_instance to provision the RDS MySQL database using Terraform configuration. I need to execute the SQL Scripts (CREATE TABLE and INSERT statements) on the RDS. I'm stuck on what command to use here?? Anyone has the sample code in my use case? Please advise. Thanks.
resource "aws_db_instance" "mydb" {
# ...
provisioner "local-exec" {
command = "command to execute script.sql"
}
}
推荐答案
This is possible using a null_resource that depends on the aws_db_instance.my_db
. This way the host is available when you run the command, it will only work if there aren't rules preventing you from accessing the DB such as security group ingress or not publicly accessible.
示例:
resource "null_resource" "setup_db" {
depends_on = ["aws_db_instance.my_db"] #wait for the db to be ready
provisioner "local-exec" {
command = "mysql -u ${aws_db_instance.my_db.username} -p${var.my_db_password} -h ${aws_db_instance.my_db.address} < file.sql"
}
}
这篇关于用于执行mysql脚本的terraform local-exec命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!