diff --git a/demo-12/instance.tf b/demo-12/instance.tf new file mode 100644 index 0000000..d3abc89 --- /dev/null +++ b/demo-12/instance.tf @@ -0,0 +1,14 @@ +resource "aws_instance" "example" { + ami = "${lookup(var.AMIS, var.AWS_REGION)}" + instance_type = "t2.micro" + + # the VPC subnet + subnet_id = "${aws_subnet.main-public-1.id}" + + # the security group + vpc_security_group_ids = ["${aws_security_group.example-instance.id}"] + + # the public SSH key + key_name = "${aws_key_pair.mykeypair.key_name}" + +} diff --git a/demo-12/key.tf b/demo-12/key.tf new file mode 100644 index 0000000..0d3c220 --- /dev/null +++ b/demo-12/key.tf @@ -0,0 +1,4 @@ +resource "aws_key_pair" "mykeypair" { + key_name = "mykeypair" + public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}" +} diff --git a/demo-12/provider.tf b/demo-12/provider.tf new file mode 100644 index 0000000..ded6d8c --- /dev/null +++ b/demo-12/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "${var.AWS_REGION}" +} diff --git a/demo-12/rds.tf b/demo-12/rds.tf new file mode 100644 index 0000000..12e2958 --- /dev/null +++ b/demo-12/rds.tf @@ -0,0 +1,39 @@ +resource "aws_db_subnet_group" "mariadb-subnet" { + name = "mariadb-subnet" + description = "RDS subnet group" + subnet_ids = ["${aws_subnet.main-private-1.id}","${aws_subnet.main-private-2.id}"] +} + +resource "aws_db_parameter_group" "mariadb-parameters" { + name = "mariadb-parameters" + family = "mariadb10.1" + description = "MariaDB parameter group" + + parameter { + name = "max_allowed_packet" + value = "16777216" + } + +} + + +resource "aws_db_instance" "mariadb" { + allocated_storage = 100 # 100 GB of storage, gives us more IOPS than a lower number + engine = "mariadb" + engine_version = "10.1.14" + instance_class = "db.t2.small" # use micro if you want to use the free tier + identifier = "mariadb" + name = "mariadb" + username = "root" # username + password = "${var.RDS_PASSWORD}" # password + db_subnet_group_name = "${aws_db_subnet_group.mariadb-subnet.name}" + parameter_group_name = "mariadb-parameters" + multi_az = "false" # set to true to have high availability: 2 instances synchronized with each other + vpc_security_group_ids = ["${aws_security_group.allow-mariadb.id}"] + storage_type = "gp2" + backup_retention_period = 30 # how long you’re going to keep your backups + availability_zone = "${aws_subnet.main-private-1.availability_zone}" # prefered AZ + tags { + Name = "mariadb-instance" + } +} diff --git a/demo-12/securitygroup.tf b/demo-12/securitygroup.tf new file mode 100644 index 0000000..ab7eca5 --- /dev/null +++ b/demo-12/securitygroup.tf @@ -0,0 +1,44 @@ +resource "aws_security_group" "example-instance" { + vpc_id = "${aws_vpc.main.id}" + name = "allow-ssh" + description = "security group that allows ssh and all egress traffic" + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } +tags { + Name = "example-instance" + } +} + +resource "aws_security_group" "allow-mariadb" { + vpc_id = "${aws_vpc.main.id}" + name = "allow-mariadb" + description = "allow-mariadb" + ingress { + from_port = 3306 + to_port = 3306 + protocol = "tcp" + security_groups = ["${aws_security_group.example-instance.id}"] # allowing access from our example instance + } + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + self = true + } + tags { + Name = "allow-mariadb" + } +} + diff --git a/demo-12/vars.tf b/demo-12/vars.tf new file mode 100644 index 0000000..d4a15c5 --- /dev/null +++ b/demo-12/vars.tf @@ -0,0 +1,18 @@ +variable "AWS_REGION" { + default = "eu-west-1" +} +variable "PATH_TO_PRIVATE_KEY" { + default = "mykey" +} +variable "PATH_TO_PUBLIC_KEY" { + default = "mykey.pub" +} +variable "AMIS" { + type = "map" + default = { + us-east-1 = "ami-13be557e" + us-west-2 = "ami-06b94666" + eu-west-1 = "ami-844e0bf7" + } +} +variable "RDS_PASSWORD" { } diff --git a/demo-12/vpc.tf b/demo-12/vpc.tf new file mode 100644 index 0000000..1325feb --- /dev/null +++ b/demo-12/vpc.tf @@ -0,0 +1,110 @@ +# Internet VPC +resource "aws_vpc" "main" { + cidr_block = "10.0.0.0/16" + instance_tenancy = "default" + enable_dns_support = "true" + enable_dns_hostnames = "true" + enable_classiclink = "false" + tags { + Name = "main" + } +} + + +# Subnets +resource "aws_subnet" "main-public-1" { + vpc_id = "${aws_vpc.main.id}" + cidr_block = "10.0.1.0/24" + map_public_ip_on_launch = "true" + availability_zone = "eu-west-1a" + + tags { + Name = "main-public-1" + } +} +resource "aws_subnet" "main-public-2" { + vpc_id = "${aws_vpc.main.id}" + cidr_block = "10.0.2.0/24" + map_public_ip_on_launch = "true" + availability_zone = "eu-west-1b" + + tags { + Name = "main-public-2" + } +} +resource "aws_subnet" "main-public-3" { + vpc_id = "${aws_vpc.main.id}" + cidr_block = "10.0.3.0/24" + map_public_ip_on_launch = "true" + availability_zone = "eu-west-1c" + + tags { + Name = "main-public-3" + } +} +resource "aws_subnet" "main-private-1" { + vpc_id = "${aws_vpc.main.id}" + cidr_block = "10.0.4.0/24" + map_public_ip_on_launch = "false" + availability_zone = "eu-west-1a" + + tags { + Name = "main-private-1" + } +} +resource "aws_subnet" "main-private-2" { + vpc_id = "${aws_vpc.main.id}" + cidr_block = "10.0.5.0/24" + map_public_ip_on_launch = "false" + availability_zone = "eu-west-1b" + + tags { + Name = "main-private-2" + } +} +resource "aws_subnet" "main-private-3" { + vpc_id = "${aws_vpc.main.id}" + cidr_block = "10.0.6.0/24" + map_public_ip_on_launch = "false" + availability_zone = "eu-west-1c" + + tags { + Name = "main-private-3" + } +} + +# Internet GW +resource "aws_internet_gateway" "main-gw" { + vpc_id = "${aws_vpc.main.id}" + + tags { + Name = "main" + } +} + +# route tables +resource "aws_route_table" "main-public" { + vpc_id = "${aws_vpc.main.id}" + route { + cidr_block = "0.0.0.0/0" + gateway_id = "${aws_internet_gateway.main-gw.id}" + } + + tags { + Name = "main-public-1" + } +} + +# route associations public +resource "aws_route_table_association" "main-public-1-a" { + subnet_id = "${aws_subnet.main-public-1.id}" + route_table_id = "${aws_route_table.main-public.id}" +} +resource "aws_route_table_association" "main-public-2-a" { + subnet_id = "${aws_subnet.main-public-2.id}" + route_table_id = "${aws_route_table.main-public.id}" +} +resource "aws_route_table_association" "main-public-3-a" { + subnet_id = "${aws_subnet.main-public-3.id}" + route_table_id = "${aws_route_table.main-public.id}" +}