Terraform Types Explained

By Josh Pollara on Feb 15, 2023
Terraform Types Explained

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:

resource "aws_instance" "web_server" {
ami = "ami-0ff8a91507f77f867"
instance_type = "t2.large"
tags = {
Name = "Backend Server"
Environment = "${var.environment}"
}
}

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:

resource "aws_ecs_service" "web_service" {
name = "web-service"
task_definition = "${aws_ecs_task_definition.my_task_definition.id}"
desired_count = 8
load_balancer {
target_group_arn = "${aws_lb_target_group.my_target_group.arn}"
container_name = "my-container"
container_port = 3000
}
}

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:

resource "aws_instance" "web_server" {
ami = "ami-0ff8a91507f77f867"
instance_type = "t2.large"
tags = {
Name = "Backend Server"
Environment = "${var.environment}"
}
disable_api_termination = false
}

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:

variable "security_groups" {
type = list
default = ["sg-9876543210zefdef4", "sg-0987654321fadfba3", "sg-4332614321fqdzba3"]
}
resource "aws_instance" "web_server" {
ami = "ami-0ff8a91507f77f867"
instance_type = "t2.large"
security_groups = "${var.security_groups}"
}

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:

variable "tags" {
type = map
default = {
Name = "web-server"
Environment = "production"
Owner = "team-sre"
}
}
resource "aws_instance" "web_server" {
ami = "ami-0ff8a91507f77f867"
instance_type = "t2.large"
tags = "${var.tags}"
}

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:

resource "aws_db_instance" "my_db" {
engine = "mysql"
instance_class = "db.t2.large"
allocated_storage = 40
storage_type = "gp2"
db_subnet_group_name = "${aws_db_subnet_group.my_db_subnet.name}"
vpc_security_group_ids = ["${aws_security_group.my_db_sg.id}"]
tags = {
Environment = "production"
Application = "my-app"
}
}
output "mydb-endpoint" {
value = "${aws_db_instance.my_db.endpoint}"
}

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.

We use cookies and similar technologies to provide certain features, enhance the user experience and deliver content that is relevant to your interests. Depending on their purpose, analysis and marketing cookies may be used in addition to technically necessary cookies. By clicking on "Agree and continue", you declare your consent to the use of the aforementioned cookies. Here you can make detailed settings or revoke your consent (in part if necessary) with effect for the future. For further information, please refer to our Privacy Policy .