This repository was archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathmain.tf
More file actions
209 lines (170 loc) · 5.36 KB
/
main.tf
File metadata and controls
209 lines (170 loc) · 5.36 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
variable "name" {
description = "RDS instance name"
}
variable "environment" {
description = "Environment tag, e.g prod, if missing defaults to name"
}
variable "engine" {
description = "Database engine: mysql, postgres, etc."
default = "postgres"
}
variable "engine_version" {
description = "Database version"
default = "9.6.1"
}
variable "port" {
description = "Port for database to listen on"
default = 5432
}
variable "database" {
description = "The database name for the RDS instance (if not specified, `var.name` will be used)"
default = ""
}
variable "username" {
description = "The username for the RDS instance (if not specified, `var.name` will be used)"
default = ""
}
variable "password" {
description = "Postgres user password"
}
variable "multi_az" {
description = "If true, database will be placed in multiple AZs for HA"
default = false
}
variable "backup_retention_period" {
description = "Backup retention, in days"
default = 5
}
variable "backup_window" {
description = "Time window for backups."
default = "00:00-01:00"
}
variable "maintenance_window" {
description = "Time window for maintenance."
default = "Mon:01:00-Mon:02:00"
}
variable "monitoring_interval" {
description = "Seconds between enhanced monitoring metric collection. 0 disables enhanced monitoring."
default = "0"
}
variable "monitoring_role_arn" {
description = "The ARN for the IAM role that permits RDS to send enhanced monitoring metrics to CloudWatch Logs. Required if monitoring_interval > 0."
default = ""
}
variable "apply_immediately" {
description = "If false, apply changes during maintenance window"
default = true
}
variable "allow_major_version_upgrade" {
description = "If true, major version upgrades are allowed"
default = false
}
variable "instance_class" {
description = "Underlying instance type"
default = "db.t2.micro"
}
variable "storage_type" {
description = "Storage type: standard, gp2, or io1"
default = "gp2"
}
variable "allocated_storage" {
description = "Disk size, in GB"
default = 10
}
variable "publicly_accessible" {
description = "If true, the RDS instance will be open to the internet"
default = false
}
variable "vpc_id" {
description = "The VPC ID to use"
}
variable "ingress_allow_security_groups" {
description = "A list of security group IDs to allow traffic from"
type = "list"
default = []
}
variable "ingress_allow_cidr_blocks" {
description = "A list of CIDR blocks to allow traffic from"
type = "list"
default = []
}
variable "subnet_ids" {
description = "A list of subnet IDs"
type = "list"
}
locals {
environment = "${coalesce(var.environment, var.name)}"
}
resource "aws_security_group" "main" {
name = "${var.name}-rds"
description = "Allows traffic to RDS from other security groups"
vpc_id = "${var.vpc_id}"
ingress {
from_port = "${var.port}"
to_port = "${var.port}"
protocol = "TCP"
security_groups = ["${var.ingress_allow_security_groups}"]
}
ingress {
from_port = "${var.port}"
to_port = "${var.port}"
protocol = "TCP"
cidr_blocks = ["${var.ingress_allow_cidr_blocks}"]
}
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "RDS ${var.name}"
Environment = "${local.environment}"
}
}
resource "aws_db_subnet_group" "main" {
name = "${var.name}"
description = "RDS subnet group"
subnet_ids = ["${var.subnet_ids}"]
tags {
Name = "RDS Subnet ${var.name}"
Environment = "${local.environment}"
}
}
resource "aws_db_instance" "main" {
identifier = "${var.name}"
# Database
engine = "${var.engine}"
engine_version = "${var.engine_version}"
allow_major_version_upgrade = "${var.allow_major_version_upgrade}"
username = "${coalesce(var.username, var.name)}"
password = "${var.password}"
multi_az = "${var.multi_az}"
name = "${coalesce(var.database, var.name)}"
# Backups / maintenance
backup_retention_period = "${var.backup_retention_period}"
backup_window = "${var.backup_window}"
maintenance_window = "${var.maintenance_window}"
monitoring_interval = "${var.monitoring_interval}"
monitoring_role_arn = "${var.monitoring_role_arn}"
apply_immediately = "${var.apply_immediately}"
final_snapshot_identifier = "${var.name}-finalsnapshot"
# Hardware
instance_class = "${var.instance_class}"
storage_type = "${var.storage_type}"
allocated_storage = "${var.allocated_storage}"
# Network / security
db_subnet_group_name = "${aws_db_subnet_group.main.id}"
vpc_security_group_ids = ["${aws_security_group.main.id}"]
publicly_accessible = "${var.publicly_accessible}"
tags {
Name = "RDS Instance ${var.name}"
Environment = "${local.environment}"
}
}
output "addr" {
value = "${aws_db_instance.main.engine}://${aws_db_instance.main.username}:${aws_db_instance.main.password}@${aws_db_instance.main.endpoint}"
}
output "url" {
value = "${aws_db_instance.main.engine}://${aws_db_instance.main.username}:${aws_db_instance.main.password}@${aws_db_instance.main.endpoint}/${aws_db_instance.main.name}"
}