Interview Prep/Real-World Interview/Write-Terraform tasks

Write a `terraform test` file that asserts the bucket name follows a convention.

AdvancedHands-onWrite-Terraform tasks
Answer
# tests/bucket.tftest.hcl
variables {
  env = "dev"
}

run "bucket_name_convention" {
  command = plan

  assert {
    condition     = startswith(aws_s3_bucket.artifacts.bucket, "devopsshack-")
    error_message = "Bucket name must start with devopsshack-"
  }
  assert {
    condition     = aws_s3_bucket_versioning.artifacts.versioning_configuration[0].status == "Enabled"
    error_message = "Versioning must be enabled"
  }
}

Run with terraform test.

Key trick → The native test framework (Terraform 1.6+) runs plan-only tests with no real resources created, which is fast enough for every PR. command = apply gives full integration tests against a sandbox account.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-Terraform tasks