Creating AWS VPC using Terraform
By: Gyanesh Sharma
Tags: AWS, Terraform

Statement of task : We have to create a web portal for our company with all the security as much as possible.

So, we use Wordpress software with dedicated database server.

Database should not be accessible from the outside world for security purposes.

We only need to public the WordPress to clients.

Steps for better understanding of task :

1) Write a Infrastructure as code using terraform, which automatically create a VPC.

2) In that VPC we have to create 2 subnets:

a) public subnet [ Accessible for Public World! ]

b) private subnet [ Restricted for Public World! ]

3) Create a public facing internet gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC.

4) Create a routing table for Internet gateway so that instance can connect to outside world, update and associate it with public subnet.

5) Launch an ec2 instance which has Wordpress setup already having the security group allowing port 80 so that our client can connect to our wordpress site.

Also attach the key to instance for further login into it.

6) Launch an ec2 instance which has MYSQL setup already with security group allowing port 3306 in private subnet so that our wordpress vm can connect with the same. Also attach the key with the same.

Steps to perform the task :

Step 1 : Create VPC using terraform

resource "aws_vpc" "myvpc" {

cidr_block = "192.168.0.0/16"

instance_tenancy = "default"

enable_dns_hostnames = "true"

tags = {

Name = "myvpc"

} }



Step 2 : Create 2 subnets [private and public]

(i) Public Subnet

resource "aws_subnet" "publicsub" {

vpc_id = "${aws_vpc.myvpc.id}"

cidr_block = "192.168.0.0/24"

availability_zone = "ap-south-1a"

map_public_ip_on_launch = "true"

depends_on = [

aws_vpc.myvpc,

]

tags = {

Name = "publicsub"

} }

(ii) Private Subnet

resource "aws_subnet" "privatesub" {

vpc_id = "${aws_vpc.myvpc.id}"

cidr_block = "192.168.1.0/24"

availability_zone = "ap-south-1b"

map_public_ip_on_launch = "false"

depends_on = [

aws_vpc.myvpc,

]

tags = {

Name = "privatesub"

}

}

Step 3 : Create a public facing internet gateway for connecting VPC/Network with the internet world and also attach gateway to VPC

resource "aws_internet_gateway" "nicrogg" {

vpc_id = "${aws_vpc.myvpc.id}"

depends_on = [

aws_vpc.myvpc,

]

tags = {

Name = "nicrogg"

} }

Step 4 : Create routing table for internet gateway

resource "aws_route_table" "nicro" {

vpc_id = "${aws_vpc.myvpc.id}"



route {

cidr_block = "0.0.0.0/0"

gateway_id = "${aws_internet_gateway.nicrogg.id}"

}

depends_on = [

aws_vpc.myvpc,

]

tags = {

Name = "nicro"

} }

With associate subnet

resource "aws_route_table_association" "associate" {

subnet_id = "${aws_subnet.publicsub.id}"

route_table_id = "${aws_route_table.nicro.id}"

depends_on = [

aws_subnet.publicsub,

]

}

Step 5 :

(i) Create security group for the wordpress

resource "aws_security_group" "mywpsg" {

name = "mywpsg"

description = "Allow TLS inbound traffic"

vpc_id = "${aws_vpc.myvpc.id}"

ingress {

description = "http"

from_port = 80

to_port = 80

protocol = "tcp"

cidr_blocks = ["0.0.0.0/0"]

}

ingress {

description = "ssh"

from_port = 22

to_port = 22

protocol = "tcp"

cidr_blocks = ["0.0.0.0/0"]

}

egress {

from_port = 0

to_port = 0

protocol = "-1"

cidr_blocks = ["0.0.0.0/0"]

}

depends_on = [

aws_vpc.myvpc,

]

tags = {

Name = "mywpsg"

} }

(ii) EC2 instance with wordpress having security group already configured

resource "aws_instance" "wordpressOS" {

ami = "ami-004a955bfb611bf13"

instance_type = "t2.micro"

key_name = "keycloud"

subnet_id = aws_subnet.publicsub.id

vpc_security_group_ids = [ "${aws_security_group.mywpsg.id}" ]

tags = {

Name = "wordpressOS"



}

}

Step 6 :

(i) Create security group for MySQL

resource "aws_security_group" "mysqlsg" {

name = "basic"

description = "Allow TLS inbound traffic"

vpc_id = "${aws_vpc.mvpc.id}"

ingress {

description = "mysql"

from_port = 3306

to_port = 3306

protocol = "tcp"

cidr_blocks = ["0.0.0.0/0"]

}

depends_on = [

aws_vpc.myvpc,

]

tags = {

Name = "mysqlsg"

} }

(ii)EC2 instance with MySQL having security group already configured

resource "aws_instance" "mysqlOS" {

ami = "ami-08706cb5f68222d09"

instance_type = "t2.micro"

key_name = "keycloud"

subnet_id = aws_subnet.privatesub.id

vpc_security_group_ids = [ "${aws_security_group.mysqlsg.id}" ]

tags = {

Name = "mysqlOS"

} }