Mukteswar Patnaik
Mukteswar Patnaik

Reputation: 113

In Terraform doing ARM deployment by azurerm_resource_group_template_deployment with updated/modified arm_parameter.json values

Looking for help with terraform when I am trying to do the below:

I have arm_template_parameters.json file which is getting fetched from repo thru a pipeline, having below structure, which need to be passed to an AzureARMdeployment after changing First-Key-Value and Second-Key-Value in Terraform. For the First-Key-Vaule, I need to set local.FirstValue and for Second-Key-Value, I need to set local.SecondValue before passing it for deployment. Rest all keys in that JSON will remain same.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "First Key": {
            "value": "First Key Value"
        },
        "Second Key": {
            "value": "Second Key Value"
        },
        "Third Key": {
            "value": "Third Key Value"
        },
        "Fourth Key": {
            "value": "Fourth Key Value"
        }
    }
}

I am trying this as below:

// ---- local.tf ----    
raw_data     = jsondecode(file("arm_template_parameters.json"))
local.raw_data.parameters["First Key"] = local.FirstValue
local.raw_data.parameters["Second Key"] = local.SecondValue


// ---- main.tf ----
resource "azurerm_resource_group_template_deployment" "rgtemplatedeployment" {
  name                = local.arm_temp_deployment_name
  resource_group_name = var.resource_group_name
  deployment_mode     = "Incremental"
  parameters_content = jsonencode({ local.raw_data })
  template_content = file("arm_template.json")
}

Is this will be correct or is there a simpler/better way to achieve this in terraform as I need to make the deployment by changing specific two keys with updated values, keeping the rest of values same.

Upvotes: 0

Views: 486

Answers (1)

Marcin
Marcin

Reputation: 238081

The best way would be to use templatefile. For that your

arm_template_parameters.json

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "First Key": {
            "value": "${firstValue}"
        },
        "Second Key": {
            "value": "${secondValue}"
        },
        "Third Key": {
            "value": "Third Key Value"
        },
        "Fourth Key": {
            "value": "Fourth Key Value"
        }
    }
}

and

resource "azurerm_resource_group_template_deployment" "rgtemplatedeployment" {
  name                = local.arm_temp_deployment_name
  resource_group_name = var.resource_group_name
  deployment_mode     = "Incremental"


  # not sure if you want to use jsonencode again? 
  # your template is already json.
  parameters_content = templatefile(
               "arm_template_parameters.json", 
               {
                   firstValue = "MyCustomFirstValue"
                   secondValue = "MyCustomSecondValue"
               })

  template_content = file("arm_template.json")
}

Upvotes: 1

Related Questions