Skip to main content
In this tutorial, you’ll learn how to build a reusable Terraform module for an AWS S3 bucket and then consume it with Terragrunt. You’ll get hands-on experience with:
  1. Directory structure
  2. Writing module files (main.tf, variables.tf, outputs.tf)
  3. Referencing the module in Terragrunt
  4. Initializing, planning, and applying changes
  5. Verifying the S3 bucket

1. Directory Structure

Organize your project by creating a dedicated module folder:
Your workspace should resemble:

2. Writing the S3 Module

main.tf

Create the S3 bucket using the bucket_name variable:

variables.tf

Declare the bucket name as a required input:
S3 bucket names must be globally unique and comply with AWS naming rules.
Avoid uppercase letters and underscores.

outputs.tf

Expose both the bucket name and ARN:

3. Consuming the Module with Terragrunt

In your root or environment-specific folder, configure Terragrunt to source the S3 module:
Terragrunt lets you keep your Terraform code DRY by abstracting common configurations.
Learn more in the Terragrunt documentation.

4. Initialize, Plan, and Apply

Run these commands from the directory containing terragrunt.hcl:
Sample plan output:
Apply your changes:
Confirm with yes when prompted:

5. Verifying the S3 Bucket

Use the AWS CLI to confirm that your bucket exists:
Example:

Next Steps

  • Store your module in a Git repository (e.g., GitHub or GitLab).
  • Pin module versions in terragrunt.hcl using a Git URL and tag.
  • Expand the module with features like versioning, lifecycle rules, or encryption.

Watch Video