icalvete
icalvete

Reputation: 1129

Get variable from terraform cloud

I'm trying https://app.terraform.io/ free plan

I have been setted some variables into the UI.

Now, I want to use those variables into my terraform code.

First I set as backend

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 2.70"
    }
  }
  backend "remote" {
    organization = "mycompany"

    workspaces {
      name = "DEV"
    }
  }
}

I set a datasource too

data "terraform_remote_state" "mycompany" {
  backend = "remote"

  config = {
    organization = "mycompany"

    workspaces = {
      name = "DEV"
    }
  }
}

Now I try to use those variables

provider "aws" {
  region     = var.region
  access_key = data.terraform_remote_state.mycomany.aws_access_key 
  secret_key = data.terraform_remote_state.mycomany.aws_secret_key 
}

When I run terraform apply I get...

Error: Unsupported attribute

  on main.tf line 3, in provider "aws":
   3:   access_key = data.terraform_remote_state.mycomany.aws_access_key

This object has no argument, nested block, or exported attribute named
"aws_access_key"

I have read https://www.terraform.io/docs/cloud/workspaces/variables.html, https://www.hashicorp.com/blog/variable-management-in-terraform-cloud and some more docs.

I cant undestand this... (from https://www.terraform.io/docs/cloud/workspaces/variables.html)

Looking Up Variable Names Terraform Cloud can't automatically discover variable names from a workspace's Terraform code. You must discover the necessary variable names by reading code or documentation, then enter them manually.

If a required input variable is missing, Terraform plans in the workspace will fail and print an explanation in the log.

So, my custion is..

How to use both normal and environment variables from Terraform cloud?

Upvotes: 1

Views: 1777

Answers (2)

Kumar Sambhav Pandey
Kumar Sambhav Pandey

Reputation: 1743

For environment variable choose the category type as environment in workspace or variable set

enter image description here

enter image description here

For variable to be used inside terraform configuration files, declare them as empty variable in variables.tf

variable "myusername" {}

and create, a terraform category variable, in respective workspace or variable set

enter image description here

Notice the quotes enclosing the variable value in workspace in screenshot.

I Terraform hope documentation should have been clearer on this.

Upvotes: 0

icalvete
icalvete

Reputation: 1129

I don't need the datasource.

I need register the variables (without values) into, for example, variables.tf

############################# 
# FROM WORKSPACE
#############################
variable "aws_access_key" {}
variable "aws_secret_key" {}

Now, you can use the variables as usual

provider "aws" {
  region     = var.region
  access_key = var.aws_access_key 
  secret_key = var.aws_secret_key 
}

To change between workspaces (to reuse our code) we can change remote config using prefix instead name. Then terraform CLI ask us for the workespace or you can choose it with terraform workspace select <WORKSPACE_NAME>

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 2.70"
    }
  }
  backend "remote" {
    organization = "mycompany"

    workspaces {
      prefix = "mycompany-"
    }
  }
}

In this case my workspaces are mycompany-DEV and mycompany-PRO

Upvotes: 1

Related Questions