Home > Mobile >  How can I pass in a variable to an XML file (via Terraform)?
How can I pass in a variable to an XML file (via Terraform)?

Time:11-16

I have an XML file that's used for configuring a database connection with an application I'm using. However, it needs my database password which is stored as an AWS parameter. I know how to pass in variables in Terraform to a user-data script, but how do I do it to an XML file?

Here's my relevant Terraform code:

# My DB Password that is stored on AWS
data "aws_ssm_parameter" "db_password" {
  name = "terraform/db-password"
}

# Creating a locals value from my dbconfig.xml file
locals {
  dbconfig = templatefile("${path.module}/../../templates/dbconfig.xml", db_password = data.aws_ssm_parameter.db_password)
}

In my locals value I assign the SSM Parameter that is my password to the variable db_password, now how do I use that variable within my xml file?

dbconfig.xml:

<?xml version="1.0" encoding="UTF-8"?>

<jira-database-config>
  <name>defaultDS</name>
  <database-type>postgresaurora96</database-type>
  <jdbc-datasource>
    <driver-class>org.postgresql.Driver</driver-class>
    <username></username>
    <password>db_password</password> <--- db_password variable
    <connection-properties>tcpKeepAlive=true;socketTimeout=240</connection-properties>
  </jdbc-datasource>
</jira-database-config>

CodePudding user response:

You can just reference your db_password as a TF variable in the template because you are passing it as db_password:

<?xml version="1.0" encoding="UTF-8"?>

<jira-database-config>
  <name>defaultDS</name>
  <database-type>postgresaurora96</database-type>
  <jdbc-datasource>
    <driver-class>org.postgresql.Driver</driver-class>
    <username></username>
    <password>${db_password}</password>
    <connection-properties>tcpKeepAlive=true;socketTimeout=240</connection-properties>
  </jdbc-datasource>
</jira-database-config>

  • Related