- Directory structure
- Writing module files (
main.tf,variables.tf,outputs.tf) - Referencing the module in Terragrunt
- Initializing, planning, and applying changes
- Verifying the S3 bucket
1. Directory Structure
Organize your project by creating a dedicated module folder:| File | Purpose | Description |
|---|---|---|
| main.tf | Resource definition | Defines the aws_s3_bucket resource |
| variables.tf | Input variables | Declares variables like bucket_name |
| outputs.tf | Outputs | Exposes attributes such as bucket name and ARN |
2. Writing the S3 Module
main.tf
Create the S3 bucket using thebucket_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.
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.
Learn more in the Terragrunt documentation.
4. Initialize, Plan, and Apply
Run these commands from the directory containingterragrunt.hcl:
yes when prompted:
5. Verifying the S3 Bucket
Use the AWS CLI to confirm that your bucket exists:Next Steps
- Store your module in a Git repository (e.g., GitHub or GitLab).
- Pin module versions in
terragrunt.hclusing a Git URL and tag. - Expand the module with features like versioning, lifecycle rules, or encryption.