0
0
Terraformcloud~30 mins

State disaster recovery in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
State Disaster Recovery with Terraform
📖 Scenario: You are managing infrastructure using Terraform. To protect your infrastructure state from accidental loss or corruption, you want to set up a remote backend with state locking and versioning.This helps you recover your infrastructure state if something goes wrong.
🎯 Goal: Set up a Terraform backend configuration that stores state remotely in an AWS S3 bucket with state locking using DynamoDB.This ensures your Terraform state is safe and recoverable in case of disasters.
📋 What You'll Learn
Create a Terraform backend configuration using AWS S3
Enable state locking with DynamoDB table
Specify the S3 bucket name as my-terraform-state-bucket
Specify the DynamoDB table name as terraform-lock-table
Set the AWS region to us-east-1
💡 Why This Matters
🌍 Real World
Terraform state files hold the current infrastructure status. Losing or corrupting them can cause deployment failures or resource duplication. Using remote state with locking prevents concurrent changes and protects state files.
💼 Career
Cloud engineers and DevOps professionals must ensure infrastructure state is safely stored and recoverable. This knowledge is essential for managing infrastructure as code in real environments.
Progress0 / 4 steps
1
Create the S3 bucket resource
Create a Terraform resource called aws_s3_bucket named terraform_state_bucket with the bucket name set to "my-terraform-state-bucket".
Terraform
Need a hint?

Use resource "aws_s3_bucket" "terraform_state_bucket" {} and set the bucket attribute.

2
Create the DynamoDB table resource for state locking
Create a Terraform resource called aws_dynamodb_table named terraform_lock_table with the table name set to "terraform-lock-table", billing mode set to "PAY_PER_REQUEST", and a hash key named LockID of type "S".
Terraform
Need a hint?

Use resource "aws_dynamodb_table" "terraform_lock_table" {} and define the name, billing_mode, hash_key, and attribute block.

3
Configure the Terraform backend
Add a terraform block with a backend "s3" configuration that sets bucket to "my-terraform-state-bucket", key to "terraform.tfstate", region to "us-east-1", and dynamodb_table to "terraform-lock-table".
Terraform
Need a hint?

Use a terraform block with a backend "s3" block inside. Set the required attributes exactly as specified.

4
Add provider configuration for AWS
Add a Terraform provider "aws" block with the region set to "us-east-1".
Terraform
Need a hint?

Add a provider "aws" block and set the region attribute.