Skip to content

Commit eb5cd1e

Browse files
committedSep 8, 2024·
chore: Update README.md and Terraform module template
Update the README.md file to include information about creating a new module and reference the Terraform Registry. Also, add a new Terraform module template with its associated files and documentation.
1 parent 1322a1f commit eb5cd1e

File tree

7 files changed

+259
-0
lines changed

7 files changed

+259
-0
lines changed
 

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ Our custom Terraform modules are located in the `modules` directory. These modul
116116
| [VPC](./modules/vpc/README.md) | Bootstrap a VPC for shared infrastructure. |
117117
| [VPC Endpoint](./modules/vpc-endpoint/README.md) | Bootstrap VPC endpoints for S3, DynamoDB, and other services. |
118118

119+
In case you need to create a new module, you can use the [Terraform Module Template](./modules/__template__/README.md) as a starting point.
120+
121+
For reference, you can also check the [Terraform Registry](https://registry.terraform.io/) for additional modules.
122+
119123
## Apps and Services
120124

121125
In addition to infrastructure provisioning, we have included a few apps and services to help you get started. These are located in the `apps` directory and provide useful examples of how to use the infrastructure we have provisioned.

‎modules/__template__/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Terraform Module Template
2+
3+
This is a template for creating Terraform modules. It includes a basic structure for organizing your module code and documentation.
4+
5+
In this example, we show how to create an S3 bucket with versioning, encryption, logging, and lifecycle management. This module uses a customizable name prefix for all resources and allows extra tags to be assigned.
6+
7+
## Key Highlights
8+
9+
1. **`name` Variable**: Used as a prefix for all resources, providing an easy way to distinguish resources created by the module.
10+
2. **`tags` Variable**: Allows additional tags to be passed to the module, merged with the default tag structure.
11+
3. **Documentation**: The README clearly documents the purpose and examples of using the module.
12+
4. **Module Documentation**: The module documentation is generated using [terraform-docs](https://github.com/terraform-docs/terraform-docs) and provides detailed information about the module's inputs and outputs.
13+
14+
## Usage
15+
16+
```hcl
17+
module "s3_bucket" {
18+
source = "path_to_your_module"
19+
20+
name = "data-lake"
21+
bucket_name = "raw-data"
22+
force_destroy = true
23+
enable_versioning = true
24+
kms_key_id = "alias/my-kms-key"
25+
logging_bucket = "my-logging-bucket"
26+
27+
tags = {
28+
Owner = "Anton"
29+
Environment = "prod"
30+
}
31+
}
32+
```
33+
34+
## Module Documentation
35+
36+
The module documentation is generated with [terraform-docs](https://github.com/terraform-docs/terraform-docs) by running `terraform-docs md . > ./docs/MODULE.md` from the module directory.
37+
38+
You can also view the latest version of the module documentation [here](./docs/MODULE.md).

‎modules/__template__/docs/MODULE.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
| Name | Version |
5+
|------|---------|
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.0.0 |
8+
9+
## Providers
10+
11+
| Name | Version |
12+
|------|---------|
13+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.0.0 |
14+
15+
## Modules
16+
17+
No modules.
18+
19+
## Resources
20+
21+
| Name | Type |
22+
|------|------|
23+
| [aws_s3_bucket.bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
24+
| [aws_s3_bucket_lifecycle_configuration.lifecycle](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
25+
| [aws_s3_bucket_logging.bucket_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
26+
| [aws_s3_bucket_ownership_controls.ownership](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource |
27+
| [aws_s3_bucket_public_access_block.public_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
28+
| [aws_s3_bucket_server_side_encryption_configuration.sse](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
29+
| [aws_s3_bucket_versioning.versioning](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning) | resource |
30+
31+
## Inputs
32+
33+
| Name | Description | Type | Default | Required |
34+
|------|-------------|------|---------|:--------:|
35+
| <a name="input_acl"></a> [acl](#input\_acl) | Canned ACL to apply to the bucket | `string` | `"private"` | no |
36+
| <a name="input_bucket_name"></a> [bucket\_name](#input\_bucket\_name) | The name of the S3 bucket | `string` | n/a | yes |
37+
| <a name="input_enable_lifecycle_rule"></a> [enable\_lifecycle\_rule](#input\_enable\_lifecycle\_rule) | Enable lifecycle rule | `bool` | `true` | no |
38+
| <a name="input_enable_versioning"></a> [enable\_versioning](#input\_enable\_versioning) | Enable versioning on the S3 bucket | `bool` | `false` | no |
39+
| <a name="input_force_destroy"></a> [force\_destroy](#input\_force\_destroy) | Force bucket deletion | `bool` | `false` | no |
40+
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | KMS key for bucket encryption | `string` | `"alias/aws/s3"` | no |
41+
| <a name="input_lifecycle_expiration_days"></a> [lifecycle\_expiration\_days](#input\_lifecycle\_expiration\_days) | Number of days after which to expire objects | `number` | `90` | no |
42+
| <a name="input_lifecycle_storage_class"></a> [lifecycle\_storage\_class](#input\_lifecycle\_storage\_class) | Storage class for lifecycle transition | `string` | `"GLACIER"` | no |
43+
| <a name="input_lifecycle_transition_days"></a> [lifecycle\_transition\_days](#input\_lifecycle\_transition\_days) | Number of days after which to transition objects | `number` | `30` | no |
44+
| <a name="input_logging_bucket"></a> [logging\_bucket](#input\_logging\_bucket) | Bucket for storing logs | `string` | n/a | yes |
45+
| <a name="input_name"></a> [name](#input\_name) | Name to be used on all the resources as identifier | `string` | `""` | no |
46+
| <a name="input_tags"></a> [tags](#input\_tags) | Any extra tags to assign to objects | `map(any)` | `{}` | no |
47+
48+
## Outputs
49+
50+
| Name | Description |
51+
|------|-------------|
52+
| <a name="output_bucket_arn"></a> [bucket\_arn](#output\_bucket\_arn) | The ARN of the S3 bucket |
53+
| <a name="output_bucket_id"></a> [bucket\_id](#output\_bucket\_id) | The ID of the S3 bucket |
54+
| <a name="output_logging_bucket"></a> [logging\_bucket](#output\_logging\_bucket) | The logging bucket for the S3 bucket |
55+
<!-- END_TF_DOCS -->

‎modules/__template__/main.tf

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
resource "aws_s3_bucket" "bucket" {
2+
bucket = "${var.name}-${var.bucket_name}"
3+
4+
force_destroy = var.force_destroy
5+
6+
tags = merge({
7+
Name = "${var.name}-s3-bucket"
8+
}, var.tags)
9+
}
10+
11+
resource "aws_s3_bucket_ownership_controls" "ownership" {
12+
bucket = aws_s3_bucket.bucket.id
13+
14+
rule {
15+
object_ownership = "BucketOwnerEnforced"
16+
}
17+
}
18+
19+
resource "aws_s3_bucket_public_access_block" "public_access" {
20+
bucket = aws_s3_bucket.bucket.id
21+
22+
block_public_acls = true
23+
block_public_policy = true
24+
restrict_public_buckets = true
25+
ignore_public_acls = true
26+
}
27+
28+
resource "aws_s3_bucket_versioning" "versioning" {
29+
bucket = aws_s3_bucket.bucket.id
30+
31+
versioning_configuration {
32+
status = var.enable_versioning ? "Enabled" : "Suspended"
33+
}
34+
}
35+
36+
resource "aws_s3_bucket_server_side_encryption_configuration" "sse" {
37+
bucket = aws_s3_bucket.bucket.id
38+
39+
rule {
40+
apply_server_side_encryption_by_default {
41+
sse_algorithm = "aws:kms"
42+
kms_master_key_id = var.kms_key_id
43+
}
44+
}
45+
}
46+
47+
resource "aws_s3_bucket_lifecycle_configuration" "lifecycle" {
48+
bucket = aws_s3_bucket.bucket.id
49+
50+
rule {
51+
id = "default"
52+
status = var.enable_lifecycle_rule ? "Enabled" : "Disabled"
53+
54+
transition {
55+
days = var.lifecycle_transition_days
56+
storage_class = var.lifecycle_storage_class
57+
}
58+
59+
expiration {
60+
days = var.lifecycle_expiration_days
61+
}
62+
}
63+
}
64+
65+
resource "aws_s3_bucket_logging" "bucket_logging" {
66+
bucket = aws_s3_bucket.bucket.id
67+
target_bucket = var.logging_bucket
68+
target_prefix = "${var.name}-${var.bucket_name}/logs/"
69+
}

‎modules/__template__/outputs.tf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "bucket_id" {
2+
description = "The ID of the S3 bucket"
3+
value = aws_s3_bucket.bucket.id
4+
}
5+
6+
output "bucket_arn" {
7+
description = "The ARN of the S3 bucket"
8+
value = aws_s3_bucket.bucket.arn
9+
}
10+
11+
output "logging_bucket" {
12+
description = "The logging bucket for the S3 bucket"
13+
value = aws_s3_bucket_logging.bucket_logging.target_bucket
14+
}

‎modules/__template__/variables.tf

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
variable "name" {
2+
description = "Name to be used on all the resources as identifier"
3+
type = string
4+
default = ""
5+
}
6+
7+
variable "tags" {
8+
description = "Any extra tags to assign to objects"
9+
type = map(any)
10+
default = {}
11+
}
12+
13+
variable "bucket_name" {
14+
description = "The name of the S3 bucket"
15+
type = string
16+
}
17+
18+
variable "force_destroy" {
19+
description = "Force bucket deletion"
20+
type = bool
21+
default = false
22+
}
23+
24+
variable "acl" {
25+
description = "Canned ACL to apply to the bucket"
26+
type = string
27+
default = "private"
28+
}
29+
30+
variable "enable_versioning" {
31+
description = "Enable versioning on the S3 bucket"
32+
type = bool
33+
default = false
34+
}
35+
36+
variable "kms_key_id" {
37+
description = "KMS key for bucket encryption"
38+
type = string
39+
default = "alias/aws/s3"
40+
}
41+
42+
variable "enable_lifecycle_rule" {
43+
description = "Enable lifecycle rule"
44+
type = bool
45+
default = true
46+
}
47+
48+
variable "lifecycle_transition_days" {
49+
description = "Number of days after which to transition objects"
50+
type = number
51+
default = 30
52+
}
53+
54+
variable "lifecycle_storage_class" {
55+
description = "Storage class for lifecycle transition"
56+
type = string
57+
default = "GLACIER"
58+
}
59+
60+
variable "lifecycle_expiration_days" {
61+
description = "Number of days after which to expire objects"
62+
type = number
63+
default = 90
64+
}
65+
66+
variable "logging_bucket" {
67+
description = "Bucket for storing logs"
68+
type = string
69+
}

‎modules/__template__/versions.tf

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 5.0.0"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)
Please sign in to comment.