Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terraform replace variable inside json

i am creating rds server using terraform. I pass options group using list variable. The options groups variable in variable.tf like below

options = [
   {
    option_name = "SQLSERVER_BACKUP_RESTORE"

    option_settings=[
      {
      name  = "IAM_ROLE_ARN"
      value = "${role_arn}"
    },
  ]
},

i want replace the "${role_arn}" variable in main.tf . Can any one help with syntax ?

like image 424
CharlesDeeZee Avatar asked Sep 01 '26 22:09

CharlesDeeZee


1 Answers

I solve this kind of issue using terraform template files.

  1. Move your json to a separate file;
  2. update yout json to use Terraform template syntax;
  3. add a data resource to render and parse your template;
  4. reference your rendered template in your resource.

Let's say that you'll need something like:

resource "provider_resource" "name" {
  property = "value"
  options = [
    {
      option_name = "SQLSERVER_BACKUP_RESTORE"
      option_settings = [
        {
          name  = "IAM_ROLE_ARN"
          value = "${role_arn}"
        },
      ]
    }
  ]
}

after the change you will have something like:

data "template_file" "json_template" {
  template = file("path/to/file.json")
  vars = {
    role_arn = var.dynamic_value
  }
}

resource "provider_resource" "name" {
  property = "value"
  options = data.template_file.json_template.rendered
}
like image 181
Gustavo Tavares Avatar answered Sep 05 '26 03:09

Gustavo Tavares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!