Search code examples
terraformterraform-provider-cloudflare

Export environment not work for terraform


I'm using Cloudflare provider and it requires some secret parms to pass on terraform. I tried using export shell env before running plan, but terraform seems not to understand.

>> export CLOUDFLARE_EMAIL="xxx"
>> export CLOUDFLARE_API_KEY="xxx"
>> export CLOUDFLARE_ZONE_ID="xxx"

>> terragrunt plan -lock=false 
╷
│ Error: Missing required argument
│ 
│   on stag.tf line 523, in module "dns_cloudflare":
│  523: module "dns_cloudflare" {
│ 
│ The argument "cloudflare_email" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│ 
│   on stag.tf line 523, in module "dns_cloudflare":
│  523: module "dns_cloudflare" {
│ 
│ The argument "cloudflare_api_key" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│ 
│   on stag.tf line 523, in module "dns_cloudflare":
│  523: module "dns_cloudflare" {
│ 
│ The argument "cloudflare_zone_id" is required, but no definition was found.
╵
ERRO[0010] 1 error occurred:
    * exit status 1


Solution

  • In order for Terraform to pick those env vars up you need to prefix them with TF_VAR_, followed by the variable name, e.g., looking at your code for dns_cloudflare the full name of the env var would be TF_VAR_dns_cloudflare. The same would apply for the rest of the variables terraform is complaining about. More information can be found in [1].

    EDIT: If there are environment variables that could be used to set up provider configuration, then the above is not an error and it should stay as is:

    >> export CLOUDFLARE_EMAIL="xxx"
    >> export CLOUDFLARE_API_KEY="xxx"
    >> export CLOUDFLARE_ZONE_ID="xxx"
    

    However, it then means that no values were provided for the variables defined in the module and since they are not defaulting to a value, those values have to be provided when running plan and apply commands. So either:

    1. define the default values for those variables (probably not a good idea because of the API key)
    2. Provide the values in the CLI when running plan/apply as described in [2]
    3. Provide the values using TF_VAR_ syntax

    [1] https://www.terraform.io/cli/config/environment-variables

    [2] https://www.terraform.io/language/values/variables#variables-on-the-command-line