Terraform module that creates a secure S3 bucket with the specified configurations, including options for access logging, custom policies, and additional lifecycle rules.
1. Install Terraform.
2. Ensure you have access to AWS S3 services (and that your AWS profile is configured locally or on your server).
- To create an S3 bucket, use the code below:
Example:
module "s3-bucket" {
source = "git::https://github.com/RubenLopSol/Module_s3.git"
region = var.region
project_name = var.project_name
bucket_name = var.bucket_name
enable_access_logging = true
logging_bucket = var.logging_bucket_name
logging_bucket_name = var.logging_bucket_name
environment = var.environment
lifecycle_rules = [
{
id = "delete-old-logs",
enabled = true,
prefix = "logs/",
expiration = {
days = 365
}
}
]
}
- The logging bucket needs to be created beforehand (must exist).
- In your
variables.tf
file, set the variables that the module requires.
- Run
terraform init
to initialize the Terraform environment and download the module dependencies.
terraform init
- Apply the configuration to create your S3 bucket by running.
terraform apply
Once the terraform apply
completes, your S3 bucket will be created with the following:
- Logging enabled (if configured).
- Lifecycle rules applied for the logs folder.
Name | Type |
---|---|
aws_s3_bucket.secure_bucket | resource |
aws_s3_bucket_lifecycle_configuration.lifecycle | resource |
aws_s3_bucket_logging.logging | resource |
aws_s3_bucket_policy.secure_bucket_policy | resource |
aws_s3_bucket_public_access_block.public_access_block | resource |
aws_s3_bucket_server_side_encryption_configuration.sse | resource |
aws_s3_bucket_versioning.versioning | resource |
Name | Description | Type | Default | Required |
---|---|---|---|---|
region | Value for AWS region | string |
n/a | yes |
environment | Value for tipe of environment | string |
n/a | yes |
project_name | Value for project name | string |
n/a | yes |
bucket_name | Name of the S3 bucket | string |
n/a | yes |
logging_bucket_name | The S3 bucket for storing access logs | string |
null |
no |
custom_bucket_policy | Custom policy for the S3 bucket | string |
null |
no |
enable_access_logging | Enable access logging for the S3 bucket | bool |
false |
no |
lifecycle_rules | Additional lifecycle rules | list(object({ |
[] |
no |
Name | Description |
---|---|
bucket_arn | ARN of the S3 bucket |
bucket_name | Name of bucket S3 |
logging_bucket_name | The target bucket for S3 bucket logging |