How to pass terraform variable to bash script inside terraform local-exec – Example

How To

Problem statement -> Most of the organizations prefer to use Terraform to provision/update/destroy resources in cloud platforms such as Azure, AWS, GCP. Sometime its required to use local-exec to perform certain operations from terraform in case the TF provider/resources are not available.

In my case , i was using Terraform with github workflow (on push) to create resources in appropriate cloud platform and i was using Linux github runner to run the terraform. i wanted to execute az cli in local-exec but also wanted to add some condition based on terraform variables before running AZ cli.

Solution-> if we want to use terraform variable in local-exec then just use “${var.VariableName}” inside the local-exec provisioner.

Example code ->

resource "null_resource" "azcli" {
  provisioner "local-exec" {
    command = <<EOT
      declare -a location=("australiaeast" "australiasouthEast" "brazilsouth" "canadacentral" "centralus" "eastus" "eastus2")
      for i in "$${location[@]}"
        if [ "${var.mssql_server_location}" = "$${location[i]}" ]
          echo "you reached if block"
          az sql db update --resource-group "${}" --server "${var.mssql_server_name}" --name "${var.resource_name}" --maint-config-id "${var.maintenancewindowconfig}"
  depends_on = [

(Visited 22 times, 1 visits today)