7

I am writing a script that takes care of running a terraform file and create infra. I have requirement where I need to take output from the terraform into the same script to create schema for DB. I need to take Endpoint, username, Password and DB name and take it as an input into the script to login to the db and create schema. I need to take the output from aws_db_instance from terraform which is already created and push that as an input into the bash script. Any help would be really appreciated as to how can we achieve this. thanks in advance. Below is the schema code that I would be using in script and would need those inputs from terraform.

RDS_MYSQL_USER="Username";
RDS_MYSQL_PASS="password";
RDS_MYSQL_BASE="DB-Name";

mysql -h $RDS_MYSQL_ENDPOINT -P $PORT -u $RDS_MYSQL_USER -p $RDS_MYSQL_PASS -D $RDS_MYSQL_BASE -e 'quit';```
5
  • 2
    Any reason not to use the mysql Terraform provider to create the schema? Commented Sep 16, 2021 at 13:03
  • I might have missed to add complete details. The Aurora DB is already created in my cloud env, I just need to call those username, password, DBName from terraform into the script Commented Sep 16, 2021 at 13:23
  • 1
    Yes but why not manage the schema with Terraform as well? Commented Sep 16, 2021 at 13:34
  • This is my first attempt with database side, so not sure how to do that, any suggestions how i can implement them Commented Sep 16, 2021 at 15:01
  • Thanks that provider option made it easier to create schema, earlier I was trying to use it via script, but once implemented via terraform that was better. Commented Sep 16, 2021 at 20:49

1 Answer 1

14

The usual way to export particular values from a Terraform configuration is to declare Output Values.

In your case it seems like you want to export several of the result attributes from aws_db_instance, which you could do with declarations like the following in your root module:

output "mysql_host" {
  value = aws_db_instance.example.address
}

output "mysql_port" {
  value = aws_db_instance.example.port
}

output "mysql_username" {
  value = aws_db_instance.example.username
}

output "mysql_password" {
  value     = aws_db_instance.example.password
  sensitive = true
}

output "mysql_database_name" {
  value     = aws_db_instance.example.name
}

After you run terraform apply you should see Terraform report the final values for each of these, with the password hidden behind (sensitive value) because I declared it with sensitive = true.

Once that's worked, you can use the terraform output command with its -raw option to retrieve these values in a way that's more convenient to use in a shell script. For example, if you are using a Bash-like shell:

MYSQL_HOST="$(terraform output -raw mysql_host)"
MYSQL_PORT="$(terraform output -raw mysql_port)"
MYSQL_USERNAME="$(terraform output -raw mysql_username)"
MYSQL_PASSWORD="$(terraform output -raw mysql_password)"
MYSQL_DB_NAME="$(terraform output -raw mysql_database_name)"

Each run of terraform output will need to retrieve the latest state snapshot from your configured backend, so running it five times might be slow if your chosen backend has a long round-trip time. You could potentially optimize this by installing separate software like jq to parse the terraform output -json result to retrieve all of the values from a single command. There are some further examples in terraform output: Use in Automation.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.