How to set up WordPress with a dedicated Database Server along with NAT Gateway on AWS Cloud?

Project Description:-

  1. Write an Infrastructure as code using Terraform, which automatically creates a VPC.
  2. In that VPC we have to create 2 subnets:
  3. public subnet [ Accessible for Public World! ]
  4. private subnet [ Restricted for Public World! ]
  5. Create a public-facing internet gateway for connecting our VPC/Network to the internet world and attach this gateway to our VPC.
  6. Create a routing table for the Internet gateway so that the instance can connect to the outside world, update and associate it with the public subnet.
  7. Create a NAT gateway to connect our VPC/Network to the internet world and attach this gateway to our VPC in the public network
  8. Update the routing table of the private subnet, so that to access the internet it uses the NAT gateway created in the public subnet
  9. Launch an ec2 instance that 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 the instance for further login into it.
  10. Launch an ec2 instance that has MYSQL setup already with a security group allowing port 3306 in a private subnet so that our WordPress VM can connect with the same. Also, attach the key with the same.

What is WordPress?

What is Bastion Host?

aws configure
resource "aws_vpc" "ananya_vpc" {
cidr_block = "192.168.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = true
tags = {
Name = "ananya_vpc"
}
}
resource "aws_subnet" "ananya_public_subnet" {
vpc_id = "${aws_vpc.ananya_vpc.id}"
cidr_block = "192.168.0.0/24"
availability_zone = "ap-south-1a"
map_public_ip_on_launch = "true"
tags = {
Name = "ananya_public_subnet"
}
}



resource "aws_subnet" "ananya_private_subnet" {
vpc_id = "${aws_vpc.ananya_vpc.id}"
cidr_block = "192.168.1.0/24"
availability_zone = "ap-south-1a"
tags = {
Name = "ananya_private_subnet"
}
}
resource "aws_internet_gateway" "ananya_gw" {
vpc_id = "${aws_vpc.ananya_vpc.id}"
tags = {
Name = "ananya_gw"
}
}
resource "aws_route_table" "ananya_rt" {
vpc_id = "${aws_vpc.ananya_vpc.id}"route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.ananya_gw.id}"
}tags = {
Name = "ananya_rt"
}
}resource "aws_route_table_association" "ananya_rta" {
subnet_id = "${aws_subnet.ananya_public_subnet.id}"
route_table_id = "${aws_route_table.ananya_rt.id}"
}
resource "aws_security_group" "ananya_sg" {name        = "ananya_sg"
vpc_id = "${aws_vpc.ananya_vpc.id}"ingress {

description = "allow_http"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0"]}ingress {

description = "allow_ssh"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {

description = "allow_icmp"

from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {

description = "allow_mysql"
from_port = 3306

to_port = 3306
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 = "ananya_sg"
}
}
resource "aws_security_group" "bastion_ssh_only" {
depends_on=[aws_subnet.ananya_public_subnet]
name = "bastion_ssh_only"
description = "It allows bastion ssh inbound traffic"

vpc_id = aws_vpc.ananya_vpc.id
ingress {
description = "allow bastion with ssh only"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "bastion_ssh_only"
}
}
resource "aws_security_group" "ananya_sg_private" {name        = "ananya_sg_private"
vpc_id = "${aws_vpc.ananya_vpc.id}"

ingress {

description = "allow_mysql"
from_port = 3306
to_port = 3306
protocol = "tcp"
security_groups = [aws_security_group.ananya_sg.id]


}
ingress {

description = "allow_icmp"
from_port = -1
to_port = -1

protocol = "icmp"
security_groups = [aws_security_group.ananya_sg.id]

}
egress {

from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}


tags = {
Name = "ananya_sg_private"
}
}
resource "aws_security_group" "bastion_host_sql_only" {
depends_on=[aws_subnet.ananya_public_subnet]
name = "bastion_with_ssh_only"
vpc_id = aws_vpc.ananya_vpc.id
ingress {
description = "bastion host ssh only "
from_port = 22
to_port = 22
protocol = "tcp"
security_groups=[aws_security_group.bastion_ssh_only.id]
}egress {
from_port = 0

to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "bastion_with_ssh_only"
}
}
resource "aws_eip" "ananya-ip" {
vpc = true
public_ipv4_pool = "amazon"
}
output "new_output" {
value= aws_eip.ananya-ip
}
resource "aws_nat_gateway" "ananya_nat_gw" {
depends_on = [aws_eip.ananya-ip]
allocation_id = aws_eip.ananya-ip.id
subnet_id = aws_subnet.ananya_public_subnet.id
tags = {
Name = "ananya_nat_gw"
}
}
resource "aws_route_table" "vp_private_subnet_for_rt" {
depends_on = [aws_nat_gateway.ananya_nat_gw]
vpc_id = aws_vpc.ananya_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_nat_gateway.ananya_nat_gw.id
}
tags = {
Name = "vp_private_subnet_for_rt"
}
}
resource "aws_route_table_association" "as_private_subnet_for_rt_association" {
depends_on = [aws_route_table.as_private_subnet_for_rt]
subnet_id = aws_subnet.ananya_private_subnet.id
route_table_id = aws_route_table.sp_private_subnet_for_rt.id
}.
resource "aws_instance" "wordpress" {

ami = "ami-ff82f990"
instance_type = "t2.micro"
key_name = "ananya_key"
subnet_id = "${aws_subnet.ananya_public_subnet.id}"
security_groups = ["${aws_security_group.ananya_sg.id}"]
associate_public_ip_address = true
availability_zone = "ap-south-1a"


tags = {
Name = "ananya_wordpress"
}
}
resource "aws_instance" "sql" {
ami = "ami-08706cb5f68222d09"
instance_type = "t2.micro"
key_name = "ananya_key"
subnet_id = "${aws_subnet.ananya_private_subnet.id}"
availability_zone = "ap-south-1a"
security_groups = ["${aws_security_group.ananya_sg_private.id}"]
depends_on = [aws_security_group.bastion_host_sql_only,aws_security_group.bastion_ssh_only]

tags = {
Name = "ananya_sql"
}
}
resource "aws_instance" "bastion_host" {
depends_on=[aws_security_group.bastion_ssh_only]
ami = "ami-08706cb5f68222d09"
instance_type = "t2.micro"
key_name = "task4"
subnet_id= aws_subnet.ananya_public_subnet.id
vpc_security_group_ids=[aws_security_group.bastion_ssh_only.id]
tags = {
Name = "bastion_host"
}

}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store