Create resource dependencies
In this tutorial, you will learn about dependencies between resources and
modules. Most of the time, Terraform infers dependencies between resources based
on the configuration given, so that resources are created and destroyed in the
correct order. Occasionally, however, Terraform cannot infer dependencies
between different parts of your infrastructure, and you will need to create an
explicit dependency with the depends_on
argument.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the Terraform Community Edition tab to complete this tutorial using Terraform Community Edition.
This tutorial assumes that you are familiar with the Terraform and HCP Terraform workflows. If you are new to Terraform, complete the Get Started tutorials first. If you are new to HCP Terraform, complete the HCP Terraform Get Started tutorials first.
For this tutorial, you will need:
- Terraform v1.2+ installed locally.
- an HCP Terraform account and organization.
- HCP Terraform locally authenticated.
- the AWS CLI.
- an HCP Terraform variable set configured with your AWS credentials.
Clone example repository
Clone the example repository for this tutorial.
Initialize workspace
Set the TF_CLOUD_ORGANIZATION
environment variable to your HCP Terraform
organization name. This will configure your HCP Terraform integration.
Initialize your configuration. Terraform will automatically create the
learn-terraform-dependencies
workspace in your HCP Terraform organization.
Note
This tutorial assumes that you are using a tutorial-specific HCP Terraform organization with a global variable set of your AWS credentials. Review the Create a Credential Variable Set for detailed guidance. If you are using a scoped variable set, assign it to your new workspace now.
Manage implicit dependencies
The most common source of dependencies is an implicit dependency between two resources or modules.
Review the configuration in main.tf
. It declares two EC2 instances and an
Elastic IP address.
The aws_eip
resource type allocates and associates an Elastic
IP to an EC2 instance.
Since the instance must exist before the Elastic IP can be created and attached,
Terraform must ensure that aws_instance.example_a
is created before it creates
aws_eip.ip
. Meanwhile, aws_instance.example_b
can be created in parallel to
the other resources.
Apply the configuration. Respond to the confirmation prompt with a yes
.
Terraform provisions your resources in order, and reports on its progress as it deploys your resources. The output will be similar to the following.
As shown above, Terraform waited until the creation of EC2 instance example_a
was complete before creating the Elastic IP address.
Terraform automatically infers when one resource depends on another by studying
the resource attributes used in interpolation expressions. In the example
above, the reference to aws_instance.example_a.id
in the definition of the
aws_eip.ip
block creates an implicit dependency.
Terraform uses this dependency information to determine the correct order in which to create the different resources. To do so, it creates a dependency graph of all of the resources defined by the configuration. In the example above, Terraform knows that the EC2 Instance must be created before the Elastic IP.
Manage explicit dependencies
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.
To illustrate this, assume you have an application running on your EC2 instance
that expects to use a specific Amazon S3 bucket. This dependency is configured
inside the application, and thus not visible to Terraform. You can use
depends_on
to explicitly declare the dependency. You can also specify multiple
resources in the depends_on
argument, and Terraform will wait until all of
them have been created before creating the target resource.
Tip
Since Terraform will wait to create the dependent resource until after the specified resource is created, adding explicit dependencies can increase the length of time it takes for Terraform to create your infrastructure.
Add the following to main.tf
.
Tip
The order in which resources are declared in your configuration files has no effect on the order in which Terraform creates or destroys them.
This configuration includes a reference to a new module,
terraform-aws-modules/sqs/aws
. Modules must be installed before Terraform can
use them.
Run terraform get
to install the module.
Now run terraform apply
to apply the changes. Respond to the confirmation
prompt with a yes
.
Terraform will print output similar to the following.
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.
Destroy resources
Both implicit and explicit dependencies affect the order in which resources are destroyed as well as created.
Clean up the resources you created in this tutorial using Terraform. Respond to
the confirmation prompt with a yes
.
Notice that the SQS Queue, Elastic IP address, and the example_c
EC2 instance
are destroyed before the resources they depend on are.
Terraform will print output similar to the following.
If you used HCP Terraform for this tutorial, after destroying your resources,
delete the learn-terraform-dependencies
workspace from your HCP Terraform
organization.
Next steps
Now that you understand how to manage implicit and explicit dependencies, explore the following resources.
- Read the Terraform documentation for the depends_on meta-argument.
- Read how Terraform uses the dependency graph to manage infrastructure.
- Learn how to provision infrastructure created with Terraform.