Skip to main content
This lesson walks through deploying S3 buckets with Terraform. The repository includes a root configuration that configures the AWS provider, generates a short random ID for unique bucket names, creates one bucket directly, and uses a reusable module for a second bucket. The same Terraform code creates reproducible cloud resources using HashiCorp Configuration Language (HCL).

Repository layout (highlight)

  • Root main.tf — provider, random_id, one aws_s3_bucket resource, and a module call.
  • modules/s3_bucket_with_env_tag/ — a simple module that creates a bucket and applies an env tag.
  • Terraform state and plan artifacts are created when you run terraform init and terraform apply.

Root configuration (abbreviated)

Root main.tf:
Key points:
  • provider "aws" sets the AWS region.
  • random_id.bucket_id provides a short hex suffix so bucket names are globally unique.
  • object_lock_enabled = true enables S3 Object Lock at bucket creation (see important notes below).
  • The module block reuses modules/s3_bucket_with_env_tag and passes env and name inputs.

Module: modules/s3_bucket_with_env_tag

modules/s3_bucket_with_env_tag/main.tf:
modules/s3_bucket_with_env_tag/variables.tf:
This module:
  • Creates a bucket with the provided name.
  • Enables object lock on creation.
  • Applies an env tag set to the supplied env value (validated to be either dev or prod).
Important: Amazon S3 requires versioning to be enabled on a bucket to use Object Lock. In Terraform you should add a versioning block inside the bucket resource when enabling object lock:

Notes and caveats

  • Object Lock must be enabled at bucket creation and cannot be disabled later. Plan accordingly for retention and compliance.
  • Using a random_id or other unique suffix avoids global name collisions for S3 buckets.
  • The env tag applied by the module helps with cost allocation and filtering in the AWS console.
Buckets created with object lock enabled are configured at creation time and cannot have object lock disabled later. Ensure you understand retention and compliance requirements before enabling this feature.

What resources will be created?

Deploying the Terraform configuration

  1. Change into the Terraform directory and initialize:
  1. Apply the configuration:
Terraform will present a plan and prompt for confirmation:
Type yes to proceed. After the apply finishes, refresh the S3 console to confirm that the buckets exist and include the random ID suffix.
A screenshot of the AWS S3 console showing a green success banner for creating the bucket "console-demo-bucket-2-1234" and the "General purpose buckets" list. The table shows two buckets with their names, AWS region (US East N. Virginia) and creation dates.
If you inspect the second bucket’s Properties, you should see the env tag set to dev (as passed into the module) and the default encryption and MFA delete settings.
Screenshot of an AWS S3 bucket settings page. It shows a tag "env: dev", default server-side encryption using Amazon S3 managed keys (SSE‑S3), and MFA delete disabled.

Summary

This example demonstrates infrastructure-as-code with Terraform:
  • Declarative HCL creates reproducible AWS S3 resources.
  • Modules encapsulate reusable patterns (here, a bucket with an env tag).
  • Use random_id or other uniqueness strategies for globally unique S3 names.
  • Remember to enable versioning whenever you enable object_lock_enabled.

Watch Video