Reputation: 89
I'm trying to use environmental variables in my Terraform templates.
Even after sourcing .env
files and double checking by running echo $env_variable
, it still won't work.
This only works if I manually specify the password in the variable file...
variable file:
variable "password" {}
.env file:
#!/bin/bash
export PASS="passwordfoo"
Then i run the following commands
terraform init
terraform plan \
-var "password=$PASS" \
And when I try terraform apply
it prompts me:
terraform apply
var.password
Enter a value:
I expect it to run without problems.
I have followed the steps provided by HashiCorp themselves:
reference links.
Upvotes: 5
Views: 8983
Reputation: 1648
Terraform can read Environment variables using tchupp
provider.
Provider: https://registry.terraform.io/providers/tchupp/env/latest/docs/data-sources/variable https://registry.terraform.io/providers/tchupp/env/latest/docs/resources/variable
Example: https://github.com/tchupp/terraform-provider-env/blob/main/example/main.tf
provider "env" {}
resource "env_variable" "user" {
name = "USER"
}
data "env_variable" "shell" {
name = "SHELL"
}
terraform {
required_version = "~> 0.13"
required_providers {
env = {
source = "github.com/tchupp/env"
}
}
}
Upvotes: 0
Reputation: 45343
terraform doesn't get system environment variable as your way export PASS="passwordfoo"
change in .env
as below code and source it, it should work directly, you don't need feed as -var "password=$PASS"
#!/bin/bash
export TF_VAR_password="passwordfoo"
Reference:
https://www.terraform.io/docs/configuration/variables.html#environment-variables
Upvotes: 5