Skip to content

Commit 183fa2b

Browse files
committed
chore: update to support global aurora clusters and add example, closes terraform-aws-modules#237
1 parent 1407dd0 commit 183fa2b

File tree

20 files changed

+440
-25
lines changed

20 files changed

+440
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ Terraform documentation is generated automatically using [pre-commit hooks](http
7676
| Name | Version |
7777
|------|---------|
7878
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
79-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.30 |
79+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.63 |
8080
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.2 |
8181

8282
## Providers
8383

8484
| Name | Version |
8585
|------|---------|
86-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.30 |
86+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.63 |
8787
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.2 |
8888

8989
## Modules

examples/autoscaling/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ Note that this example may create resources which cost money. Run `terraform des
2020
| Name | Version |
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
23-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.8 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.63 |
2424

2525
## Providers
2626

2727
| Name | Version |
2828
|------|---------|
29-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.8 |
29+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.63 |
3030

3131
## Modules
3232

examples/autoscaling/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 3.8"
7+
version = ">= 3.63"
88
}
99
}
1010
}

examples/custom_instance_settings/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ Note that this example may create resources which cost money. Run `terraform des
2020
| Name | Version |
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
23-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.8 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.63 |
2424

2525
## Providers
2626

2727
| Name | Version |
2828
|------|---------|
29-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.8 |
29+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.63 |
3030

3131
## Modules
3232

examples/custom_instance_settings/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 3.8"
7+
version = ">= 3.63"
88
}
99
}
1010
}

examples/global_cluster/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# PostgreSQL Example
2+
3+
Configuration in this directory creates a PostgreSQL Aurora cluster.
4+
5+
## Usage
6+
7+
To run this example you need to execute:
8+
9+
```bash
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Requirements
19+
20+
| Name | Version |
21+
|------|---------|
22+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.63 |
24+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.2 |
25+
26+
## Providers
27+
28+
| Name | Version |
29+
|------|---------|
30+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.63 |
31+
| <a name="provider_aws.secondary"></a> [aws.secondary](#provider\_aws.secondary) | >= 3.63 |
32+
33+
## Modules
34+
35+
| Name | Source | Version |
36+
|------|--------|---------|
37+
| <a name="module_primary_aurora"></a> [primary\_aurora](#module\_primary\_aurora) | ../../ | n/a |
38+
| <a name="module_primary_vpc"></a> [primary\_vpc](#module\_primary\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
39+
| <a name="module_secondary_aurora"></a> [secondary\_aurora](#module\_secondary\_aurora) | ../../ | n/a |
40+
| <a name="module_secondary_vpc"></a> [secondary\_vpc](#module\_secondary\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
41+
42+
## Resources
43+
44+
| Name | Type |
45+
|------|------|
46+
| [aws_kms_key.primary](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
47+
| [aws_kms_key.secondary](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
48+
| [aws_rds_global_cluster.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_global_cluster) | resource |
49+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
50+
| [aws_iam_policy_document.rds](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
51+
52+
## Inputs
53+
54+
No inputs.
55+
56+
## Outputs
57+
58+
| Name | Description |
59+
|------|-------------|
60+
| <a name="output_global_cluster_members"></a> [global\_cluster\_members](#output\_global\_cluster\_members) | Set of objects containing Global Cluster members |
61+
| <a name="output_primary_cluster_database_name"></a> [primary\_cluster\_database\_name](#output\_primary\_cluster\_database\_name) | Name for an automatically created database on cluster creation |
62+
| <a name="output_primary_cluster_endpoint"></a> [primary\_cluster\_endpoint](#output\_primary\_cluster\_endpoint) | The cluster endpoint |
63+
| <a name="output_primary_cluster_id"></a> [primary\_cluster\_id](#output\_primary\_cluster\_id) | The ID of the cluster |
64+
| <a name="output_primary_cluster_instance_dbi_resource_ids"></a> [primary\_cluster\_instance\_dbi\_resource\_ids](#output\_primary\_cluster\_instance\_dbi\_resource\_ids) | A list of all the region-unique, immutable identifiers for the DB instances |
65+
| <a name="output_primary_cluster_instance_endpoints"></a> [primary\_cluster\_instance\_endpoints](#output\_primary\_cluster\_instance\_endpoints) | A list of all cluster instance endpoints |
66+
| <a name="output_primary_cluster_instance_ids"></a> [primary\_cluster\_instance\_ids](#output\_primary\_cluster\_instance\_ids) | A list of all cluster instance ids |
67+
| <a name="output_primary_cluster_master_password"></a> [primary\_cluster\_master\_password](#output\_primary\_cluster\_master\_password) | The master password |
68+
| <a name="output_primary_cluster_master_username"></a> [primary\_cluster\_master\_username](#output\_primary\_cluster\_master\_username) | The master username |
69+
| <a name="output_primary_cluster_port"></a> [primary\_cluster\_port](#output\_primary\_cluster\_port) | The port |
70+
| <a name="output_primary_cluster_reader_endpoint"></a> [primary\_cluster\_reader\_endpoint](#output\_primary\_cluster\_reader\_endpoint) | The cluster reader endpoint |
71+
| <a name="output_primary_cluster_resource_id"></a> [primary\_cluster\_resource\_id](#output\_primary\_cluster\_resource\_id) | The Resource ID of the cluster |
72+
| <a name="output_primary_security_group_id"></a> [primary\_security\_group\_id](#output\_primary\_security\_group\_id) | The security group ID of the cluster |
73+
| <a name="output_secondary_cluster_database_name"></a> [secondary\_cluster\_database\_name](#output\_secondary\_cluster\_database\_name) | Name for an automatically created database on cluster creation |
74+
| <a name="output_secondary_cluster_endpoint"></a> [secondary\_cluster\_endpoint](#output\_secondary\_cluster\_endpoint) | The cluster endpoint |
75+
| <a name="output_secondary_cluster_id"></a> [secondary\_cluster\_id](#output\_secondary\_cluster\_id) | The ID of the cluster |
76+
| <a name="output_secondary_cluster_instance_dbi_resource_ids"></a> [secondary\_cluster\_instance\_dbi\_resource\_ids](#output\_secondary\_cluster\_instance\_dbi\_resource\_ids) | A list of all the region-unique, immutable identifiers for the DB instances |
77+
| <a name="output_secondary_cluster_instance_endpoints"></a> [secondary\_cluster\_instance\_endpoints](#output\_secondary\_cluster\_instance\_endpoints) | A list of all cluster instance endpoints |
78+
| <a name="output_secondary_cluster_instance_ids"></a> [secondary\_cluster\_instance\_ids](#output\_secondary\_cluster\_instance\_ids) | A list of all cluster instance ids |
79+
| <a name="output_secondary_cluster_master_password"></a> [secondary\_cluster\_master\_password](#output\_secondary\_cluster\_master\_password) | The master password |
80+
| <a name="output_secondary_cluster_master_username"></a> [secondary\_cluster\_master\_username](#output\_secondary\_cluster\_master\_username) | The master username |
81+
| <a name="output_secondary_cluster_port"></a> [secondary\_cluster\_port](#output\_secondary\_cluster\_port) | The port |
82+
| <a name="output_secondary_cluster_reader_endpoint"></a> [secondary\_cluster\_reader\_endpoint](#output\_secondary\_cluster\_reader\_endpoint) | The cluster reader endpoint |
83+
| <a name="output_secondary_cluster_resource_id"></a> [secondary\_cluster\_resource\_id](#output\_secondary\_cluster\_resource\_id) | The Resource ID of the cluster |
84+
| <a name="output_secondary_security_group_id"></a> [secondary\_security\_group\_id](#output\_secondary\_security\_group\_id) | The security group ID of the cluster |
85+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/global_cluster/main.tf

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
provider "aws" {
2+
region = local.primary.region
3+
}
4+
5+
provider "aws" {
6+
alias = "secondary"
7+
region = local.secondary.region
8+
}
9+
10+
locals {
11+
name = "postgresql"
12+
primary = {
13+
region = "eu-west-1"
14+
cidr_prefix = "10.99"
15+
}
16+
secondary = {
17+
region = "us-east-1"
18+
cidr_prefix = "10.98"
19+
}
20+
tags = {
21+
Owner = "user"
22+
Environment = "dev"
23+
}
24+
}
25+
26+
data "aws_caller_identity" "current" {}
27+
28+
################################################################################
29+
# Supporting Resources
30+
################################################################################
31+
32+
module "primary_vpc" {
33+
source = "terraform-aws-modules/vpc/aws"
34+
version = "~> 3.0"
35+
36+
name = local.name
37+
cidr = "${local.primary.cidr_prefix}.0.0/18"
38+
39+
azs = ["${local.primary.region}a", "${local.primary.region}b", "${local.primary.region}c"]
40+
public_subnets = ["${local.primary.cidr_prefix}.0.0/24", "${local.primary.cidr_prefix}.1.0/24", "${local.primary.cidr_prefix}.2.0/24"]
41+
private_subnets = ["${local.primary.cidr_prefix}.3.0/24", "${local.primary.cidr_prefix}.4.0/24", "${local.primary.cidr_prefix}.5.0/24"]
42+
database_subnets = ["${local.primary.cidr_prefix}.7.0/24", "${local.primary.cidr_prefix}.8.0/24", "${local.primary.cidr_prefix}.9.0/24"]
43+
44+
tags = local.tags
45+
}
46+
47+
module "secondary_vpc" {
48+
source = "terraform-aws-modules/vpc/aws"
49+
version = "~> 3.0"
50+
51+
providers = { aws = aws.secondary }
52+
53+
name = local.name
54+
cidr = "${local.secondary.cidr_prefix}.0.0/18"
55+
56+
azs = ["${local.secondary.region}a", "${local.secondary.region}b", "${local.secondary.region}c"]
57+
public_subnets = ["${local.secondary.cidr_prefix}.0.0/24", "${local.secondary.cidr_prefix}.1.0/24", "${local.secondary.cidr_prefix}.2.0/24"]
58+
private_subnets = ["${local.secondary.cidr_prefix}.3.0/24", "${local.secondary.cidr_prefix}.4.0/24", "${local.secondary.cidr_prefix}.5.0/24"]
59+
database_subnets = ["${local.secondary.cidr_prefix}.7.0/24", "${local.secondary.cidr_prefix}.8.0/24", "${local.secondary.cidr_prefix}.9.0/24"]
60+
61+
tags = local.tags
62+
}
63+
64+
data "aws_iam_policy_document" "rds" {
65+
statement {
66+
sid = "Enable IAM User Permissions"
67+
actions = ["kms:*"]
68+
resources = ["*"]
69+
70+
principals {
71+
type = "AWS"
72+
identifiers = [
73+
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root",
74+
data.aws_caller_identity.current.arn,
75+
]
76+
}
77+
}
78+
79+
statement {
80+
sid = "Allow use of the key"
81+
actions = [
82+
"kms:Encrypt",
83+
"kms:Decrypt",
84+
"kms:ReEncrypt*",
85+
"kms:GenerateDataKey*",
86+
"kms:DescribeKey"
87+
]
88+
resources = ["*"]
89+
90+
principals {
91+
type = "Service"
92+
identifiers = [
93+
"monitoring.rds.amazonaws.com",
94+
"rds.amazonaws.com",
95+
]
96+
}
97+
}
98+
}
99+
100+
resource "aws_kms_key" "primary" {
101+
policy = data.aws_iam_policy_document.rds.json
102+
tags = local.tags
103+
}
104+
105+
resource "aws_kms_key" "secondary" {
106+
provider = aws.secondary
107+
108+
policy = data.aws_iam_policy_document.rds.json
109+
tags = local.tags
110+
}
111+
112+
################################################################################
113+
# RDS Aurora Module
114+
################################################################################
115+
116+
resource "aws_rds_global_cluster" "this" {
117+
global_cluster_identifier = local.name
118+
engine = "aurora-postgresql"
119+
engine_version = "11.12"
120+
database_name = "example_db"
121+
storage_encrypted = true
122+
}
123+
124+
module "primary_aurora" {
125+
source = "../../"
126+
127+
name = local.name
128+
database_name = aws_rds_global_cluster.this.database_name
129+
engine = aws_rds_global_cluster.this.engine
130+
engine_version = aws_rds_global_cluster.this.engine_version
131+
global_cluster_identifier = aws_rds_global_cluster.this.id
132+
instance_type = "db.r6g.large"
133+
kms_key_id = aws_kms_key.primary.arn
134+
135+
vpc_id = module.primary_vpc.vpc_id
136+
db_subnet_group_name = module.primary_vpc.database_subnet_group_name
137+
create_security_group = true
138+
allowed_cidr_blocks = module.primary_vpc.private_subnets_cidr_blocks
139+
140+
replica_count = 2
141+
skip_final_snapshot = true
142+
143+
tags = local.tags
144+
}
145+
146+
module "secondary_aurora" {
147+
source = "../../"
148+
149+
providers = { aws = aws.secondary }
150+
151+
is_primary_cluster = false
152+
153+
name = local.name
154+
engine = aws_rds_global_cluster.this.engine
155+
engine_version = aws_rds_global_cluster.this.engine_version
156+
global_cluster_identifier = aws_rds_global_cluster.this.id
157+
source_region = local.primary.region
158+
instance_type = "db.r6g.large"
159+
kms_key_id = aws_kms_key.secondary.arn
160+
161+
vpc_id = module.secondary_vpc.vpc_id
162+
db_subnet_group_name = module.secondary_vpc.database_subnet_group_name
163+
create_security_group = true
164+
allowed_cidr_blocks = module.secondary_vpc.private_subnets_cidr_blocks
165+
166+
replica_count = 2
167+
skip_final_snapshot = true
168+
169+
depends_on = [
170+
module.primary_aurora
171+
]
172+
173+
tags = local.tags
174+
}

0 commit comments

Comments
 (0)