Today, we'll dive into dependencies in Terraform and cover two main topics:
- Implicit and Explicit Dependencies
- Variables and TFVars
Terraform automatically handles resource dependencies in two ways:
An implicit dependency occurs when one resource refers to the attribute of another resource. For example, when creating a VPC and then an Internet Gateway, the Internet Gateway doesn't inherently know that it must wait for the VPC to be created. However, when you reference the VPC ID in the Internet Gateway resource, Terraform understands that the VPC must be created first.
- Example:
When you declare a VPC, its ID is generated only after it is created. Any resource, like a subnet or Internet Gateway, that references this VPC ID creates an implicit dependency.
Sometimes, implicit dependencies aren’t enough. For example, if we want the S3 bucket to be created only after the VPC is created, we need to use explicit dependencies. This is done using the depends_on argument in Terraform.
- Example:
A NAT Gateway should only be created after a Route Table has been established. If the NAT Gateway is created before the route table, it won’t function as expected. This is where explicit dependencies come into play usingdepends_on.
- First, we’ll create a VPC and an S3 bucket. Since there's no direct dependency between the VPC and the S3 bucket, Terraform may create the S3 bucket first.
- To enforce order, we’ll explore how to use
depends_onto make sure that resources like the NAT Gateway and S3 bucket are created in the correct sequence.
- Create an
s3.tffile. - In it, define three S3 buckets.
- Observe that the S3 buckets and VPC will deploy in parallel because there is no dependency between them.
To ensure that the S3 bucket is created after the VPC, we’ll add explicit dependencies using the depends_on argument.
Variables allow us to easily change values without editing the code directly. This makes managing infrastructure more flexible and reusable.
Terraform variable values can be stored in separate .tfvars files, helping to:
- Keep the code clean.
- Manage sensitive data or multiple environments efficiently.
Remember to clean up all terraform.tfstate.lock files before redeploying to avoid locking issues.
When replacing resources, Terraform often follows the create before destroy pattern. This ensures minimal downtime by creating a replacement resource before destroying the original.
- Example:
When updating a resource like a Key Pair or upgrading a component, Terraform will first create the new key, then destroy the old one after the new one is functional.
- Deploy the resource.
- Run
terraform planand observe the changes. (Copy the output to a Notepad for reference.) - Deploy the resource.
- Add an additional name to the S3 bucket and reapply the changes to see how Terraform manages updates.
Use prevent_destroy to safeguard critical resources. This is especially useful for resources like databases or sensitive buckets where destruction could cause significant issues.
By the end of this session, you’ll have a deeper understanding of how Terraform handles dependencies, the flexibility of variables, and the best practices for managing infrastructure deployment and updates.