Understanding Data Types
Explore different data types in Terraform variables such as string, number, boolean, lists, and maps. Understand how to apply these types in configurations and validate inputs to ensure accurate infrastructure provisioning.
Overview
When we create a variable, it’s important to set the data type so that irrelevant inputs won’t be supplied to the variable. Although we can specify a variable that allows any type of value, it’s important to set a type so that a user doesn’t end up providing a string value for a variable that’s supposed to accept numbers.
Terraform variable types
When working with Terraform variables, we can set different variable types, including string, number, bool(ean), list/tuple, map/object. These variable types are classified into two major sub-categories, which are:
- Primitive types
- Complex types
Variable Types
Primitive Type | Complex Type |
String | List/tuple |
Number | Map/object |
Bool | - |
Primitive types
These are primary data types that aren’t made up of other data types. This type includes string, number, and bool.
String
String variables are used to store text or string values. Let’s consider that we need to create an EC2 instance with a specific AMI ID. We can create an ami_id of type ...