Import an existing, manually created S3 bucket into Terraform.
AdvancedHands-onWrite-Terraform tasks
Answer
import {
to = aws_s3_bucket.legacy
id = "legacy-reports-bucket"
}
resource "aws_s3_bucket" "legacy" {
bucket = "legacy-reports-bucket"
}
terraform plan -generate-config-out=generated.tf # optional: let Terraform write the resource
terraform apply
Key trick → The import block (Terraform 1.5+) is code-reviewed and planned like any other change, unlike the CLI terraform import. Aim for a plan showing "1 to import, 0 to change".
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