Search⌘ K
AI Features

Create Multiple Resources

Explore how to create multiple instances of the same Terraform resource using count, for_each, and splat operators. Understand how to apply loops and manage resource outputs for scalable infrastructure projects.

Overview

This lesson covers different aspects of creating multiple instances of the same resource in Terraform.

In it, we’ll go over the following concepts:

  • The count resource meta-argument.

-The splat ([*]) operator.

  • The for loop in Terraform.

  • The for_each resource meta-argument.

Importance

These techniques and keywords are essential for a firm understanding of Terraform. This is particularly true for systems and applications that need to scale their resources. For example, we might want to create 100 AWS EC2 instances, or loop over a list of usernames creating accounts for each.

Multiple VPCs

We’ve created the ltthw_multiple directory and the main.tf file. In the main.tf file, we’ve added provider and resource.

variable "region" {
    type= string
}
provider "aws" {
  region = var.region
}
resource "aws_vpc" "aws_vpc_count" {
  count = 3
  cidr_block = format("172.%d.0.0/16", 16 + count.index)
}
Creating multiple VPCs

The count meta-argument looks ...