Terraform Aws Wordpress with Private Database
HellO EveryOne ..!!
I am again here with new article on how to setup wordpress in public world and database as private and so that it will be more secure hybrid multi cloud computing task 4 .
This Task is Upgradation of Task 3
Task DescripTion :-
# Performing the following steps:1. Write an Infrastructure as code using terraform, which automatically create a VPC.2. In that VPC we have to create 2 subnets:1. public subnet [ Accessible for Public World! ]2. 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. Create a NAT gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC in the public network6. Update the routing table of the private subnet, so that to access the internet it uses the nat gateway created in the public subnet7. Launch an ec2 instance which has Wordpress setup already having the security group allowing port 80 sothat our client can connect to our wordpress site. Also attach the key to instance for further login into it.8. 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.
Pre-requisites
- A AWS Account .
- A IAM User On aws so that using its credential we can create a profile t login and can use it in future
So here i am showing u how to create a profile , u will get credential once u create a iam user on aws so just give those credential here
After Creating an IAM User with adminstration access then u will get one csv file named as credential.csv in this file u can find your aws iam user username and password known as access key and secret key .
Lets Start with the explanationof the task :-
Provider :- This code will login to aws with the profile we provided using the credential of iam user .
provider “aws” {
region = “ap-south-1”
profile = “Abhi”
}
— — — — — — —
Create Key-Pairs :- This Code will Create Key and will download and save in the folder . sO that we can use it future when launching instance .
resource “tls_private_key” “Abhikey0” {
algorithm = “RSA”
}resource “aws_key_pair” “generated_key” {
key_name = “Abhikey0”
public_key = “${tls_private_keyS.Abhikey0.public_key_openssh}”
depends_on = [
tls_private_key.Abhikey0
]
}resource “local_file” “key-file” {
content = “${tls_private_key.Abhikey0.private_key_pem}”
filename = “Abhikey0.pem”
depends_on = [
tls_private_key.Abhikey0
]
}
— — — — — -
Create VPC :- This Particular Code will Create VPC so that we can use it furthur .
resource “aws_vpc” “abhivpc00” {
cidr_block = “192.168.0.0/16”
instance_tenancy = “default”
enable_dns_hostnames = “true”tags = {
Name = “abhivpc00”
}
}
— — — — — — -
Creating Security Group For Wordpress :-
resource “aws_security_group” “abhisg_wp” {
name = “abhisg_wp”
description = “Allow HTTP inbound traffic”
vpc_id = “${aws_vpc.abhivpc00.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” ]
}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 = “abhisg_wp”
}
}
— — — — — — —
Creating Security Group For Bastion Hosts :-
resource “aws_security_group” “abhisg_bastionhost” {
name = “abhisg_bastionhost”
description = “ssh_bh”
vpc_id = “${aws_vpc.abhivpc00.id}”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”]}tags = {
Name = “abhisg_bastionhost”
}
}
— — — — — — — —
Creating Security Group For MySql :-
resource “aws_security_group” “abhisg_mysql” {
name = “abhisg_mysql”
description = “mysql”
vpc_id = “${aws_vpc.abhivpc00.id}”ingress {
description = “mysql”
from_port = 3306
to_port = 3306
protocol = “tcp”
cidr_blocks = [ “0.0.0.0/0” ]
}ingress {
description = “ssh”
from_port = 22
to_port = 22
protocol = “tcp”
security_groups = [ “${aws_security_group.abhisg_bastionhost.id}” ]
}egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
}tags = {
Name = “abhisg_mysql”
}
}
— — — — — — — —
Create Public Subnet For Wordpress :- This Code will Create a subnet for wordpress and will add internet gateway , edit route table and all .
resource “aws_subnet” “abhisubnet_public” {
vpc_id = “${aws_vpc.abhivpc00.id}”
availability_zone = “ap-south-1a”
cidr_block = “192.168.1.0/24”
map_public_ip_on_launch = truetags = {
Name = “abhisubnet_public”
}
}resource “aws_internet_gateway” “abhi_ig” {
vpc_id = “${aws_vpc.abhivpc00.id}”tags = {
Name = “abhi_ig”
}
}resource “aws_route_table” “abhi_route” {
vpc_id = “${aws_vpc.abhivpc00.id}”
route {
cidr_block = “0.0.0.0/0”
gateway_id = “${aws_internet_gateway.abhi_ig.id}”
}tags = {
Name = “abhi_route”
}
}resource “aws_route_table_association” “abhinata” {
subnet_id = aws_subnet.abhisubnet_public.id
route_table_id = aws_route_table.abhi_route.id
}
— — — — — — — -
Creating Private Subnet For Mysql Instance :-
resource “aws_subnet” “abhisubnet_private” {
vpc_id = “${aws_vpc.abhivpc00.id}”
availability_zone = “ap-south-1b”
cidr_block = “192.168.2.0/24”tags = {
Name = “abhisubnet_private”
}
}
— — — — — -
Adding Elastic IP , Creating Nat Gateway and editing route table and associate it .
resource “aws_eip” “elastic_ip” {vpc = true}resource “aws_nat_gateway” “abhi_natgateway” {allocation_id = “${aws_eip.elastic_ip.id}”subnet_id = “${aws_subnet.abhisubnet_public.id}”depends_on = [ “aws_nat_gateway.abhi_natgateway” ]}resource “aws_route_table” “abhi_natgateway_route” {
vpc_id = “${aws_vpc.abhivpc00.id}”
route {
cidr_block = “0.0.0.0/0”
gateway_id = “${aws_nat_gateway.abhi_natgateway.id}”
}tags = {
Name = “abhi_natgateway_route”
}
}resource “aws_route_table_association” “abhnatb” {
subnet_id = aws_subnet.abhisubnet_private.id
route_table_id = aws_route_table.abhi_natgateway_route.id
}
Finally All 3 Launching Instance’s For Completing Our Task .
resource “aws_instance” “AbhiWp_Os” {
ami = “ami-7e257211”
instance_type = “t2.micro”
key_name = aws_key_pair.generated_key.key_name
subnet_id = “${aws_subnet.abhisubnet_public.id}”
vpc_security_group_ids = [ “${aws_security_group.abhisg_wp.id}” ]tags = {
Name = “AbhiWp_Os”
}
}resource “aws_instance” “Abhi_BaTionHost” {
ami = “ami-0ebc1ac48dfd14136”
instance_type = “t2.micro”
key_name = aws_key_pair.generated_key.key_name
subnet_id = “${aws_subnet.abhisubnet_public.id}”
vpc_security_group_ids = [“${aws_security_group.abhisg_bastionhost.id}” ]tags = {
Name = “Abhi_BastionHost”
}
}resource “aws_instance” “Abhi_MySql” {
ami = “ami-0b5bff6d9495eff69”
instance_type = “t2.micro”
key_name = aws_key_pair.generated_key.key_name
subnet_id = “${aws_subnet.abhisubnet_private.id}”
vpc_security_group_ids = [ “${aws_security_group.abhisg_mysql.id}” ]tags = {
Name = “Abhi_MySql”
}
}
Now we will Run Terrform all commands to see output
terraform init# It will install plugin rewuired for our code
terraform validate# it will validate our code is their any or not if not then we can go ahead
terraform apply --auto-approve
VPC :-
SubneT :-
RouTe Table :-
Internet Gateway :-
Nat Gateway :-
All 3 Instances Are Also Created now we can view our website and can also go inside bastion hosts instance to change anything in mysql os for managing database .
Writing FirsT Article :-
Now client can use this ip to get our article .
terraform destroy --auto-approve# This will destroy our entire environment that we created !!
That’s All Our Task is Done
And we can also do some more things in this task like i have created for this code in this way that our mysql database is on private world meas inside this instance no one from public world can go inside and so for this i have created one bastion host instance using this only we can go inside the mysql os and can modify it and wordpress is running in public world so that anyone from anywhere can access our website .
All The Code and Files i am uploading on GitHub
Thank yOu Vimal Daga Sir For Giving Us RighT KnowleDge and Teaching THis Tools and Technologies sO That ToDay I Can CreaTe Good Projects like This By My Own .
THank yOu EveryOne For Reading .!!
Connect with me On Linkedin For FurThur Queries Or Suggestion’s if u Have Any .!!
THank yOu EveryOne For Reading ..!!