Write Terraform for an S3 bucket with versioning, encryption and public access blocked.
BeginnerHands-onWrite-Terraform tasks
Answer
resource "aws_s3_bucket" "artifacts" {
bucket = "devopsshack-artifacts-${var.env}"
}
resource "aws_s3_bucket_versioning" "artifacts" {
bucket = aws_s3_bucket.artifacts.id
versioning_configuration { status = "Enabled" }
}
resource "aws_s3_bucket_server_side_encryption_configuration" "artifacts" {
bucket = aws_s3_bucket.artifacts.id
rule {
apply_server_side_encryption_by_default { sse_algorithm = "aws:kms" }
}
}
resource "aws_s3_bucket_public_access_block" "artifacts" {
bucket = aws_s3_bucket.artifacts.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
Key trick → Since AWS provider v4, versioning, encryption and the rest are separate resources, not inline blocks on aws_s3_bucket. Interviewers check whether you know the current provider style.
More on Write-Terraform tasks
Write 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.BeginnerWrite Terraform that looks up an existing VPC by tag instead of hardcoding its ID.Intermediate