-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
81 lines (70 loc) · 2.79 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Data lookup to get our CIDR
data "aws_vpc" "this" {
id = "${var.vpc_id}"
}
# The randomly generated database password
resource "random_string" "this" {
length = 12
special = false
}
# DB Instance
resource "aws_db_instance" "this" {
identifier = "${replace(var.name,"-","")}"
username = "${local.username}"
password = "${random_string.this.result}"
port = "${local.port}"
engine = "${var.engine}"
engine_version = "${local.engine_version}"
instance_class = "${var.instance_type}"
allocated_storage = "${var.allocated_storage}"
storage_encrypted = "${var.instance_type == "db.t2.micro" ? false : true}"
vpc_security_group_ids = ["${compact(concat(list(aws_security_group.this.id), var.security_group_ids))}"]
db_subnet_group_name = "${aws_db_subnet_group.this.name}"
parameter_group_name = "${aws_db_parameter_group.this.name}"
multi_az = "${var.is_live ? true : false }"
storage_type = "gp2"
publicly_accessible = "false"
snapshot_identifier = "${var.restore_from_snapshot}"
allow_major_version_upgrade = "${var.is_live ? true : false }"
auto_minor_version_upgrade = "${var.is_live ? true : false }"
apply_immediately = "false"
maintenance_window = "${var.maintenance_window}"
skip_final_snapshot = "${var.is_live ? false : true }"
copy_tags_to_snapshot = "true"
backup_retention_period = "${var.is_live ? 7 : 0 }"
backup_window = "${var.backup_window}"
final_snapshot_identifier = "${var.name}"
tags = "${var.tags}"
}
# DB Parameter Groups
resource "aws_db_parameter_group" "this" {
name = "${var.name}"
family = "${local.engine_family}"
parameter = "${var.db_parameter}"
tags = "${var.tags}"
}
# The DB subnet group
resource "aws_db_subnet_group" "this" {
name = "${var.name}"
subnet_ids = ["${var.subnet_ids}"]
tags = "${var.tags}"
}
# A default security group we'll create to allow everyone in our CIDR access
resource "aws_security_group" "this" {
name = "${var.name}-rds-allow-cidr"
description = "Allow inbound traffic from the CIDR"
vpc_id = "${var.vpc_id}"
ingress {
from_port = "${local.port}"
to_port = "${local.port}"
protocol = "tcp"
cidr_blocks = ["${data.aws_vpc.this.cidr_block}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = "${merge(var.tags, map("Name", format("%s-rds-allow-cidr", var.name)))}"
}