Home > Software engineering >  How to insert Terraform variable in shell script
How to insert Terraform variable in shell script

Time:02-20

is there any way to use terraform variable inside a shell script?

The variable i want to use inside my shell-script:

variable "json_file" {
description = "JSON filename"
type = string
default = "Testing_Dashboard--2022-02-16T14_29_16.json"
}

Shell-script (var placement is marked at the end of curl):

#!/bin/sh

DD_API_KEY=abcd
DD_APP_KEY=abcd

dashboard_id="p24-ry9-p5i"

curl -X GET "https://api.datadoghq.eu/api/v1/dashboard/${dashboard_id}" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_API_KEY_CENSHARE}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY_CENSHARE}" \
> dashboard_exports/<USE_THE_VARIABLE_HERE>

I'am executing the shell-script with null_resource type:

resource "null_resource" "provision" {

  triggers = {
    build_number = "${timestamp()}"
  }

  provisioner "local-exec" {
    command = "/bin/bash shell_scripts/get_dashboard_json.sh"
    interpreter=["/bin/bash", "-c"]
    working_dir=path.module
  }
}

CodePudding user response:

You can tell the local-exec provisioner to set environment variables when it launches the child program:

  provisioner "local-exec" {
    command     = "shell_scripts/get_dashboard_json.sh"
    interpreter = ["/bin/bash"]
    working_dir = path.module

    environment = {
      DASHBOARD_FILENAME = var.json_file
    }
  }

The provisioner will just set the environment variable before launching the program, so it's up to the program to access the environment and decide how to make use of it.

Since your program here is a shell script running in bash, you can access the variable's value using the normal bash interpolation syntax, which is independent of Terraform's interpolation syntax despite using similar punctuation:

#!/bin/sh

DD_API_KEY=abcd
DD_APP_KEY=abcd

dashboard_id="p24-ry9-p5i"

curl -X GET "https://api.datadoghq.eu/api/v1/dashboard/${dashboard_id}" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_API_KEY_CENSHARE}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY_CENSHARE}" \
> "dashboard_exports/${DASHBOARD_FILENAME}"

Please note that provisioners are a last resort. If there is a Terraform provider for Datadog that can support the operation you are trying to perform here then you should use that provider instead.

The DataDog/datadog provider has a data source datadog_dashboard that seems to be able to fetch the data about a particular dashboard given its name.

CodePudding user response:

Just a quick update, I found the solution. You can archive the needed behavior by passing Terraform templatefile() function inside the null_resource command value.

The null_resource shut look like this:

resource "null_resource" "provision" {

provisioner "local-exec" {
  command = templatefile("shell_scripts/get_dashboard_json.sh",
  {
    "DD_API_KEY" = local.dd_api_key
    "DD_APP_KEY" = local.dd_app_key
    "dashboard_id"        = local.dashboard_id
    "file_name"           = local.file_name
  })
  interpreter=["/bin/bash", "-c"]
  working_dir=path.module
  }
}

To interpret the variable inside the shell script you have to assign a placeholder like this: ${var_name}

#!/bin/sh

curl -X GET "https://api.datadoghq.eu/api/v1/dashboard/${dashboard_id}" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_API_KEY}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY}" \
> dashboard_exports/${file_name}
  • Related