Skip to content

Commit 9632fc2

Browse files
authored
Add Terraform resources (#1)
* Add Terraform resources * Fix LICENSE * Add README.md and tests * Fix variable names
1 parent 851f0b1 commit 9632fc2

File tree

7 files changed

+373
-59
lines changed

7 files changed

+373
-59
lines changed

.gitignore

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,9 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
1+
# Compiled files
2+
*.tfstate
3+
*.tfstate.backup
74

8-
# Runtime data
9-
pids
10-
*.pid
11-
*.seed
12-
*.pid.lock
13-
14-
# Directory for instrumented libs generated by jscoverage/JSCover
15-
lib-cov
16-
17-
# Coverage directory used by tools like istanbul
18-
coverage
19-
20-
# nyc test coverage
21-
.nyc_output
22-
23-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24-
.grunt
25-
26-
# Bower dependency directory (https://bower.io/)
27-
bower_components
28-
29-
# node-waf configuration
30-
.lock-wscript
31-
32-
# Compiled binary addons (http://nodejs.org/api/addons.html)
33-
build/Release
34-
35-
# Dependency directories
36-
node_modules/
37-
jspm_packages/
38-
39-
# Typescript v1 declaration files
40-
typings/
41-
42-
# Optional npm cache directory
43-
.npm
44-
45-
# Optional eslint cache
46-
.eslintcache
47-
48-
# Optional REPL history
49-
.node_repl_history
50-
51-
# Output of 'npm pack'
52-
*.tgz
53-
54-
# Yarn Integrity file
55-
.yarn-integrity
56-
57-
# dotenv environment variables file
58-
.env
5+
# Module directory
6+
.terraform/
597

8+
.idea
9+
*.iml

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {yyyy} {name of copyright owner}
189+
Copyright 2017 Cloud Posse, LLC
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,100 @@
11
# tf_rds
2-
Terraform Template for AWS RDS
2+
Terraform module for AWS RDS
3+
4+
5+
The module will create:
6+
* DB instance (MySQL, Postgres, SQL Server, Oracle)
7+
* DB Parameter Group
8+
* DB Subnet Group
9+
* DB Security Group
10+
* DNS Record in Route53 for the DB endpoint
11+
12+
13+
14+
## Input Variables
15+
16+
- `stage` - The deployment stage (_e.g._ `prod`, `staging`, `dev`)
17+
- `namespace` - The namespace of the application the DB instance belongs to (_e.g._ `global`, `shared`, or the name of your company like `cloudposse`)
18+
- `name` - The name of the application the DB instance belongs to
19+
- `dns_zone_id` - The ID of the DNS Zone in Route53 where a new DNS record will be created for the DB host name
20+
- `host_name` - The DB host name created in Route53
21+
- `security_group_ids` - The IDs of the security groups from which to allow `ingress` traffic to the DB instance
22+
- `database_name` - The name of the database (_e.g._ `wordpress`)
23+
- `database_user` - Admin user name (_e.g._ `admin`)
24+
- `database_password` - Admin password
25+
- `database_port` - Database port (_e.g._ `3306` for `MySQL`). Used in the DB Security Group to allow access to the DB instance from the provided `security_group_ids`
26+
- `multi_az` - Default `false`. Set to `true` for a multi-AZ deployment (recommended for production)
27+
- `storage_type` - One of `standard` (magnetic), `gp2` (general purpose SSD), or `io1` (provisioned IOPS SSD). Default `standard` (magnetic)
28+
- `iops` - The amount of provisioned IOPS. Setting this implies a storage_type of `io1`. Default is `0` if rds storage type is not `io1`
29+
- `allocated_storage` - The number of GBs to allocate for DB storage. Must be an integer, _e.g._ `10`
30+
- `engine` - Engine type, such as `mysql` or `postgres`
31+
- `engine_version` - DB Engine version, _e.g._ `9.5.4` for `Postgres`
32+
- `instance_class` - Instance class, _e.g._ `db.t2.micro`
33+
- `db_parameter_group` - DB Parameter Group, _e.g._ `mysql5.6` for MySQL, `postgres9.5` for `Postgres`
34+
- `publicly_accessible` - Determines if the DB instance can be publicly accessed from the Internet. Default `false`
35+
- `subnet_ids` - List of subnets IDs in the VPC, _e.g._ `["sb-1234567890", "sb-0987654321"]`
36+
- `vpc_id` - VPC ID the DB instance will be connected to
37+
- `auto_minor_version_upgrade` - Automatically upgrade minor version of the DB (eg. from Postgres 9.5.3 to Postgres 9.5.4). Default `true`
38+
- `allow_major_version_upgrade` - Allow upgrading of major version of database (eg. from Postgres 9.5.x to Postgres 9.6.x). Default `false`
39+
- `apply_immediately` - Specifies whether any database modifications are applied immediately, or during the next maintenance window. Default `false`
40+
- `maintenance_window` - The window to perform maintenance in. Default `"Mon:03:00-Mon:04:00"`
41+
- `skip_final_snapshot` - If `true` (default), DB won't be backed up before deletion
42+
- `copy_tags_to_snapshot` - Copy all tags from RDS database to snapshot. Default `true`
43+
- `backup_retention_period` - Backup retention period in days (default `0`). Must be `> 0` to enable backups
44+
- `backup_window` - When to perform DB snapshots. Default `"22:00-03:00"`. Can't overlap with the maintenance window
45+
46+
47+
48+
## Outputs
49+
50+
- `instance_id` - ID of the instance
51+
- `instance_address` - Address of the instance
52+
- `instance_endpoint` - DNS Endpoint of the instance
53+
- `subnet_group_id` - ID of the Subnet Group
54+
- `security_group_id` - ID of the Security Group
55+
- `parameter_group_id` - ID of the Parameter Group
56+
- `hostname` - DNS host name of the instance
57+
58+
59+
60+
## Usage
61+
62+
63+
```
64+
module "rds_instance" {
65+
source = "git::https://github.com/cloudposse/tf_rds.git?ref=tags/0.1.0"
66+
stage = "prod"
67+
namespace = "cloudposse"
68+
name = "app"
69+
dns_zone_id = "Z89FN1IW975KPE"
70+
host_name = "db"
71+
security_group_ids = ["sg-xxxxxxxx"]
72+
database_name = "wordpress"
73+
database_user = "admin"
74+
database_password = "xxxxxxxxxxxx"
75+
database_port = 3306
76+
multi_az = true
77+
storage_type = "gp2"
78+
allocated_storage = "100"
79+
engine = "mysql"
80+
engine_version = "5.7.17"
81+
instance_class = "db.t2.medium"
82+
db_parameter_group = "mysql5.6"
83+
publicly_accessible = false
84+
subnet_ids = ["sb-xxxxxxxxx", "sb-xxxxxxxxx"]
85+
vpc_id = "vpc-xxxxxxxx"
86+
auto_minor_version_upgrade = true
87+
allow_major_version_upgrade = false
88+
apply_immediately = false
89+
maintenance_window = "Mon:03:00-Mon:04:00"
90+
skip_final_snapshot = false
91+
copy_tags_to_snapshot = true
92+
backup_retention_period = 7
93+
backup_window = "22:00-03:00"
94+
}
95+
```
96+
97+
98+
# License
99+
100+
Apache 2 Licensed. See LICENSE for full details.

main.tf

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module "label" {
2+
source = "git::https://github.com/cloudposse/tf_label.git?ref=tags/0.2.0"
3+
namespace = "${var.namespace}"
4+
name = "${var.name}"
5+
stage = "${var.stage}"
6+
}
7+
8+
resource "aws_db_instance" "default" {
9+
identifier = "${module.label.id}"
10+
name = "${var.database_name}"
11+
username = "${var.database_user}"
12+
password = "${var.database_password}"
13+
port = "${var.database_port}"
14+
engine = "${var.engine}"
15+
engine_version = "${var.engine_version}"
16+
instance_class = "${var.instance_class}"
17+
allocated_storage = "${var.allocated_storage}"
18+
vpc_security_group_ids = ["${aws_security_group.default.id}"]
19+
db_subnet_group_name = "${aws_db_subnet_group.default.name}"
20+
parameter_group_name = "${aws_db_parameter_group.default.name}"
21+
multi_az = "${var.multi_az}"
22+
storage_type = "${var.storage_type}"
23+
iops = "${var.iops}"
24+
publicly_accessible = "${var.publicly_accessible}"
25+
allow_major_version_upgrade = "${var.allow_major_version_upgrade}"
26+
auto_minor_version_upgrade = "${var.auto_minor_version_upgrade}"
27+
apply_immediately = "${var.apply_immediately}"
28+
maintenance_window = "${var.maintenance_window}"
29+
skip_final_snapshot = "${var.skip_final_snapshot}"
30+
copy_tags_to_snapshot = "${var.copy_tags_to_snapshot}"
31+
backup_retention_period = "${var.backup_retention_period}"
32+
backup_window = "${var.backup_window}"
33+
tags = "${module.label.tags}"
34+
}
35+
36+
resource "aws_db_parameter_group" "default" {
37+
name = "${module.label.id}"
38+
family = "${var.db_parameter_group}"
39+
tags = "${module.label.tags}"
40+
}
41+
42+
resource "aws_db_subnet_group" "default" {
43+
name = "${module.label.id}"
44+
subnet_ids = ["${var.subnet_ids}"]
45+
tags = "${module.label.tags}"
46+
}
47+
48+
resource "aws_security_group" "default" {
49+
name = "${module.label.id}"
50+
description = "Allow inbound traffic from the security groups"
51+
vpc_id = "${var.vpc_id}"
52+
53+
ingress {
54+
from_port = "${var.database_port}"
55+
to_port = "${var.database_port}"
56+
protocol = "tcp"
57+
security_groups = ["${var.security_group_ids}"]
58+
}
59+
60+
egress {
61+
from_port = 0
62+
to_port = 0
63+
protocol = "-1"
64+
cidr_blocks = ["0.0.0.0/0"]
65+
}
66+
67+
tags = "${module.label.tags}"
68+
}
69+
70+
module "dns_host_name" {
71+
source = "git::https://github.com/cloudposse/tf_hostname.git?ref=tags/0.1.0"
72+
namespace = "${var.namespace}"
73+
name = "${var.host_name}"
74+
stage = "${var.stage}"
75+
zone_id = "${var.dns_zone_id}"
76+
records = ["${aws_db_instance.default.endpoint}"]
77+
}

outputs.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
output "instance_id" {
2+
value = "${aws_db_instance.default.id}"
3+
}
4+
5+
output "instance_address" {
6+
value = "${aws_db_instance.default.address}"
7+
}
8+
9+
output "instance_endpoint" {
10+
value = "${aws_db_instance.default.endpoint}"
11+
}
12+
13+
output "subnet_group_id" {
14+
value = "${aws_db_subnet_group.default.id}"
15+
}
16+
17+
output "security_group_id" {
18+
value = "${aws_security_group.default.id}"
19+
}
20+
21+
output "parameter_group_id" {
22+
value = "${aws_db_parameter_group.default.id}"
23+
}
24+
25+
output "hostname" {
26+
value = "${module.dns_host_name.hostname}"
27+
}

tests/rds_instance.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module "rds_instance" {
2+
source = "git::https://github.com/cloudposse/tf_rds.git?ref=tags/0.1.0"
3+
stage = "prod"
4+
namespace = "cloudposse"
5+
name = "app"
6+
dns_zone_id = "Z89FN1IW975KPE"
7+
host_name = "db"
8+
security_group_ids = ["sg-xxxxxxxx"]
9+
database_name = "wordpress"
10+
database_user = "admin"
11+
database_password = "xxxxxxxxxxxx"
12+
database_port = 3306
13+
multi_az = true
14+
storage_type = "gp2"
15+
allocated_storage = "100"
16+
engine = "mysql"
17+
engine_version = "5.7.17"
18+
instance_class = "db.t2.medium"
19+
db_parameter_group = "mysql5.6"
20+
publicly_accessible = false
21+
subnet_ids = ["sb-xxxxxxxxx", "sb-xxxxxxxxx"]
22+
vpc_id = "vpc-xxxxxxxx"
23+
auto_minor_version_upgrade = true
24+
allow_major_version_upgrade = false
25+
apply_immediately = false
26+
maintenance_window = "Mon:03:00-Mon:04:00"
27+
skip_final_snapshot = false
28+
copy_tags_to_snapshot = true
29+
backup_retention_period = 7
30+
backup_window = "22:00-03:00"
31+
}

0 commit comments

Comments
 (0)