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
165 lines (135 loc) · 4.19 KB
/
main.tf
File metadata and controls
165 lines (135 loc) · 4.19 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
variable "name" {
description = "RDS name and Postgres username"
}
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 "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 "apply_immediately" {
description = "If false, apply changes during maintenance window"
default = true
}
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"
}
resource "aws_security_group_rule" "main-ingress-cidrs" {
security_group_id = "${aws_security_group.main.id}"
type = "ingress"
cidr_blocks = ["${var.ingress_allow_cidr_blocks}"]
from_port = "${var.port}"
to_port = "${var.port}"
protocol = "TCP"
}
resource "aws_security_group_rule" "main-ingress-sgs" {
security_group_id = "${aws_security_group.main.id}"
type = "ingress"
count = "${length(var.ingress_allow_security_groups)}"
source_security_group_id = "${element(var.ingress_allow_security_groups, count.index)}"
from_port = "${var.port}"
to_port = "${var.port}"
protocol = "TCP"
}
resource "aws_security_group_rule" "main-egress-all" {
security_group_id = "${aws_security_group.main.id}"
type = "egress"
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group" "main" {
name = "${var.name}-rds"
description = "Allows traffic to RDS from other security groups"
vpc_id = "${var.vpc_id}"
tags {
Name = "RDS (${var.name})"
}
}
resource "aws_db_subnet_group" "main" {
name = "${var.name}"
description = "RDS subnet group"
subnet_ids = ["${var.subnet_ids}"]
}
resource "aws_db_instance" "main" {
identifier = "${var.name}"
# Database
engine = "${var.engine}"
engine_version = "${var.engine_version}"
username = "${var.name}"
password = "${var.password}"
multi_az = "${var.multi_az}"
name = "${var.name}"
# Backups / maintenance
backup_retention_period = "${var.backup_retention_period}"
backup_window = "${var.backup_window}"
maintenance_window = "${var.maintenance_window}"
apply_immediately = "${var.apply_immediately}"
# 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}"
}
output "addr" {
value = "postgres://${aws_db_instance.main.username}:${aws_db_instance.main.password}@${aws_db_instance.main.endpoint}"
}