In this post, we will discuss a use case where we want to access the variable value which is part of the list or the map.
let’s try to understand the use case with an example:
resource "aws_instance" "myec2" {
ami = "ami-082b5a644766e0e6f"
instance_type = <INSTANCE_TYPE>
}variable "list" {
type = list
default = ["t2.nano", "t2.micro", "t2.medium"]
}variable "types" {
type = map
default = {
dev = "t2.nano",
int = "t2.micro",
prod = "t2.medium"
},
}
here we want to assign a value for the instance_type from either variable list or from variable types. Variable list is a list type and variable type is a map type.
First, let’s use the list variable. To access the value from the list variable we will use the position. We want to assign let’s say t2.micro in that case we want position 1.
resource "aws_instance" "myec2" {
ami = "ami-082b5a644766e0e6f"
instance_type = var.list[1]
}
Similarly for the map type:
resource "aws_instance" "myec2" {
ami = "ami-082b5a644766e0e6f"
instance_type = var.types["us-east-1"]
}
In the map type, we refer to the key.
Comments
Post a Comment