We've seen how to declare and use default values in our Terraform code, such as the Ubuntu AMI for our region or our VM size. An interesting feature in Terraform is the ability to declare and use maps of values, so, depending on a key, the variable can have a different value. We'll see how it applies to the correct AMI of the corresponding AWS.
To step through this recipe, you will need the following:
A working Terraform installation
An AWS provider and an EC2 instance (using a SSH key pair and a security group), all configured in Terraform (refer to the previous recipes)
An Internet connection
Here's how we simply declared the AMI we wanted for the eu-west-1
region in the variables.tf
file:
variable "ami" { default = "ami-ee6b189d" }
We accessed it easily like this in the instances.tf
file:
ami = "${var.ami}"
A similar, but more explicit way would be to use a map, so we know which region the value refers to:
variable "ami" { default...