Use `locals`, `terraform.workspace` or tfvars to vary instance size by environment.
IntermediateHands-onWrite-Terraform tasks
Answer
variable "env" { type = string }
locals {
sizes = {
dev = "t3.micro"
staging = "t3.small"
prod = "m6i.large"
}
instance_type = lookup(local.sizes, var.env, "t3.micro")
common_tags = {
Environment = var.env
ManagedBy = "terraform"
}
}
terraform plan -var-file=envs/prod.tfvars
Key trick → A map plus lookup keeps the environment logic in one place. Many teams prefer separate directories or state per environment over workspaces, so a wrong workspace select can't apply dev changes to prod.
More on Write-Terraform tasks
Write Terraform for an S3 bucket with versioning, encryption and public access blocked.BeginnerWrite the remote backend configuration with S3 and state locking.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.Beginner