Create AWS Resources Based on Conditions Using Terraform

--

Terraform

Sometimes you require that AWS resources be created if specific conditions are fulfilled.

Let’s say you want to create an Amazon S3 bucket only if var var_x = abc or var_x = xyz.

We can do this with the help of a count attribute in Terraform.

Example: sample-bucket will be created only if var_a = abc or var_a = xyz, you can refer below Terraform source code:

resource "aws_s3_bucket" "sample-bucket-resource" {
bucket = "sample-bucket"
acl = "private"
  tags   = {
# add tags if you want
}

count = (var.var_a == "abc" || var.var_a == "xyz") ? 1 : 0
}

Thank you for reading, please follow if you like the article.

Subscribe to stay tuned for upcoming articles.

--

--

Keval Padsumbiya

Competitive Programmer and Software Engineer with experience in Java, Spring Boot, AWS, Jooq, SQL, Terraform etc