forked from benoutram/terraform-aws-vpc-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrds.tf
79 lines (69 loc) · 2.67 KB
/
rds.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
# Create subnets in each availability zone for RDS, each with address blocks within the VPC.
resource "aws_subnet" "rds" {
count = "${length(data.aws_availability_zones.available.names)}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "10.0.${length(data.aws_availability_zones.available.names) + count.index}.0/24"
map_public_ip_on_launch = true
availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}"
tags {
Name = "rds-${element(data.aws_availability_zones.available.names, count.index)}"
}
}
# Create a subnet group with all of our RDS subnets. The group will be applied to the database instance.
resource "aws_db_subnet_group" "default" {
name = "${var.rds_instance_identifier}-subnet-group"
description = "Terraform example RDS subnet group"
subnet_ids = ["${aws_subnet.rds.*.id}"]
}
# Create a RDS security group in the VPC which our database will belong to.
resource "aws_security_group" "rds" {
name = "terraform_rds_security_group"
description = "Terraform example RDS Mysql server"
vpc_id = "${aws_vpc.vpc.id}"
# Keep the instance private by only allowing traffic from the web server.
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
security_groups = ["${aws_security_group.default.id}"]
}
# Allow all outbound traffic.
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "terraform-example-rds-security-group"
}
}
# Create a RDS MySQL database instance in the VPC with our RDS subnet group and security group.
resource "aws_db_instance" "default" {
identifier = "${var.rds_instance_identifier}"
allocated_storage = 5
engine = "mysql"
engine_version = "5.6.35"
instance_class = "db.t2.micro"
name = "${var.database_name}"
username = "${var.database_user}"
password = "${var.database_password}"
db_subnet_group_name = "${aws_db_subnet_group.default.id}"
vpc_security_group_ids = ["${aws_security_group.rds.id}"]
skip_final_snapshot = true
final_snapshot_identifier = "Ignore"
}
# Manage the MySQL configuration by creating a parameter group.
resource "aws_db_parameter_group" "default" {
name = "${var.rds_instance_identifier}-param-group"
description = "Terraform example parameter group for mysql5.6"
family = "mysql5.6"
parameter {
name = "character_set_server"
value = "utf8"
}
parameter {
name = "character_set_client"
value = "utf8"
}
}