Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

>Solution :

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>

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading