Before I get started, you can go find my code in my repo at this link.

This bucket module is going to be made of a few different files.

  1. Main.tf — for configuration
  2. Variables.tf — for variables
  3. Outputs.tf — for outputs

First we will take a look at the main.tf configuration.

Main.tf File

resource "aws_s3_bucket" "b" {
  bucket_prefix = var.bucket_prefix
  acl    = var.acl

versioning {
        enabled = var.versioning
    }
logging {
        target_bucket = var.target_bucket
        target_prefix = var.target_prefix
    }
server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        kms_master_key_id = var.kms_master_key_id
        sse_algorithm     = var.sse_algorithm
      }
    }
  }
tags = var.tags
}

We are going to do a couple things here that I want to note. First, we will be setting variables for every argument so that we can create some defaults. Second, we are choosing to use the bucket_prefix argument rather than the bucket argument. That way we don’t accidentally try to create a bucket with the same name as one that already exists in the global namespace.

When we use bucket_prefix it would be best to name the bucket something like **my-bucket- **that way the string added to the end of the bucket name comes after the dash.

#terraform #devops #aws #hashicorp-terraform #aws-s3

Creating an S3 Bucket Module in Terraform
2.55 GEEK