Terraform specific environment variables

Does anyone know if there is a way to populate variables in Terraform based on what the environment / workspace is? Preferably

  • populates the var namespace (i.e. not an external data source),
  • no wrapper required
    • like tf(){ terraform --var-file=$(get_tf_env).tfvars
  • takes effect by changing the terraform env / workspace without any additional control steps (i.e. steps that do not start at startup terraform env)?
+4
source share
4 answers

Terraform Workspaces

- Terraform. Terraform .

Terraform 0.9 "". 0.10 , " " Terraform, , Terraform.

. , . :

resource "aws_instance" "example" {
  count = "${terraform.workspace == "default" ? 5 : 1}"

  # ... other arguments
}
+4

Terraform, . , , TF, tfvars. , , Terraform Workspaces, 0.10.

, OctopusDeploy. , Octopus . tfvars Octopus .

, tfvars Octopus , .

, , travform Terraform ( ) Octopus ( ).

. tfvars,

instance_size = "Medium"

2 Octopus, Staging and Production. Octopus "instance_size" (, "" " " ).

, , "instance_size", , , :

instance_size = "Big"

instance_size = "Biggest"
+1

"Stacks" Terraform Project, "" (aka Environment). , , .

?

  • , . (aka Stacks)
  • , . (aka Limit the Blast Radius)
  • , , . (aka Patterns, Workflow)

  • "State" "Stacks" "Workspaces"

  • "" "" " "

  • "" " ".

Terraform Project

/
  /scripts
     <shell scripts>
     <terraform wrapper functions>

  /stacks
    /application_1   # Provisions Application 1 and its dependencies
    /application_2   # Provisions Application 2 and its dependencies
    /application_n   # Provisions Application N and its dependencies
        backend.tf       # Remote State
        data.tf          # Data Sources
        stack.tf         # Stack Variables and Defaults
        aws_resource.tf 
        ...
        ...    
    /network    # Provisions VPC, Subnets, Route Tables, Route53 Zones
    /security   # Provisions Security Groups, Network ACLs, IAM Resources
    /storage    # Provisions Storage Resources like S3, EFS, CDN

  global.tf     # Global Variables
  dev.tfvars    # Development Environment Variables
  tst.tfvars    # Testing Environment Variables
  stg.tfvars    # Staging Environment Variables
  prd.tfvars    # Production Environment Variables
  terraform.sh  # Wrapper Script for Executing Terraform (Workflow)

, .

Terraform . .

, , , Dev, Test, Production ..... .

, , Terraform AWS.

!

0

var, :

variable "ami_id" {
  type = "map"

  default = {
    stg = "ami-foo28929"
    prd = "ami-bar39b12"
  }
}

image_id = "${lookup(var.ami_id, terraform.workspace)}"
0

Source: https://habr.com/ru/post/1683326/


All Articles