How to use Terraform depends_on

With the rise of cloud computing over the years, the need for automated and fast tools to manage them has become necessary. Terraform is one of these tools.

Terraform, developed by Hashicorp, is an infrastructure as a code utility. It allows you to provision cloud infrastructure in a fast and efficient way. Using Terraform, you can build, modify and destroy cloud resources in a few lines of code. It supports cloud providers such as Amazon AWS, Google Cloud, Microsoft Azure, and more, including custom cloud infrastructure.

This tutorial will teach about the depends_on meta argument you can use when provisioning resources in the cloud.


Before we proceed with the tutorial, you will require to:

  • Be familiar with the Terraform language and use it to create resources.
  • Have Terraform CLI installed and configured on your system.

CAUTION: We will use Google Cloud as the provider for the example configurations and samples. You can also opt for other supported providers. However, the code in this tutorial is not tested on other providers.

What is Terraform Depends_on?

The depends_on is a meta tag that allows you to specify dependencies between resources and modules. For example, you can have a Google cloud instance that depends on a specific bucket. Using the depends_on tag allows Terraform to create or destroy resources correctly. When Terraform sees the depends_on module, it will first create or kill the bucket before performing the actions specified in the instance.

To use the depends_on parameter on module and resource blocks, ensure you have Terraform version 0.13 and above.

Example Use Case

Let us use a simple example to illustrate the depends_on argument in Terraform.

NOTE: We assume you have Terraform CLI installed and applied your configurations for your specific provider.

Start by creating a working directory as:

mkdir terraform-tutorials

Navigate to the created directory:

cd terraform-tutorials

Create a terraform file and call it depends_on.tf

touch depends_on.tf

Next, using your favorite editor, add an example configuration file as shown:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/google"
provider "google" {
  credentials = file("gcp-conf.json")
  project     = "fine-climber-318807"
  region      = "us-central1"
  zone        = "us-central1-c"
resource "google_storage_bucket" "my_archive" {
  name          = "my_archive"
  location      = "us-central1-a"
data "google_compute_image" "my_ubuntu_image" {
  family  = "ubuntu-20"
  project = "fine-climber-318807"
resource "google_compute_instance" "default" {
  name         = "ubuntu-instance"
  machine_type = "e2-micro"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "data.google_compute_image.my_ubuntu_image.self_link"
  scratch_disk {
    interface = "SCSI"
  network_interface {
    network = "default"
    access_config {

  depends_on = [google_storage_bucket.my_archive]

Save the file and close the editor.

In the example Terraform file, we start by setting up the provider like Google and adding all the required credentials.

NOTE: Check the documentation to learn how to set up Terraform for google cloud.

Next, we create a Google cloud bucket and provide all the required parameters. Next, we create a Compute Engine instance that depends on the created storage bucket.

Verify the configuration file contains no syntax error as:

ubuntu@cs:~/terraform-tutorials$ terraform validate
Success! The configuration is valid.

If the configuration is valid, you should see a success message, as shown above.

Run the file to provision the required resources as:

terraform apply

Type “yes” in the prompt to confirm the changes.

If you have the Google Cloud details configured correctly, Terraform will provision the specified resources.

Wrap Up

This tutorial introduces you to the Terraform depends_on parameter and how to create dependencies for resources and modules. If you are new to Terraform, check the documentation to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list