Skip to content

Commit 9913375

Browse files
committed
Add postgres and mysql replica examples
1 parent 7e2c09c commit 9913375

File tree

7 files changed

+556
-0
lines changed

7 files changed

+556
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ module "db" {
120120
* [Complete RDS example for Oracle](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/examples/complete-oracle)
121121
* [Complete RDS example for MSSQL](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/examples/complete-mssql)
122122
* [Enhanced monitoring example](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/examples/enhanced-monitoring)
123+
* [Replica RDS example for MySQL](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/examples/replica-mysql)
124+
* [Replica RDS example for PostgreSQL](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/examples/replica-postgres)
123125

124126
## Notes
125127

examples/replica-mysql/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Master and Replica RDS example for MySQL
2+
3+
Configuration in this directory creates set of RDS resources demonstrating master and replica in the same VPC.
4+
5+
Data sources are used to discover existing VPC resources (VPC, subnet and security group).
6+
7+
## Usage
8+
9+
To run this example you need to execute:
10+
11+
```bash
12+
$ terraform init
13+
$ terraform plan
14+
$ terraform apply
15+
```
16+
17+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
18+
19+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
20+
## Outputs
21+
22+
| Name | Description |
23+
|------|-------------|
24+
| master\_db\_instance\_address | The address of the RDS instance |
25+
| master\_db\_instance\_arn | The ARN of the RDS instance |
26+
| master\_db\_instance\_availability\_zone | The availability zone of the RDS instance |
27+
| master\_db\_instance\_endpoint | The connection endpoint |
28+
| master\_db\_instance\_hosted\_zone\_id | The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record) |
29+
| master\_db\_instance\_id | The RDS instance ID |
30+
| master\_db\_instance\_name | The database name |
31+
| master\_db\_instance\_password | The database password (this password may be old, because Terraform doesn't track it after initial creation) |
32+
| master\_db\_instance\_port | The database port |
33+
| master\_db\_instance\_resource\_id | The RDS Resource ID of this instance |
34+
| master\_db\_instance\_status | The RDS instance status |
35+
| master\_db\_instance\_username | The master username for the database |
36+
| master\_db\_subnet\_group\_arn | The ARN of the db subnet group |
37+
| master\_db\_subnet\_group\_id | The db subnet group name |
38+
| replica\_db\_instance\_address | The address of the RDS instance |
39+
| replica\_db\_instance\_arn | The ARN of the RDS instance |
40+
| replica\_db\_instance\_availability\_zone | The availability zone of the RDS instance |
41+
| replica\_db\_instance\_endpoint | The connection endpoint |
42+
| replica\_db\_instance\_hosted\_zone\_id | The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record) |
43+
| replica\_db\_instance\_id | The RDS instance ID |
44+
| replica\_db\_instance\_name | The database name |
45+
| replica\_db\_instance\_port | The database port |
46+
| replica\_db\_instance\_resource\_id | The RDS Resource ID of this instance |
47+
| replica\_db\_instance\_status | The RDS instance status |
48+
| replica\_db\_instance\_username | The replica username for the database |
49+
50+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/replica-mysql/main.tf

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
####################################
6+
# Variables common to both instnaces
7+
####################################
8+
locals {
9+
engine = "mysql"
10+
engine_version = "5.7.19"
11+
instance_class = "db.t2.large"
12+
allocated_storage = 5
13+
port = "3306"
14+
}
15+
16+
##############################################################
17+
# Data sources to get VPC, subnets and security group details
18+
##############################################################
19+
data "aws_vpc" "default" {
20+
default = true
21+
}
22+
23+
data "aws_subnet_ids" "all" {
24+
vpc_id = "${data.aws_vpc.default.id}"
25+
}
26+
27+
data "aws_security_group" "default" {
28+
vpc_id = "${data.aws_vpc.default.id}"
29+
name = "default"
30+
}
31+
32+
###########
33+
# Master DB
34+
###########
35+
module "master" {
36+
source = "../../"
37+
38+
identifier = "demodb-master"
39+
40+
engine = "${local.engine}"
41+
engine_version = "${local.engine_version}"
42+
instance_class = "${local.instance_class}"
43+
allocated_storage = "${local.allocated_storage}"
44+
45+
name = "demodb"
46+
username = "user"
47+
password = "YourPwdShouldBeLongAndSecure!"
48+
port = "${local.port}"
49+
50+
vpc_security_group_ids = ["${data.aws_security_group.default.id}"]
51+
52+
maintenance_window = "Mon:00:00-Mon:03:00"
53+
backup_window = "03:00-06:00"
54+
55+
multi_az = true
56+
57+
# Backups are required in order to create a replica
58+
backup_retention_period = 1
59+
60+
# DB subnet group
61+
subnet_ids = ["${data.aws_subnet_ids.all.ids}"]
62+
63+
create_db_option_group = false
64+
create_db_parameter_group = false
65+
}
66+
67+
############
68+
# Replica DB
69+
############
70+
module "replica" {
71+
source = "../../"
72+
73+
identifier = "demodb-replica"
74+
75+
# Source database. For cross-region use this_db_instance_arn
76+
replicate_source_db = "${module.master.this_db_instance_id}"
77+
78+
engine = "${local.engine}"
79+
engine_version = "${local.engine_version}"
80+
instance_class = "${local.instance_class}"
81+
allocated_storage = "${local.allocated_storage}"
82+
83+
# Username and password should not be set for replicas
84+
username = ""
85+
password = ""
86+
port = "${local.port}"
87+
88+
vpc_security_group_ids = ["${data.aws_security_group.default.id}"]
89+
90+
maintenance_window = "Tue:00:00-Tue:03:00"
91+
backup_window = "03:00-06:00"
92+
93+
multi_az = false
94+
95+
# disable backups to create DB faster
96+
backup_retention_period = 0
97+
98+
# Not allowed to specify a subnet group for replicas in the same region
99+
create_db_subnet_group = false
100+
101+
create_db_option_group = false
102+
create_db_parameter_group = false
103+
}

examples/replica-mysql/outputs.tf

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Master
2+
output "master_db_instance_address" {
3+
description = "The address of the RDS instance"
4+
value = "${module.master.this_db_instance_address}"
5+
}
6+
7+
output "master_db_instance_arn" {
8+
description = "The ARN of the RDS instance"
9+
value = "${module.master.this_db_instance_arn}"
10+
}
11+
12+
output "master_db_instance_availability_zone" {
13+
description = "The availability zone of the RDS instance"
14+
value = "${module.master.this_db_instance_availability_zone}"
15+
}
16+
17+
output "master_db_instance_endpoint" {
18+
description = "The connection endpoint"
19+
value = "${module.master.this_db_instance_endpoint}"
20+
}
21+
22+
output "master_db_instance_hosted_zone_id" {
23+
description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"
24+
value = "${module.master.this_db_instance_hosted_zone_id}"
25+
}
26+
27+
output "master_db_instance_id" {
28+
description = "The RDS instance ID"
29+
value = "${module.master.this_db_instance_id}"
30+
}
31+
32+
output "master_db_instance_resource_id" {
33+
description = "The RDS Resource ID of this instance"
34+
value = "${module.master.this_db_instance_resource_id}"
35+
}
36+
37+
output "master_db_instance_status" {
38+
description = "The RDS instance status"
39+
value = "${module.master.this_db_instance_status}"
40+
}
41+
42+
output "master_db_instance_name" {
43+
description = "The database name"
44+
value = "${module.master.this_db_instance_name}"
45+
}
46+
47+
output "master_db_instance_username" {
48+
description = "The master username for the database"
49+
value = "${module.master.this_db_instance_username}"
50+
}
51+
52+
output "master_db_instance_password" {
53+
description = "The database password (this password may be old, because Terraform doesn't track it after initial creation)"
54+
value = "${module.master.this_db_instance_password}"
55+
}
56+
57+
output "master_db_instance_port" {
58+
description = "The database port"
59+
value = "${module.master.this_db_instance_port}"
60+
}
61+
62+
output "master_db_subnet_group_id" {
63+
description = "The db subnet group name"
64+
value = "${module.master.this_db_subnet_group_id}"
65+
}
66+
67+
output "master_db_subnet_group_arn" {
68+
description = "The ARN of the db subnet group"
69+
value = "${module.master.this_db_subnet_group_arn}"
70+
}
71+
72+
# Replica
73+
output "replica_db_instance_address" {
74+
description = "The address of the RDS instance"
75+
value = "${module.replica.this_db_instance_address}"
76+
}
77+
78+
output "replica_db_instance_arn" {
79+
description = "The ARN of the RDS instance"
80+
value = "${module.replica.this_db_instance_arn}"
81+
}
82+
83+
output "replica_db_instance_availability_zone" {
84+
description = "The availability zone of the RDS instance"
85+
value = "${module.replica.this_db_instance_availability_zone}"
86+
}
87+
88+
output "replica_db_instance_endpoint" {
89+
description = "The connection endpoint"
90+
value = "${module.replica.this_db_instance_endpoint}"
91+
}
92+
93+
output "replica_db_instance_hosted_zone_id" {
94+
description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"
95+
value = "${module.replica.this_db_instance_hosted_zone_id}"
96+
}
97+
98+
output "replica_db_instance_id" {
99+
description = "The RDS instance ID"
100+
value = "${module.replica.this_db_instance_id}"
101+
}
102+
103+
output "replica_db_instance_resource_id" {
104+
description = "The RDS Resource ID of this instance"
105+
value = "${module.replica.this_db_instance_resource_id}"
106+
}
107+
108+
output "replica_db_instance_status" {
109+
description = "The RDS instance status"
110+
value = "${module.replica.this_db_instance_status}"
111+
}
112+
113+
output "replica_db_instance_name" {
114+
description = "The database name"
115+
value = "${module.replica.this_db_instance_name}"
116+
}
117+
118+
output "replica_db_instance_username" {
119+
description = "The replica username for the database"
120+
value = "${module.replica.this_db_instance_username}"
121+
}
122+
123+
output "replica_db_instance_port" {
124+
description = "The database port"
125+
value = "${module.replica.this_db_instance_port}"
126+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Master and Replica RDS example for PostgreSQL
2+
3+
Configuration in this directory creates set of RDS resources demonstrating master and replica in the same VPC.
4+
5+
Data sources are used to discover existing VPC resources (VPC, subnet and security group).
6+
7+
## Usage
8+
9+
To run this example you need to execute:
10+
11+
```bash
12+
$ terraform init
13+
$ terraform plan
14+
$ terraform apply
15+
```
16+
17+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
18+
19+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
20+
## Outputs
21+
22+
| Name | Description |
23+
|------|-------------|
24+
| master\_db\_instance\_address | The address of the RDS instance |
25+
| master\_db\_instance\_arn | The ARN of the RDS instance |
26+
| master\_db\_instance\_availability\_zone | The availability zone of the RDS instance |
27+
| master\_db\_instance\_endpoint | The connection endpoint |
28+
| master\_db\_instance\_hosted\_zone\_id | The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record) |
29+
| master\_db\_instance\_id | The RDS instance ID |
30+
| master\_db\_instance\_name | The database name |
31+
| master\_db\_instance\_password | The database password (this password may be old, because Terraform doesn't track it after initial creation) |
32+
| master\_db\_instance\_port | The database port |
33+
| master\_db\_instance\_resource\_id | The RDS Resource ID of this instance |
34+
| master\_db\_instance\_status | The RDS instance status |
35+
| master\_db\_instance\_username | The master username for the database |
36+
| master\_db\_subnet\_group\_arn | The ARN of the db subnet group |
37+
| master\_db\_subnet\_group\_id | The db subnet group name |
38+
| replica\_db\_instance\_address | The address of the RDS instance |
39+
| replica\_db\_instance\_arn | The ARN of the RDS instance |
40+
| replica\_db\_instance\_availability\_zone | The availability zone of the RDS instance |
41+
| replica\_db\_instance\_endpoint | The connection endpoint |
42+
| replica\_db\_instance\_hosted\_zone\_id | The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record) |
43+
| replica\_db\_instance\_id | The RDS instance ID |
44+
| replica\_db\_instance\_name | The database name |
45+
| replica\_db\_instance\_port | The database port |
46+
| replica\_db\_instance\_resource\_id | The RDS Resource ID of this instance |
47+
| replica\_db\_instance\_status | The RDS instance status |
48+
| replica\_db\_instance\_username | The replica username for the database |
49+
50+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

0 commit comments

Comments
 (0)