Type Constraints - Map
We will learn how to use Map type constraints in your Terraform project.
We'll cover the following
Map#
A map is a set of values indexed by key name. You can give a map a type, which will be the type of the values.
Project example#
Consider the following code:
In this project, we are creating a map of type number
. We are initialising the map to have two keys: alpha and bravo. The values for the keys are 2
and 3
respectively. The fact that we have specified
that the map is of type (number)
means that all of the values have to match the number constraint.
Running the project#
Clicking the terminal will run terraform init
and terraform apply
commands. When prompted, type yes
and press enter.
Map output value#
The map output value will print the whole map. We will also select a value out of a map by key using the []
syntax. The alpha_value
output will
print the value for the alpha key in the map, which will be 2
.
Feel free to run the project and get a feel for how the code is working.
📝Note: Once you are done with the project, do not forget to run
terraform destroy
and then confirm with yes to delete all of the resources in this project.