Write the remote backend configuration with S3 and state locking.
BeginnerHands-onWrite-Terraform tasks
Answer
terraform {
required_version = ">= 1.10"
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
backend "s3" {
bucket = "devopsshack-tfstate"
key = "network/prod/terraform.tfstate"
region = "ap-south-1"
encrypt = true
use_lockfile = true
}
}
Key trick → use_lockfile = true (Terraform 1.10+) uses S3's native conditional writes for locking, so a DynamoDB table is no longer needed (dynamodb_table is the older method and is deprecated). Backend blocks can't use variables.
More on Write-Terraform tasks
Write Terraform for an S3 bucket with versioning, encryption and public access blocked.BeginnerWrite a VPC with public and private subnets across 3 AZs using `count` or `for_each`.IntermediateWrite a reusable module for an EC2 instance with inputs, outputs and validation.AdvancedWrite a security group that allows HTTPS from anywhere and SSH only from an office CIDR.BeginnerCreate multiple IAM users from a list with `for_each`, and output their ARNs as a map.BeginnerWrite Terraform that looks up an existing VPC by tag instead of hardcoding its ID.Intermediate