-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
137 lines (123 loc) · 4.38 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
locals {
id = "${replace(var.name, " ", "-")}"
}
# -----------------------------------------------
# Create Private subnets
# -----------------------------------------------
resource "aws_subnet" "this" {
count = "${length(var.azs)}"
vpc_id = "${data.aws_vpc.this.id}"
cidr_block = "${cidrsubnet(data.aws_vpc.this.cidr_block, var.newbits, var.netnum + count.index)}"
availability_zone = "${element(var.azs, count.index)}"
map_public_ip_on_launch = false
tags = "${merge(var.tags, map("Name", "${var.name} DB ${count.index}"))}"
}
resource "aws_route_table_association" "this" {
count = "${length(var.azs)}"
subnet_id = "${element(aws_subnet.this.*.id, count.index)}"
route_table_id = "${var.rt_id}"
}
# ------------------------------------------------------------------------------------------
# CREATE DB SUBNET GROUP
# -------------------------------------------------------------------------------------------
resource "aws_db_subnet_group" "this" {
name = "${lower(local.id)}"
subnet_ids = "${aws_subnet.this.*.id}"
description = "${var.name} DB subnet group"
tags = "${merge(var.tags, map("Name", "${var.name}"))}"
}
# -------------------------------
# CREATE RDS
# -------------------------------
resource "aws_db_instance" "this" {
count = "${var.cluster ? 0 : 1 }"
identifier = "${lower(local.id)}"
allocated_storage = "${var.storage}"
storage_type = "gp2"
engine = "${var.engine}"
engine_version = "${var.engine_version}"
instance_class = "${var.type}"
name = "${var.db_name}"
username = "${var.db_user}"
password = "${var.db_password}"
snapshot_identifier = "${var.db_snapshot_identifier}"
db_subnet_group_name = "${aws_db_subnet_group.this.name}"
vpc_security_group_ids = ["${aws_security_group.this.id}"]
final_snapshot_identifier = "${local.id}"
tags = "${merge(var.tags, map("Name", var.name))}"
copy_tags_to_snapshot = true
backup_retention_period = 7
storage_encrypted = true
}
resource "aws_rds_cluster_instance" "this" {
count = "${var.cluster ? 1 : 0 }"
engine = "${var.engine}"
identifier = "${lower(local.id)}"
cluster_identifier = "${aws_rds_cluster.this.0.id}"
instance_class = "${var.type}"
db_subnet_group_name = "${aws_db_subnet_group.this.name}"
}
resource "aws_rds_cluster" "this" {
count = "${var.cluster ? 1 : 0 }"
engine = "${var.engine}"
cluster_identifier = "${lower(local.id)}"
availability_zones = "${var.azs}"
database_name = "${var.db_name}"
master_username = "${var.db_user}"
master_password = "${var.db_snapshot_identifier == "" ? var.db_password : "" }"
db_subnet_group_name = "${aws_db_subnet_group.this.name}"
vpc_security_group_ids = ["${aws_security_group.this.id}"]
final_snapshot_identifier = "${local.id}"
}
# ---------------------------------------
# Network ACL DB
# ---------------------------------------
resource "aws_network_acl" "this" {
vpc_id = "${data.aws_vpc.this.id}"
subnet_ids = "${aws_subnet.this.*.id}"
tags = "${merge(var.tags, map("Name", "${var.name} DB"))}"
}
# ---------------------------------------
# Network ACL Inbound/Outbound DB
# ---------------------------------------
resource "aws_network_acl_rule" "inbound" {
count = "${length(var.azs)}"
network_acl_id = "${aws_network_acl.this.id}"
rule_number = "${(count.index+1)*100}"
egress = false
protocol = "tcp"
rule_action = "allow"
cidr_block = "${element(var.access_cidrs, count.index)}"
from_port = "${var.port}"
to_port = "${var.port}"
}
resource "aws_network_acl_rule" "outbound" {
count = "${length(var.azs)}"
network_acl_id = "${aws_network_acl.this.id}"
rule_number = "${(count.index+1)*100}"
egress = true
protocol = "tcp"
rule_action = "allow"
cidr_block = "${element(var.access_cidrs, count.index)}"
from_port = 1024
to_port = 65535
}
# Security group Database access
resource "aws_security_group" "this" {
name = "${local.id}-DB"
description = "Access to DB port"
vpc_id = "${data.aws_vpc.this.id}"
ingress {
from_port = "${var.port}"
to_port = "${var.port}"
protocol = "tcp"
security_groups = "${var.access_sg_ids}"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = "${merge(var.tags, map("Name", "${var.name} DB"))}"
}