Interview Prep/Terraform/Testing Upgrades and Large-Scale Operations

Write a native test for a module that creates an encrypted S3 bucket.

IntermediateScenarioTesting Upgrades and Large-Scale Operations
Answer

Situation read → Use terraform test at plan level for fast feedback.

What I'd do →

# tests/bucket.tftest.hcl
variables {
  name = "acme-test-bucket"
}

run "bucket_is_encrypted" {
  command = plan

  assert {
    condition     = aws_s3_bucket_server_side_encryption_configuration.this.rule[0].apply_server_side_encryption_by_default[0].sse_algorithm == "aws:kms"
    error_message = "Bucket must use KMS encryption."
  }
}

Run with terraform test.

Why → Plan-mode tests don't create anything, so they run on every PR without cost.

Interview tip → Use mock_provider (1.7+) to avoid needing credentials.

All Terraform questions Roadmap: Terraform in production

More on Testing Upgrades and Large-Scale Operations