Skip to content

Commit 2e8ea79

Browse files
Added aurora module
1 parent eb78a43 commit 2e8ea79

File tree

13 files changed

+500
-21
lines changed

13 files changed

+500
-21
lines changed

live/core/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@
1717

1818
✔️ [Terraform](https://www.terraform.io/downloads.html)
1919

20-
## Module Documentation
21-
22-
The module documentation is generated with [terraform-docs](https://github.com/terraform-docs/terraform-docs) by running the following command from the module directory:
23-
24-
```sh
25-
terraform-docs md . > ./docs/MODULE.md
26-
```
27-
28-
You can also view the latest version of the module documentation [here](./docs/MODULE.md).
29-
3020
## Setup
3121

3222
1. Initialize the Terraform working directory:
@@ -178,3 +168,13 @@ You can now execute SQL commands to test the database setup. For example:
178168
```
179169
180170
These steps will help you verify the successful setup of the database and ensure that the necessary connections and configurations are in place.
171+
172+
## Module Documentation
173+
174+
The module documentation is generated with [terraform-docs](https://github.com/terraform-docs/terraform-docs) by running the following command from the module directory:
175+
176+
```sh
177+
terraform-docs md . > ./docs/MODULE.md
178+
```
179+
180+
You can also view the latest version of the module documentation [here](./docs/MODULE.md).

modules/rds-aurora/.terraform.lock.hcl

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/rds-aurora/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# RDS Aurora Module
2+
3+
Terraform module to bootstrap a RDS Aurora instances and other database resources.
4+
5+
## Usage
6+
7+
```hcl
8+
module "db" {
9+
source = "../../modules/rds-aurora"
10+
11+
name = "examples-rds-aurora"
12+
13+
vpc_id = "vpc-1234567890"
14+
db_subnet_group = "db-subnet-group-1234567890"
15+
16+
db_name = "db_name"
17+
db_master_username = "db_master_username"
18+
db_port = 5432
19+
20+
db_instance_class = "db.serverless"
21+
instances = {
22+
one = {}
23+
two = {}
24+
}
25+
}
26+
```
27+
28+
## Module Documentation
29+
30+
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.
31+
32+
You can also view the latest version of the module documentation [here](./docs/MODULE.md).

modules/rds-aurora/docs/MODULE.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
| Name | Version |
5+
|------|---------|
6+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.0.0 |
7+
8+
## Providers
9+
10+
| Name | Version |
11+
|------|---------|
12+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.0.0 |
13+
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
14+
15+
## Modules
16+
17+
| Name | Source | Version |
18+
|------|--------|---------|
19+
| <a name="module_db"></a> [db](#module\_db) | terraform-aws-modules/rds/aws | 6.0.0 |
20+
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |
21+
22+
## Resources
23+
24+
| Name | Type |
25+
|------|------|
26+
| [aws_secretsmanager_secret.secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | resource |
27+
| [aws_secretsmanager_secret_version.secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret_version) | resource |
28+
| [random_password.rds_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
29+
| [aws_vpc.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
30+
31+
## Inputs
32+
33+
| Name | Description | Type | Default | Required |
34+
|------|-------------|------|---------|:--------:|
35+
| <a name="input_db_allocated_storage"></a> [db\_allocated\_storage](#input\_db\_allocated\_storage) | Storage size in GB. | `number` | `20` | no |
36+
| <a name="input_db_backup_retention_period"></a> [db\_backup\_retention\_period](#input\_db\_backup\_retention\_period) | Backup retention period in days. | `string` | `"1"` | no |
37+
| <a name="input_db_backup_window"></a> [db\_backup\_window](#input\_db\_backup\_window) | Preferred backup window. | `string` | `"03:00-06:00"` | no |
38+
| <a name="input_db_engine"></a> [db\_engine](#input\_db\_engine) | The name of the database engine to be used for RDS. | `string` | `"postgres"` | no |
39+
| <a name="input_db_engine_version"></a> [db\_engine\_version](#input\_db\_engine\_version) | The database engine version. | `string` | `"14"` | no |
40+
| <a name="input_db_family"></a> [db\_family](#input\_db\_family) | The family of the database engine to be used for RDS. | `string` | `"postgres14"` | no |
41+
| <a name="input_db_instance"></a> [db\_instance](#input\_db\_instance) | The instance class to use for RDS. | `string` | `"db.t4g.large"` | no |
42+
| <a name="input_db_maintenance_window"></a> [db\_maintenance\_window](#input\_db\_maintenance\_window) | Preferred maintenance window. | `string` | `"Mon:00:00-Mon:03:00"` | no |
43+
| <a name="input_db_major_engine_version"></a> [db\_major\_engine\_version](#input\_db\_major\_engine\_version) | The major engine version. | `string` | `"14"` | no |
44+
| <a name="input_db_master_password"></a> [db\_master\_password](#input\_db\_master\_password) | Database password | `string` | `""` | no |
45+
| <a name="input_db_master_username"></a> [db\_master\_username](#input\_db\_master\_username) | Database username | `string` | `"name"` | no |
46+
| <a name="input_db_max_allocated_storage"></a> [db\_max\_allocated\_storage](#input\_db\_max\_allocated\_storage) | Maximum storage size in GB. | `number` | `100` | no |
47+
| <a name="input_db_name"></a> [db\_name](#input\_db\_name) | Database name | `string` | `"name"` | no |
48+
| <a name="input_db_port"></a> [db\_port](#input\_db\_port) | Database port | `number` | `5432` | no |
49+
| <a name="input_db_storage_type"></a> [db\_storage\_type](#input\_db\_storage\_type) | Storage Type for RDS. | `string` | `"gp2"` | no |
50+
| <a name="input_db_subnet_group"></a> [db\_subnet\_group](#input\_db\_subnet\_group) | Database subnet group to use. Leave blank to create a new one. | `string` | `""` | no |
51+
| <a name="input_enable_multi_az"></a> [enable\_multi\_az](#input\_enable\_multi\_az) | Create RDS instance in multiple availability zones. | `bool` | `false` | no |
52+
| <a name="input_enable_public_access"></a> [enable\_public\_access](#input\_enable\_public\_access) | Enable public access for RDS. | `bool` | `true` | no |
53+
| <a name="input_enable_skip_final_snapshot"></a> [enable\_skip\_final\_snapshot](#input\_enable\_skip\_final\_snapshot) | When DB is deleted and If this variable is false, no final snapshot will be made. | `bool` | `true` | no |
54+
| <a name="input_name"></a> [name](#input\_name) | Name to be used on all the resources as identifier | `string` | `""` | no |
55+
| <a name="input_storage_encrypted"></a> [storage\_encrypted](#input\_storage\_encrypted) | Enable storage encryption. | `bool` | `true` | no |
56+
| <a name="input_tags"></a> [tags](#input\_tags) | Any extra tags to assign to objects | `map(any)` | `{}` | no |
57+
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | VPC id in which the RDS instance is to be created. | `string` | n/a | yes |
58+
59+
## Outputs
60+
61+
| Name | Description |
62+
|------|-------------|
63+
| <a name="output_connection_secret_arn"></a> [connection\_secret\_arn](#output\_connection\_secret\_arn) | The ARN of the AWS Secrets Manager secret created |
64+
| <a name="output_connection_secret_name"></a> [connection\_secret\_name](#output\_connection\_secret\_name) | The name of the AWS Secrets Manager secret created |
65+
| <a name="output_db_instance_address"></a> [db\_instance\_address](#output\_db\_instance\_address) | The address of the RDS instance |
66+
| <a name="output_db_instance_arn"></a> [db\_instance\_arn](#output\_db\_instance\_arn) | The ARN of the RDS instance |
67+
| <a name="output_db_instance_availability_zone"></a> [db\_instance\_availability\_zone](#output\_db\_instance\_availability\_zone) | The availability zone of the RDS instance |
68+
| <a name="output_db_instance_cloudwatch_log_groups"></a> [db\_instance\_cloudwatch\_log\_groups](#output\_db\_instance\_cloudwatch\_log\_groups) | Map of CloudWatch log groups created and their attributes |
69+
| <a name="output_db_instance_endpoint"></a> [db\_instance\_endpoint](#output\_db\_instance\_endpoint) | The connection endpoint |
70+
| <a name="output_db_instance_engine"></a> [db\_instance\_engine](#output\_db\_instance\_engine) | The database engine |
71+
| <a name="output_db_instance_engine_version_actual"></a> [db\_instance\_engine\_version\_actual](#output\_db\_instance\_engine\_version\_actual) | The running version of the database |
72+
| <a name="output_db_instance_hosted_zone_id"></a> [db\_instance\_hosted\_zone\_id](#output\_db\_instance\_hosted\_zone\_id) | The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record) |
73+
| <a name="output_db_instance_identifier"></a> [db\_instance\_identifier](#output\_db\_instance\_identifier) | The RDS instance ID |
74+
| <a name="output_db_instance_name"></a> [db\_instance\_name](#output\_db\_instance\_name) | The database name |
75+
| <a name="output_db_instance_port"></a> [db\_instance\_port](#output\_db\_instance\_port) | The database port |
76+
| <a name="output_db_instance_resource_id"></a> [db\_instance\_resource\_id](#output\_db\_instance\_resource\_id) | The RDS Resource ID of this instance |
77+
| <a name="output_db_instance_status"></a> [db\_instance\_status](#output\_db\_instance\_status) | The RDS instance status |
78+
| <a name="output_db_instance_username"></a> [db\_instance\_username](#output\_db\_instance\_username) | The master username for the database |
79+
| <a name="output_db_parameter_group_arn"></a> [db\_parameter\_group\_arn](#output\_db\_parameter\_group\_arn) | The ARN of the db parameter group |
80+
| <a name="output_db_parameter_group_id"></a> [db\_parameter\_group\_id](#output\_db\_parameter\_group\_id) | The db parameter group id |
81+
| <a name="output_db_subnet_group_arn"></a> [db\_subnet\_group\_arn](#output\_db\_subnet\_group\_arn) | The ARN of the db subnet group |
82+
| <a name="output_db_subnet_group_id"></a> [db\_subnet\_group\_id](#output\_db\_subnet\_group\_id) | The db subnet group name |
83+
| <a name="output_enhanced_monitoring_iam_role_arn"></a> [enhanced\_monitoring\_iam\_role\_arn](#output\_enhanced\_monitoring\_iam\_role\_arn) | The Amazon Resource Name (ARN) specifying the monitoring role |
84+
<!-- END_TF_DOCS -->

modules/rds-aurora/outputs.tf

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
output "cluster_arn" {
2+
description = "The ARN of the RDS instance"
3+
value = module.db.cluster_arn
4+
}
5+
6+
output "cluster_endpoint" {
7+
description = "The connection endpoint"
8+
value = module.db.cluster_endpoint
9+
}
10+
11+
output "cluster_reader_endpoint" {
12+
description = "The address of the RDS instance"
13+
value = module.db.cluster_reader_endpoint
14+
}
15+
16+
output "cluster_members" {
17+
description = "List of RDS Instances that are a part of this cluster"
18+
value = try(module.db.cluster_members, null)
19+
}
20+
21+
output "cluster_engine_version_actual" {
22+
description = "The running version of the database"
23+
value = module.db.cluster_engine_version_actual
24+
}
25+
26+
output "cluster_database_name" {
27+
description = "The database name"
28+
value = module.db.cluster_database_name
29+
}
30+
31+
output "cluster_resource_id" {
32+
description = "The RDS Resource ID of this instance"
33+
value = module.db.cluster_resource_id
34+
}
35+
36+
output "cluster_port" {
37+
description = "The database port"
38+
value = module.db.cluster_port
39+
}
40+
41+
output "cluster_master_username" {
42+
description = "The master username for the database"
43+
value = module.db.cluster_master_username
44+
sensitive = true
45+
}
46+
47+
output "cluster_instances" {
48+
description = "The RDS Instances for this cluster"
49+
value = module.db.cluster_instances
50+
}
51+
52+
output "enhanced_monitoring_iam_role_name" {
53+
description = "The name of the IAM role used for enhanced monitoring"
54+
value = module.db.enhanced_monitoring_iam_role_name
55+
}
56+
57+
output "enhanced_monitoring_iam_role_arn" {
58+
description = "The ARN of the IAM role used for enhanced monitoring"
59+
value = module.db.enhanced_monitoring_iam_role_arn
60+
}
61+
62+
output "enhanced_monitoring_iam_role_unique_id" {
63+
description = "The unique ID of the IAM role used for enhanced monitoring"
64+
value = module.db.enhanced_monitoring_iam_role_unique_id
65+
}
66+
67+
output "security_group_id" {
68+
description = "The ID of the security group"
69+
value = module.db.security_group_id
70+
}
71+
72+
output "db_cluster_cloudwatch_log_groups" {
73+
description = "The CloudWatch log groups for the DB cluster"
74+
value = module.db.db_cluster_cloudwatch_log_groups
75+
}
76+
77+
output "connection_secret_name" {
78+
description = "The name of the AWS Secrets Manager secret created"
79+
value = aws_secretsmanager_secret.secret.name
80+
}
81+
82+
output "connection_secret_arn" {
83+
description = "The ARN of the AWS Secrets Manager secret created"
84+
value = aws_secretsmanager_secret.secret.arn
85+
}

modules/rds-aurora/rds.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module "db" {
2+
source = "terraform-aws-modules/rds-aurora/aws"
3+
version = "8.3.1"
4+
5+
name = "${var.name}-rds-aurora"
6+
7+
# All available versions: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts
8+
engine = var.db_engine
9+
engine_version = var.db_engine_version
10+
instance_class = var.db_instance_class
11+
12+
storage_type = var.db_storage_type
13+
storage_encrypted = var.storage_encrypted
14+
allocated_storage = var.db_allocated_storage
15+
16+
# NOTE: Do NOT use 'user' as the value for 'username' as it throws:
17+
# "Error creating DB Instance: InvalidParameterValue: MasterUsername
18+
# user cannot be used as it is a reserved word used by the engine"
19+
database_name = var.db_name
20+
master_username = local.username
21+
master_password = local.password
22+
port = var.db_port
23+
24+
db_subnet_group_name = var.db_subnet_group
25+
vpc_security_group_ids = [module.security_group.security_group_id]
26+
27+
preferred_maintenance_window = var.db_maintenance_window
28+
preferred_backup_window = var.db_backup_window
29+
enabled_cloudwatch_logs_exports = ["postgresql", "upgrade"]
30+
create_cloudwatch_log_group = true
31+
32+
backup_retention_period = var.db_backup_retention_period
33+
skip_final_snapshot = var.enable_skip_final_snapshot
34+
deletion_protection = false
35+
36+
publicly_accessible = var.enable_public_access
37+
38+
performance_insights_enabled = true
39+
performance_insights_retention_period = 7
40+
create_monitoring_role = true
41+
monitoring_interval = 60
42+
43+
# We will create our own secret and store it in AWS Secrets Manager
44+
manage_master_user_password = false
45+
46+
tags = var.tags
47+
}

modules/rds-aurora/secret.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Create a random initial password for the RDS DB Instance
2+
resource "random_password" "rds_password" {
3+
length = 16
4+
special = true
5+
override_special = "_%@"
6+
}
7+
8+
locals {
9+
# if var.db_master_password is not set, use the random password
10+
password = var.db_master_password != "" ? var.db_master_password : random_password.rds_password.result
11+
username = var.db_master_username
12+
}
13+
14+
resource "aws_secretsmanager_secret" "secret" {
15+
description = "RDS DB Instance Connection Credentials"
16+
name = "${var.name}-connection-secret"
17+
}
18+
19+
resource "aws_secretsmanager_secret_version" "secret" {
20+
lifecycle {
21+
ignore_changes = [
22+
secret_string
23+
]
24+
}
25+
secret_id = aws_secretsmanager_secret.secret.id
26+
secret_string = <<EOF
27+
{
28+
"username": "${local.username}",
29+
"password": "${local.password}",
30+
"host": "${module.db.cluster_endpoint}",
31+
"port": ${module.db.cluster_port},
32+
"dbname" : "${var.db_name}"
33+
}
34+
EOF
35+
}

modules/rds-aurora/sg.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
data "aws_vpc" "main" {
2+
id = var.vpc_id
3+
}
4+
5+
module "security_group" {
6+
source = "terraform-aws-modules/security-group/aws"
7+
version = "~> 4.0"
8+
9+
name = "${var.name}-security-group"
10+
description = "Security group for ${var.name}"
11+
vpc_id = var.vpc_id
12+
13+
ingress_with_cidr_blocks = [
14+
{
15+
from_port = module.db.cluster_port
16+
to_port = module.db.cluster_port
17+
protocol = "tcp"
18+
description = "RDS DB Instance access from within VPC"
19+
cidr_blocks = data.aws_vpc.main.cidr_block
20+
}
21+
]
22+
23+
egress_rules = ["all-all"]
24+
25+
tags = var.tags
26+
}

0 commit comments

Comments
 (0)