variables
Introduction
Terraform variables are placeholders for values that you can use to make your configurations more dynamic and reusable
Differnet types for terraform variables are below
- String - stores text values
- Number - numeric values
- Bool - short for boolean (true, false)
- List - sequence of values of same type
- Maps - collection of key-value pairs, each unique key map to specific values.
- Tuple - similar to list but can contain fixed number of elements.
- Object - used to define structure with named attributes
- Set - collections of unique values of the same type.
Local variables
Local variables are decalred using the locals block. It is a group of key-value pairs that can be used in the configuration. The values can be hard-coded or be a refernce to another variable or resource. It isaccessible within the module/configuration where they declared.
Example -
locals {
ami = "ami-0d26eb3972b7f8c96"
type = "t2.micro"
tags = {
Env = "Dev"
}
subnet = "subnet-76a8163a"
nic = aws_network_interface.my_nic.id
}
resoufce "aws_instance" "myvm" {
ami = local.ami
instance_type = local.type
tags = local.tags
network_interface{
network_interface_id = local.nic
device_index = 0
}
}
resource "aws_network_interface" "my_nic" {
description = "My NIC"
subnet_id = var.subnet
}