Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Terraform Tutorials: How to set condition in Terraform Program?

Method 1: conditional expression

Terraform doesn’t support if statements. Luckily we can achieve the same result by using a specific parameter called count.
You can think about it this way: you can set count to 1 on a specific resource and get one copy of that resource.
However, setting the same count parameter to 0 you won’t get any resource created.

If condition is true then the result is true_val. If condition is false then the result is false_val.


<CONDITION> ? <TRUE_VAL> <FALSE_VAL>Code language: HTML, XML (xml)

resource "aws_instance" "example" {
  ami           = "ami-053b0d53c279acc90"
  instance_type = "t2.micro"
  count         = var.create_instance ? 1 : 0

  tags = {
    Name = "My EC2 Instance"
  }
}
Code language: JavaScript (javascript)

Method 2: Depends_on

Implicit dependencies are the primary way that Terraform understands the relationships between your resources. Sometimes there are dependencies between resources that are not visible to Terraform, however. The depends_on argument is accepted by any resource or module block and accepts a list of resources to create explicit dependencies for.

Add the following to main.tf. Since both the instance and the SQS Queue are dependent upon the S3 Bucket, Terraform waits until the bucket is created to begin creating the other two resources.



variable "instance_type" {
  default = "t2.micro"
}

resource "aws_instance" "example" {
  ami           = "ami-053b0d53c279acc90"
  instance_type = var.instance_type

  tags = {
    Name = "My EC2 Instance"
  }
}

resource "aws_s3_bucket" "example_bucket" {
  depends_on = [aws_instance.example]
  bucket     = "my-unique-bucket-name"
  acl        = "private"
}Code language: JavaScript (javascript)

resource "aws_s3_bucket" "example" {
  acl    = "private"
}

resource "aws_instance" "example_c" {
  ami           = data.aws_ami.amazon_linux.id
  instance_type = "t2.micro"

  depends_on = [aws_s3_bucket.example]
}

module "example_sqs_queue" {
  source  = "terraform-aws-modules/sqs/aws"
  version = "2.1.0"

  depends_on = [aws_s3_bucket.example, aws_instance.example_c]
}Code language: JavaScript (javascript)

Example code for terraform calling multiple modules in dependson


# main.tf
module "module1" {
  source = "./module1"
}

module "module2" {
  source = "./module2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  depends_on = [
    module.module1,
    module.module2
  ]
}
Code language: PHP (php)

In this example, we have two modules, module1 and module2, and an EC2 instance defined in the aws_instance resource. The depends_on argument specifies that the instance should be created only after both module1 and module2 have been created.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Terraform Tutorials: Local Values using Local Block

What is local value in terraform? In Terraform, a locals block is used to define local variables within a module, allowing you to create reusable expressions and…

Read More

Terraform Tutorials: Module Complete Guide

A Terraform module is a collection of configuration files that encapsulate resources used together to achieve a specific outcome. Modules promote reusability, organization, and maintainability in infrastructure…

Read More

What is Terrafile?

Terrafile is a tool used to manage Terraform modules as dependencies. It simplifies the process of downloading and managing Terraform modules by automating the fetching of modules…

Read More

What is Terraform and use cases of Terraform?

What is Terraform? Terraform is a infrastructure as code (IaC) tool and open-source from HashiCorp. It empowers users to define and manage cloud infrastructure and on-premises resources…

Read More

What is HashiCorp Terraform and use cases of HashiCorp Terraform?

What is HashiCorp Terraform? HashiCorp Terraform is a leading infrastructure as code (IaC) tool, empowering you to define, provision, and manage your cloud and on-premises infrastructure in…

Read More

Terraform Tutorials: What is terraform.tfvars with example

What is terraform.tfvars? Terraform .tfvars files are used to store variable definitions. This allows you to externalize your variable definitions and makes it easier to manage them,…

Read More