Ari
Ari

Reputation: 6159

Access Terraform Cloud environment variable

I'm trying to figure out Terraform Cloud, just wondering how I would access environment variables I've set in the workspace within my files?

// main.tf

// Configure the Google Cloud provider
provider "google" {
  credentials = "GOOGLE_CREDENTIALS"
  project     = "my-project"
  region      = "australia-southeast1"
}

I've set an environment variable on the cloud workspace GOOGLE_CREDENTIALS with the value being my .json key formatted to work with TFC.

Just not sure how to access it within my main.tf file like above

Upvotes: 2

Views: 1806

Answers (1)

Montassar Bouajina
Montassar Bouajina

Reputation: 1652

add credentials to the Terraform Cloud environment variable TF_VAR_credentials and set it as sensitive. then you can use the variable here :

provider "google" {
  credentials = var.credentials
  project     = var.project
  region      = var.region
}

and declare your variable like this

variable "credentials" {
  description = "credentials"
}

variable "project" {
  description = "project"
}

variable "region" {
  description = "region"
}

then on the apply command you can pass :

terraform apply \
    -var "region=${REGION_FROM_ENV}" \
    -var "project=${PROJECT_FROM_ENV}" \
    -var "credentials=${GOOGLE_CREDENTIALS}" 

here's a reference : https://www.terraform.io/docs/commands/apply.html#var-39-foo-bar-39-

Upvotes: 2

Related Questions