Search code examples
gitlabterraformgitlab-citerraform-template-file

Use GitLab env variable in terraform file


I have a secret stored as an env variable in GitLab : TF_VAR_DD_API_KEY

In my main.tf I am doing something like this:

{
  "name": "datadog-agent",
  "image": "datadog/agent:latest",
  "environment": [
    {
      "name": "DD_API_KEY",
      "value": "${var.DD_API_KEY}"
    }
  ]
}

This doesn't seem to work. How can I get the value from the GitLab TF_DD_API_KEY variable and use it in my main.tf ?

Thank you


Solution

  • You need to add a module input variable like this:

    variable DD_API_KEY {
      type = string
    }
    

    The TF_VAR_ prefix will be removed by terraform.

    Once you have the input variable your reference var.DD_API_KEY will work as expected.