Understanding Data Types
Explore the different data types available in Terraform variables, including strings, numbers, booleans, lists, and maps. Understand how to enforce data validation rules for variables using the HashiCorp Configuration Language to prevent input errors. Gain hands-on experience by creating and validating variables to configure resources like EC2 instances on AWS using Terraform.
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 ...