terraform-aws-modules/terraform-aws-s3-bucket
repository·master·Indexed 20 days ago
https://github.com/terraform-aws-modules/terraform-aws-s3-bucketAn AWS S3 bucket Terraform module providing comprehensive support for S3 features, including versioning, encryption, replication, and specialized log delivery policies. It includes configurations for account-level Public Access Blocks, directory buckets, bucket notifications to Lambda, SQS, and SNS, as well as S3 analytics and inventory configurations.
What's inside terraform-aws-s3-bucket
- This module manages the S3 account-level Public Access Block configuration. Note that each AWS account can only have exactly one S3 Public Access Block configuration. Use this module to enforce security settings across all S3 buckets within a specific AWS account.
Create S3 bucket objects
masterThe
S3 bucket objectmodule allows you to create and manage individual objects within an S3 bucket. You can upload content via literal strings, base64-encoded data, or by specifying a path to a local file. It supports various configurations including encryption (SSE-KMS, AES256), storage classes, object locking, and metadata.# Example usage depends on your specific requirements for content source (content, content_base64, or file_source).What is the table-bucket wrapper module?
masterThe
table-bucketwrapper is a pattern implementation designed to manage multiple instances of the S3 bucket module within a single configuration block. This is particularly useful in environments like Terragrunt where native Terraformfor_eachmight not be feasible for managing multiple resources, allowing you to manage several buckets without duplicating configuration files.Note that this wrapper does not add new functionality; it simply provides a way to pass a map of items to the underlying module.
What is the vectors module wrapper
masterThevectorswrapper is a pattern designed to manage multiple instances of the S3 bucket module within a single configuration block. This is particularly useful in environments like Terragrunt where native Terraformfor_eachlogic might not be feasible or where you want to avoid duplicatingterragrunt.hclfiles for every individual resource. The wrapper itself does not add new functionality; it simply provides a way to pass a map of items to the underlying module.What is the object module wrapper and when to use it
masterThe
wrappers/objectmodule is a pattern implementation designed to manage multiple instances of the core S3 bucket module within a single configuration block. This is particularly useful in environments like Terragrunt where using native Terraformfor_eachmight not be feasible or where you want to avoid duplicatingterragrunt.hclfiles for every individual resource instance.Key Characteristics:
- No extra functionality: The wrapper does not add new AWS features; it only provides a structural way to pass multiple configurations to the underlying module.
- Bulk Management: It allows you to define a set of
defaultsand a map ofitemsto instantiate multiple buckets at once.
What is the notification module wrapper and when to use it
masterThewrappers/notificationmodule implements a single module wrapper pattern. It is designed to allow managing multiple instances of a module within a single configuration block. This is particularly useful in environments like Terragrunt where using native Terraformfor_eachmight not be feasible or where you want to manage multiple resources within a singleterragrunt.hclfile without duplicating files for every resource instance.Attach a custom bucket policy with placeholders
masterUse the
policyargument to attach a custom JSON policy to the bucket. To ensure the policy remains valid even when using bucket prefixes or dynamic naming, you can use the following placeholders in your policy document. The module will replace these with actual values during attachment:_S3_BUCKET_ID__S3_BUCKET_ARN__AWS_ACCOUNT_ID_
Use the account-public-access wrapper for multiple resources
masterThe
account-public-accesswrapper is a pattern used to manage multiple instances of the S3 bucket module within a single configuration block. This is particularly useful in Terragrunt environments where native Terraformfor_eachmight not be feasible, allowing you to manage several resources without duplicating.hclfiles.This wrapper does not add new functionality; it simply provides a way to pass a map of
itemsand a map ofdefaultsto the underlying module.# Concept: One 'defaults' block for shared config, and an 'items' map for specific instances inputs = { defaults = { ... } items = { instance_1 = { ... } instance_2 = { ... } } }How the module wrapper pattern works
masterThe
wrappersdirectory implements a single module wrapper pattern. This pattern is designed for scenarios where native Terraformfor_eachusage is not feasible, such as when using Terragrunt. It allows you to manage multiple instances of the S3 bucket module within a single configuration file (like a singleterragrunt.hcl) instead of duplicating files for every individual bucket.Note that this wrapper does not add any new functionality; it simply provides a way to pass a map of configurations to the underlying root module to instantiate multiple resources.
Configure S3 object content source
masterYou must choose one of the following methods to provide the object's content:
content: A literal string value uploaded as UTF-8-encoded text.content_base64: Base64-encoded data decoded and uploaded as raw bytes. Recommended only for small content (e.g., results ofgzipbase64).file_source: The path to a local file that will be read and uploaded as raw bytes. This is recommended for larger objects.
Migrate from v2.x to v3.x
masterUpgrading from version 2.x to 3.x involves significant changes due to the refactoring of the
aws_s3_bucketresource into multiple smaller resources in the AWS provider.Key Requirements
- AWS Provider Version: You must use AWS provider
v4.5.0or higher. If you are using AWS providerv3.75, the latest supported module version isv3.0.1. - Resource Refactoring: The module now uses several granular resources (e.g.,
aws_s3_bucket_acl,aws_s3_bucket_versioning) instead of a single monolithic resource. To prevent Terraform from recreating existing resources or causing data loss, you must import these resources into your state.
Migration Steps
- Update your module version to
~> 3.0. - Update your
required_providersforawsto>= 4.5. - Run
terraform init -upgradeto download the new provider. - Run the
terraform importcommands listed in the Import existing resources section to map your existing bucket configuration to the new resource structure.
module "s3_bucket" { source = "terraform-aws-modules/s3-bucket/aws" version = "~> 3.0" bucket = "my-awesome-bucket" acl = "log-delivery-write" } terraform { required_providers { aws = ">= 4.5" } }- AWS Provider Version: You must use AWS provider
Use the vectors wrapper with Terragrunt
masterTo use the wrapper in Terragrunt, set the
sourcein yourterraformblock and define two main input maps:defaultsanditems.defaults: A map of arguments applied to every instance created by the wrapper.items: A map where each key represents a unique instance, and the value is a map of arguments specific to that instance. Any argument supported by the base S3 bucket module can be used within these maps.
terraform { source = "tfr:///terraform-aws-modules/s3-bucket/aws//wrappers/vectors" } inputs = { defaults = { create = true tags = { Terraform = "true" Environment = "dev" } } items = { my-item = { # can be any argument supported by the module } my-second-item = { # can be any argument supported by the module } } }