Skip to content

Instantly share code, notes, and snippets.

@joerx
Created January 29, 2018 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerx/6cba28d95988631cefe00ff8c41d1d41 to your computer and use it in GitHub Desktop.
Save joerx/6cba28d95988631cefe00ff8c41d1d41 to your computer and use it in GitHub Desktop.
RDS subnet group vs VPC
terraform {
required_version = ">= 0.11"
}
provider "aws" {
region = "${var.aws_region}"
version = "~> 1.6"
}
variable "aws_region" {
default = "ap-southeast-1"
}
variable "aws_azs" {
default = [
"ap-southeast-1a",
"ap-southeast-1b",
]
}
resource "aws_vpc" "vpc_one" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "VPC 1"
}
}
resource "aws_vpc" "vpc_two" {
cidr_block = "10.50.0.0/16"
tags = {
Name = "VPC 2"
}
}
resource "aws_subnet" "vpc_one" {
count = "${length(var.aws_azs)}"
vpc_id = "${aws_vpc.vpc_one.id}"
cidr_block = "10.0.${count.index}.0/24"
availability_zone = "${element(var.aws_azs, count.index)}"
tags = {
Name = "Subnet 1-${count.index}"
}
}
resource "aws_subnet" "vpc_two" {
count = "${length(var.aws_azs)}"
vpc_id = "${aws_vpc.vpc_two.id}"
cidr_block = "10.50.${count.index}.0/24"
availability_zone = "${element(var.aws_azs, count.index)}"
tags = {
Name = "Subnet 2-${count.index}"
}
}
resource "aws_db_subnet_group" "vpc_one" {
name = "sg_one"
subnet_ids = ["${aws_subnet.vpc_one.*.id}"]
tags = {
Name = "SG 1"
}
}
resource "aws_db_subnet_group" "vpc_two" {
name = "sg_two"
subnet_ids = ["${aws_subnet.vpc_two.*.id}"]
tags = {
Name = "SG 2"
}
}
resource "aws_db_parameter_group" "default" {
name = "rds-mysql"
family = "mysql5.6"
}
# the db_subnet_group_name can be freely changed between the existing subnets
resource "aws_db_instance" "default" {
allocated_storage = 10
storage_type = "gp2"
engine = "mysql"
engine_version = "5.6.37"
instance_class = "db.t2.micro"
name = "mydb"
username = "root"
password = "r00tr00t"
db_subnet_group_name = "${aws_db_subnet_group.vpc_one.name}"
parameter_group_name = "${aws_db_parameter_group.default.name}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment