-
Notifications
You must be signed in to change notification settings - Fork 369
/
main.tf
122 lines (102 loc) · 3.68 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
resource "random_password" "postgres_password" {
length = 99
special = true
override_special = "-+=_^~,."
}
resource "random_string" "postgres_username" {
length = 62
special = false
}
resource "random_integer" "postgres_port" {
min = 1024
max = 49151
}
resource "random_integer" "postgres_random_identifier_suffix" {
min = 0
max = 999999999999
}
data "aws_availability_zones" "available" {
state = "available"
}
resource "aws_subnet" "postgres_subnet" {
count = 2
vpc_id = var.vpc_id
cidr_block = cidrsubnet(var.vpc_cidr_block, 8, count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
tags = {
Name = "${var.environment}-postgres-subnet-${count.index}"
Environment = var.environment
}
}
resource "aws_db_subnet_group" "postgres_subnet_group" {
name = "${var.environment}-postgres-subnet-group"
subnet_ids = aws_subnet.postgres_subnet.*.id
tags = {
Name = "${var.environment}-postgres-subnet-group"
Environment = var.environment
}
}
resource "aws_route_table_association" "postgres_route_table_association" {
count = length(aws_subnet.postgres_subnet)
subnet_id = aws_subnet.postgres_subnet[count.index].id
route_table_id = var.route_table_id
}
resource "aws_security_group" "postgres_security_group" {
name = "${var.environment}-postgres-security-group"
vpc_id = var.vpc_id
revoke_rules_on_delete = true
ingress {
from_port = random_integer.postgres_port.result
to_port = random_integer.postgres_port.result
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
lifecycle {
create_before_destroy = true
}
tags = {
Name = "${var.environment}-postgres-security-group"
Environment = var.environment
}
}
resource "aws_db_instance" "postgres" {
identifier = "${var.environment}-postgres-${random_integer.postgres_random_identifier_suffix.result}"
storage_type = "gp2"
allocated_storage = var.allocated_storage
engine = "postgres"
engine_version = var.engine_version
instance_class = var.instance_class
username = "u${random_string.postgres_username.result}" # has to start with a letter
password = random_password.postgres_password.result
db_name = "tabnews"
port = random_integer.postgres_port.result
publicly_accessible = true
multi_az = false
max_allocated_storage = var.max_allocated_storage
allow_major_version_upgrade = true
auto_minor_version_upgrade = true
db_subnet_group_name = aws_db_subnet_group.postgres_subnet_group.name
vpc_security_group_ids = [aws_security_group.postgres_security_group.id]
apply_immediately = true
backup_retention_period = var.backup_retention_period
backup_window = "04:00-05:00"
maintenance_window = "wed:06:00-wed:07:00"
skip_final_snapshot = var.skip_final_snapshot
final_snapshot_identifier = "${var.environment}-postgres-final-snapshot-${random_integer.postgres_random_identifier_suffix.result}"
copy_tags_to_snapshot = true
deletion_protection = var.deletion_protection
delete_automated_backups = var.delete_automated_backups
tags = {
Name = "${var.environment}-postgres"
Environment = var.environment
}
}