Task

Provision S3 Buckets using Terraform Count

Terraform · 15 min · Practical

Scale S3 bucket infrastructure dynamically with Terraform's count meta-argument. Learn how to iterate over numeric values with count.index and list variables using length(), exporting all created bucket names with splat syntax ([*]).

Core Implementation Highlights

Numeric Count Loopcount = var.countfors3 (3 buckets)
Index Interpolationbucket = "shan-count-bucket-${count.index}"
List-Length Iterationcount = length(var.coutntwithname)
Splat Output Queryvalue = aws_s3_bucket.counttask[*].bucket

The Complete Terraform Code

Save the code below into main.tf. It defines two distinct count iteration patterns: numeric loop with index suffixing, and list-driven iteration matching custom bucket names.

main.tf
# Numeric variable controlling number of S3 buckets
variable "countfors3" {
  default = 3
}

# Create multiple S3 buckets using numeric count loop and count.index
resource "aws_s3_bucket" "counttask" {
  count  = var.countfors3
  bucket = "shan-count-bucket-${count.index}"
}

# Output list of created bucket names using splat syntax
output "s3_bucket_name" {
  value = aws_s3_bucket.counttask[*].bucket
}

# List variable containing explicit bucket names
variable "coutntwithname" {
  default = ["shannewbucketcount1", "shannewbucketcount2"]
}

# Create S3 buckets based on list length and index lookup
resource "aws_s3_bucket" "countwithnameresource" {
  count  = length(var.coutntwithname)
  bucket = var.coutntwithname[count.index]
}

# Output list of named S3 buckets using splat syntax
output "s3_bucket_name_count" {
  value = aws_s3_bucket.countwithnameresource[*].bucket
}

Deep Dive Explanation

Numeric Count & Index Interpolation

The variable countfors3 sets how many instances to create. The meta-argument count = var.countfors3 instructs Terraform to provision 3 identical resources. We use ${count.index} (which evaluates to 0, 1, 2) to give each S3 bucket a unique name: shan-count-bucket-0, shan-count-bucket-1, and shan-count-bucket-2.

main.tf
resource "aws_s3_bucket" "counttask" {
  count  = var.countfors3
  bucket = "shan-count-bucket-${count.index}"
}

List Length & Specific Bucket Names

Instead of hardcoding a number, length(var.coutntwithname) dynamically counts how many items are in the list variable. Terraform iterates through the list using var.coutntwithname[count.index], creating buckets with custom names: shannewbucketcount1 and shannewbucketcount2.

main.tf
resource "aws_s3_bucket" "countwithnameresource" {
  count  = length(var.coutntwithname)
  bucket = var.coutntwithname[count.index]
}

Splat Expressions ([*]) for Outputs

When a resource uses count, Terraform creates a list of resource instances indexed by position (e.g. aws_s3_bucket.counttask[0]). The splat syntax [*] queries every item in that list and returns an array containing all bucket names.

outputs.tf
output "s3_bucket_name" {
  value = aws_s3_bucket.counttask[*].bucket
}

Deployment & Command Execution

terminal
# 1. Initialize AWS provider
$ terraform init

# 2. Validate configuration syntax
$ terraform validate
$ terraform fmt

# 3. Preview plan (will show 5 buckets to create)
$ terraform plan

# 4. Provision infrastructure in AWS
$ terraform apply -auto-approve

# 5. Output results
Outputs:
s3_bucket_name = [
  "shan-count-bucket-0",
  "shan-count-bucket-1",
  "shan-count-bucket-2",
]
s3_bucket_name_count = [
  "shannewbucketcount1",
  "shannewbucketcount2",
]

# 6. Clean up resources when done
$ terraform destroy -auto-approve