0
0
Terraformcloud~20 mins

Terraform apply -replace flag - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Terraform apply with the -replace Flag
📖 Scenario: You are managing cloud infrastructure using Terraform. Sometimes, you need to force Terraform to recreate a specific resource without affecting others. This is useful when a resource is in a bad state or needs to be refreshed.
🎯 Goal: Learn how to use the terraform apply -replace command to recreate a specific resource while applying changes.
📋 What You'll Learn
Create a Terraform configuration with a resource
Add a variable to specify the resource to replace
Write a command to apply changes with the -replace flag
Complete the Terraform command with the correct resource address
💡 Why This Matters
🌍 Real World
In real cloud projects, sometimes resources get corrupted or need to be recreated without changing the entire infrastructure. The '-replace' flag helps target only those resources.
💼 Career
Cloud engineers and DevOps professionals use Terraform and the '-replace' flag to manage infrastructure safely and efficiently, minimizing downtime and errors.
Progress0 / 4 steps
1
Create a Terraform configuration with an AWS S3 bucket
Create a Terraform file named main.tf with a resource called aws_s3_bucket.my_bucket that creates an S3 bucket named my-unique-bucket-12345.
Terraform
Need a hint?

Use the resource block with type aws_s3_bucket and name my_bucket. Set the bucket attribute to the exact bucket name.

2
Add a variable to specify the resource to replace
Add a variable called replace_resource of type string with a default value of "aws_s3_bucket.my_bucket" in a file named variables.tf.
Terraform
Need a hint?

Use the variable block with the name replace_resource. Set the type to string and default to the resource address.

3
Write the Terraform apply command with the -replace flag
Write a shell command that runs terraform apply and uses the -replace flag with the value of the variable replace_resource.
Terraform
Need a hint?

Use the command terraform apply -replace="${var.replace_resource}" to force replacement of the specified resource.

4
Complete the Terraform command with the correct resource address
Modify the terraform apply command to replace the resource aws_s3_bucket.my_bucket explicitly by writing terraform apply -replace="aws_s3_bucket.my_bucket".
Terraform
Need a hint?

Write the full command terraform apply -replace="aws_s3_bucket.my_bucket" to force recreate the bucket resource.