Overview
There are two approaches for creating multiple similar S3 buckets with this module:- Terraform
for_each— available since Terraform 0.13, works natively in any Terraform workflow. - Wrapper module — a thin wrapper in the
wrappers/directory designed for Terragrunt users who want a single configuration file to manage multiple buckets.
Terraform for_each
Use the native
for_each meta-argument inside a module block to create multiple buckets from a single module source.Terragrunt Wrapper
Use the
//wrappers sub-path to pass a map of bucket configurations from a single terragrunt.hcl.Using for_each in a module block
Terraform 0.13 introduced for_each as a module meta-argument. You can use it directly with this module to create several buckets without repeating the module block.
module.s3_buckets["bucket-a"], module.s3_buckets["bucket-b"], etc.
For a more complex scenario where each bucket needs different configuration, use a map:
The wrappers/ directory
The wrappers/ directory contains a thin wrapper module that uses for_each internally. It accepts two variables:
| Variable | Type | Description |
|---|---|---|
items | any | Map of bucket configurations. Each key becomes one bucket. |
defaults | any | Default values applied to every item unless overridden. |
defaults.
Output
The wrapper exposes a singlewrapper output that is a map of all module outputs, keyed by the same keys as items:
Usage with Terraform
Usage with Terragrunt
Terragrunt users can use the wrapper to manage multiple buckets from a singleterragrunt.hcl, avoiding the need for one file per bucket.
- Simple example
When using the wrapper from Terragrunt,
type = any variables such as versioning, lifecycle_rule, and cors_rule must still be wrapped in jsonencode(). See Terragrunt Compatibility for the full list.Accessing wrapper outputs
Because the wrapper emits a singlewrapper output that is a map, you access individual bucket outputs by key:

