Terraform Types Explained
Introduction
Terraform is based on a declarative language called HCL that allows users to define infrastructure resources in a human-readable format. In this blog post, I will explain the different types available in Terraform and how they are useful.
Strings
Strings are a sequence of characters enclosed in double quotes. Strings are used to represent text-based values like resource names, labels, and descriptions. Strings can be combined using string concatenation to create more complex values.
For example, a string can be defined as follows:
In this example, the Name
and Environment
tags are defined as strings.
Numbers
Numbers are used to represent numeric values like port numbers, resource counts, and memory sizes. Numbers can be defined as integers or floating-point numbers.
For example, a number can be defined as follows:
In this example, the desired_count
and container_port
values are defined as integers.
Booleans
Booleans are used to represent true or false values using the keywords true
or false
.
For example, a boolean can be defined as follows:
In this example, the disable_api_termination
value is set to false
, which means that the instance can be terminated using the AWS EC2 API.
Lists
Lists are used to represent ordered sequences of values. Lists are defined using square brackets []
.
For example, a list can be defined as follows:
In this example, the security_groups
variable is defined as a list of security group IDs.
Maps
Maps are used to represent key-value pairs. Maps are defined using curly braces {}
.
For example, a map can be defined as follows:
In this example, the tags
variable is defined as a map of key-value pairs.
Objects
Objects, similar to maps, can represent key-value pairs. Objects have additional functionality that allow them to be used as a data structure. Objects are defined using curly braces {}
and the .
operator is used to access object properties.
For example, you can define an object as follows:
In this example, the aws_db_instance
resource is defined as an object with properties engine
, instance_class
, and allocated_storage
.
Conclusion
Terraform provides many data types that can be used to define infrastructure resources in a human-readable format. Understanding the different types available in Terraform and how they can be used is crucial when creating efficient infrastructure code.