terraform-aws-modules/terraform-aws-s3-bucket

repository·master·Indexed 20 days ago

https://github.com/terraform-aws-modules/terraform-aws-s3-bucket

An 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.

Tokens
24.4K
Snippets
44
Records
93
Agent score
65%

What's inside terraform-aws-s3-bucket

  1. Create S3 bucket objects

    master

    The S3 bucket object module 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).
  2. What is the table-bucket wrapper module?

    master

    The table-bucket wrapper 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 Terraform for_each might 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.

  3. What is the vectors module wrapper

    master
    The vectors wrapper 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 Terraform for_each logic might not be feasible or where you want to avoid duplicating terragrunt.hcl files 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.
  4. What is the object module wrapper and when to use it

    master

    The wrappers/object module 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 Terraform for_each might not be feasible or where you want to avoid duplicating terragrunt.hcl files 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 defaults and a map of items to instantiate multiple buckets at once.
  5. What is the notification module wrapper and when to use it

    master
    The wrappers/notification module 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 Terraform for_each might not be feasible or where you want to manage multiple resources within a single terragrunt.hcl file without duplicating files for every resource instance.
  6. Attach a custom bucket policy with placeholders

    master

    Use the policy argument 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_
  7. Use the account-public-access wrapper for multiple resources

    master

    The account-public-access wrapper 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 Terraform for_each might not be feasible, allowing you to manage several resources without duplicating .hcl files.

    This wrapper does not add new functionality; it simply provides a way to pass a map of items and a map of defaults to the underlying module.

    # Concept: One 'defaults' block for shared config, and an 'items' map for specific instances
    inputs = {
      defaults = { ... }
      items = {
        instance_1 = { ... }
        instance_2 = { ... }
      }
    }
  8. How the module wrapper pattern works

    master

    The wrappers directory implements a single module wrapper pattern. This pattern is designed for scenarios where native Terraform for_each usage 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 single terragrunt.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.

  9. Configure S3 object content source

    master

    You must choose one of the following methods to provide the object's content:

    1. content: A literal string value uploaded as UTF-8-encoded text.
    2. content_base64: Base64-encoded data decoded and uploaded as raw bytes. Recommended only for small content (e.g., results of gzipbase64).
    3. file_source: The path to a local file that will be read and uploaded as raw bytes. This is recommended for larger objects.
  10. Migrate from v2.x to v3.x

    master

    Upgrading from version 2.x to 3.x involves significant changes due to the refactoring of the aws_s3_bucket resource into multiple smaller resources in the AWS provider.

    Key Requirements

    • AWS Provider Version: You must use AWS provider v4.5.0 or higher. If you are using AWS provider v3.75, the latest supported module version is v3.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

    1. Update your module version to ~> 3.0.
    2. Update your required_providers for aws to >= 4.5.
    3. Run terraform init -upgrade to download the new provider.
    4. Run the terraform import commands 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"
      }
    }
  11. Use the vectors wrapper with Terragrunt

    master

    To use the wrapper in Terragrunt, set the source in your terraform block and define two main input maps: defaults and items.

    • 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
        }
      }
    }