Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Terraform - Language: Variable

Back


Variable


Input Variables


Declaring an Input Variable

variable "image_id" {
  type = string
}

variable "availability_zone_names" {
  type    = list(string)
  default = ["us-west-1a"]
}

variable "docker_ports" {
  type = list(object({
    internal = number
    external = number
    protocol = string
  }))
  default = [
    {
      internal = 8300
      external = 8300
      protocol = "tcp"
    }
  ]
}

Arguments

optional arguments desc
default default value .
type specifies what value types are accepted for the variable.
description specifies the input variable’s documentation.
validation A block to define validation rules, usually in addition to type constraints.
sensitive Limits Terraform UI output when the variable is used in configuration.
nullable Specify if the variable can be null within the module.

Type Constraints


Type Example

# define a variable

variable "mystr" {
  type    = string
  default = "hello terrform"
}

variable "mymap" {
  type = map(string)
  default = {
    mykey = "my value"
  }
}

variable "mylist" {
  type    = list(any)
  default = [1, 2, 3]
}
terrafor console
# > var.mystr
# "hello terrform"
# > var.mymap
# tomap({
#   "mykey" = "my value"
# })
# > var.mymap["mykey"]
# "my value"
# > var.mylist
# tolist([
#   1,
#   2,
#   3,
# ])
# > var.mylist[0]
# 1

Custom Validation Rules

variable "image_id" {
  type        = string
  description = "The id of the machine image (AMI) to use for the server."

  validation {
    condition     = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
    error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
  }
}

Sensitive arguement

variable "password" {
  type = string
  sensitive = true
}

resource "resource_type" "resource_name" {
  parameter = var.password
}

Using Input Variable Values

resource "aws_instance" "example" {
  instance_type = "t2.micro"
  ami           = var.image_id
}

Assigning Values to Root Module Variables

Variables on the Command Line

terraform apply -var="image_id=ami-abc123"
terraform apply -var='image_id_list=["ami-abc123","ami-def456"]' -var="instance_type=t2.micro"
terraform apply -var='image_id_map={"us-east-1":"ami-abc123","us-east-2":"ami-def456"}'
# The above examples show appropriate syntax for Unix-style shells, such as on Linux or macOS.

Variable Definitions (.tfvars) Files

image_id = "ami-abc123"
availability_zone_names = [
  "us-east-1a",
  "us-west-1c",
]
# Linux, Mac OS, and UNIX:
terraform apply -var-file="testing.tfvars"

# PowerShell:
terraform apply -var-file='testing.tfvars'

# Windows cmd.exe:
terraform apply -var-file="testing.tfvars"

{
  "image_id": "ami-abc123",
  "availability_zone_names": ["us-west-1a", "us-west-1c"]
}

Environment Variables

export TF_VAR_image_id=ami-abc123
terraform plan

Output Values

Argument desc
value Mandatory, the value of the output variable
description the description of the output variable
sensitive Whether the variable is sensitive
depends_on Specify the another resource on which the variable depends

Declaring an Output Value

output "instance_ip_addr" {
    value = aws_instance.server.private_ip
    # In this example, the expression refers to the private_ip attribute exposed by an aws_instance resource defined elsewhere in this module (not shown).
}

Local Values


Declaring a Local Value

locals {
  service_name = "forum"
  owner        = "Community Team"
}
locals {
  # Ids for multiple sets of EC2 instances, merged together
  instance_ids = concat(aws_instance.blue.*.id, aws_instance.green.*.id)
}

locals {
  # Common tags to be assigned to all resources
  common_tags = {
    Service = local.service_name
    Owner   = local.owner
  }
}

Using Local Values

resource "aws_instance" "example" {
  # ...

  tags = local.common_tags
}

TOP