AWS S3 bucket Terraform module
Terraform module which creates S3 bucket on AWS with all (or almost all) features provided by Terraform AWS provider.
These features of S3 bucket configurations are supported:
- static web-site hosting
- access logging
- versioning
- CORS
- lifecycle rules
- server-side encryption
- object locking
- Cross-Region Replication (CRR)
- ELB log delivery bucket policy
- ALB/NLB log delivery bucket policy
- WAF log delivery bucket policy
- Account-level Public Access Block
- S3 Directory Bucket
- S3 Table Bucket
- S3 Vectors
Usage
Private bucket with versioning enabled
module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-s3-bucket"
acl = "private"
control_object_ownership = true
object_ownership = "ObjectWriter"
versioning = {
enabled = true
}
}
Bucket with ELB access log delivery policy attached
module "s3_bucket_for_logs" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-s3-bucket-for-logs"
acl = "log-delivery-write"
# Allow deletion of non-empty bucket
force_destroy = true
control_object_ownership = true
object_ownership = "ObjectWriter"
attach_elb_log_delivery_policy = true
}
Bucket with ALB/NLB access log delivery policy attached
module "s3_bucket_for_logs" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-s3-bucket-for-logs"
# Allow deletion of non-empty bucket
force_destroy = true
control_object_ownership = true
object_ownership = "ObjectWriter"
attach_elb_log_delivery_policy = true # Required for ALB logs
attach_lb_log_delivery_policy = true # Required for ALB/NLB logs
}
Bucket with WAF log delivery policy attached
module "s3_bucket_for_waf_logs" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-s3-bucket-for-waf-logs"
# Allow deletion of non-empty bucket
force_destroy = true
control_object_ownership = true
object_ownership = "ObjectWriter"
attach_waf_log_delivery_policy = true # Required for WAF logs
}
Bucket with a custom policy attached
When you need to attach a custom policy to the bucket, you can use the policy argument. To keep bucket policy with correct S3 bucket and AWS account properties, you can use the placeholders _S3_BUCKET_ID_, _S3_BUCKET_ARN_, and _AWS_ACCOUNT_ID_ in the policy document. Those values will be replaced with the actual values during the policy attachment. This is especially useful when using bucket prefixes.
Conditional creation
Sometimes you need to have a way to create S3 resources conditionally but Terraform does not allow to use count inside module block, so the solution is to specify argument create_bucket.
# This S3 bucket will not be created
module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
create_bucket = false
# ... omitted
}
Terragrunt and variable "..." { type = any }
There is a bug #1211 in Terragrunt related to the way how the variables of type any are passed to Terraform.
This module solves this issue by supporting jsonencode()-string in addition to the expected type (list or map).
In terragrunt.hcl you can write:
inputs = {
bucket = "foobar" # `bucket` has type `string`, no need to jsonencode()
cors_rule = jsonencode([...]) # `cors_rule` has type `any`, so `jsonencode()` is required
}
Module wrappers
Users of this Terraform module can create multiple similar resources by using for_each meta-argument within module block which became available in Terraform 0.13.
Users of Terragrunt can achieve similar results by using modules provided in the wrappers directory, if they prefer to reduce amount of configuration files.
Examples:
- Complete - Complete S3 bucket with most of supported features enabled
- Cross-Region Replication - S3 bucket with Cross-Region Replication (CRR) enabled
- S3 Notifications - S3 bucket notifications to Lambda functions, SQS queues, and SNS topics.
- S3 Object - Manage S3 bucket objects.
- S3 Analytics - S3 bucket Analytics Configurations.
- S3 Inventory - S3 bucket Inventory configuration.
- S3 Account-level Public Access Block - Manage S3 account-level Public Access Block.
- S3 Directory Bucket - S3 Directory Bucket configuration.
- S3 Table Bucket - S3 Table Bucket configuration.
- S3 Vectors - S3 Vectors vector bucket with indexes configuration.
Authors
Module is maintained by Anton Babenko with help from these awesome contributors.
License
Apache 2 Licensed. See LICENSE for full details.
Additional information for users from Russia and Belarus
- Russia has illegally annexed Crimea in 2014 and brought the war in Donbas followed by full-scale invasion of Ukraine in 2022.
- Russia has brought sorrow and devastations to millions of Ukrainians, killed hundreds of innocent people, damaged thousands of buildings, and forced several million people to flee.
- Putin khuylo!