1

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
  }
}
2
  • 1
    One query here.. is your shell script resides outside terraform module..or you are using this shell script as part of user_data script in your terraform file? Commented Feb 18, 2022 at 18:10
  • 3
    I assume that you can use terraform output json_file? Commented Feb 18, 2022 at 18:10

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

The null_resource should 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}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.