Launching a VPC network for deploying WordPress with Database on AWS using Terraform

MRIDUL MARKANDEY
4 min readNov 19, 2020

Task-

1) Write an Infrastructure as code using Terraform, which automatically creates 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.

Note: Wordpress instance has to be part of public subnet so that our client can connect our site.

mysql instance has to be part of private subnet so that outside world can’t connect to it.

Don’t forgot to add auto ip assign and auto dns name assignment option to be enabled.

1. Creating a VPC

resource "aws_vpc" "myvpc" {
cidr_block = "10.5.0.0/16"
enable_dns_hostnames = true
tags = {
Name = "main"
}
}

2. Create a Public Subnet

resource "aws_subnet" "alpha-1a" {
vpc_id = "${aws_vpc.myvpc.id}"
availability_zone = "ap-south-1a"
cidr_block = "10.5.1.0/24"
map_public_ip_on_launch = true
tags = {
Name = "main-1a"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.myvpc.id}"
tags = {
Name = "main-1a"
}
}
resource "aws_route_table" "rtable" {
vpc_id = "${aws_vpc.myvpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
tags = {
Name = "main-1a"
}}

Attaching a routing table, add-

resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.alpha-1a.id
route_table_id = aws_route_table.rtable.id}

3. Create a Private Subnet

resource "aws_subnet" "alpha-1b" {
vpc_id = "${aws_vpc.myvpc.id}"
availability_zone = "ap-south-1b"
cidr_block = "10.5.2.0/24"
tags = {
Name = "main-1b"
}}

4. Launch one EC2 instance running WordPress

For creating a key pair-

resource "tls_private_key" "key_form" {
algorithm = "RSA"
}
resource "aws_key_pair" "task_key" {
key_name = "task_key"
public_key = "${tls_private_key.key_form.public_key_openssh}"
}
output "key-pair" {
value = tls_private_key.key_form.private_key_pem}

For creating security group, which allows port 80 -

resource "aws_security_group" "allow_http_wordpress" {
name = "allow_http_wordpress"
description = "Allow HTTP inbound traffic"
vpc_id = "${aws_vpc.myvpc.id}"
ingress {
description = "Http from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
ingress {
description = "HTTPS"
from_port = 443
to_port = 443
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"]
}
tags = {
Name = "sgroup"
}
}

For launching an instance running WordPress on it-

resource "aws_instance" "wordpress" {
ami = "ami-0979674e4a8c6ea0c"
instance_type = "t2.micro"
key_name = "task_key"
availability_zone = "ap-south-1a"
subnet_id = "${aws_subnet.alpha-1a.id}"
vpc_security_group_ids = [ "${aws_security_group.allow_http_wordpress.id}" ]
tags = {
Name = "Wordpress"
}}

5. Launching an EC2 instance as Bastion Host

resource "aws_security_group" "bostion-sg" {
name = "bostion-sg"
description = "SSH to bostion-host"
vpc_id = "${aws_vpc.myvpc.id}"
ingress {
description = "SSH from VPC"
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"]
}
tags = {
Name = "sgroup"
}}resource "aws_instance" "bostion-host" {
ami = "ami-00b494a3f139ba61f"
instance_type = "t2.micro"
key_name = "task_key"
availability_zone = "ap-south-1a"
subnet_id = "${aws_subnet.alpha-1a.id}"
vpc_security_group_ids = [ "${aws_security_group.bostion-sg.id}" ]
tags = {
Name = "bostion-host"
}}

6. Launch an EC2 instance running MySQL Database

resource "aws_security_group" "mysql-sg" {
name = "for-mysql"
description = "MYSQL-setup"
vpc_id = "${aws_vpc.myvpc.id}"
ingress {
description = "MYSQL from VPC"
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [ "${aws_security_group.bostion-sg.id}" ]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "sgroup"
}}resource "aws_instance" "mysql" {
ami = "ami-76166b19"
instance_type = "t2.micro"
key_name = "task_key"
availability_zone = "ap-south-1b"
subnet_id = "${aws_subnet.alpha-1b.id}"
vpc_security_group_ids = [ "${aws_security_group.mysql-sg.id}" ]
tags = {
Name = "MYSQL"
}}

Now the setup is complete. Save this file with extension .tf

For deploying the infrastructure, use the following commands:

  • terraform init
  • terraform apply

THANKYOU!

--

--