AWS ,VPC ,Internet Gateway Subnet, Routing table and launch Wordpress and Mysql using Terraform
Things we do here:-
We will do here every thing here using terraform automation tool:
- Create a VPC
- Then we create 2 subnet in our VPC . >> one is public Subnet and other is Private.
- Then we create an internet gateway and connect it to only public subnet.
- Then we create a routing table for the internet gateway then update and associate it with public subnet.
- Then we create a security group which allow port 22 for ssh , port 80 for our wordpress site instance and port 3306 for our mysql database instance. And allow egress port 0 so that instance can also connect to internet.
- And finally we launch a instance using wordpress AMI in public subnet and a instance for mysql instance for database in private subnet.
NOTE :-
>>> Create an account on AWS
>>> Create a IAM user with Admin Access power and download the .csv file .
>>> Install AWS CLI on your base os (In my case i am using MacOS). Install AWS CLI using this link >>> https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-windows.html
>>> Install terraform in your base OS. Install terraform using this link >>> https://learn.hashicorp.com/terraform/getting-started/install.html
>>> Now configure aws then enter access key , Secret key and region. (On the place of ankit write your IAM user profile name)
>>> Here i am expecting you already created a key pair.
>>> Now start creating terraform code.
First create a folder And Create a vpc.tf file (You can write any name but extension must be .tf )
>>> Now create VPC . Here give provider name region and create our profile .
>>> Before applying first download the plugins. by using terraform init command. This will take some time depends upon your internet speed.
NOTE:- Every command you have to run inside that folder where your .tf file is present. In my case my .tf file is inside vpc folder.
>>> Now run our code using this terraform apply command.
>>> Enter yes to approve.
>>> In one or two minute your vpc will be created.
>>> now our vpc will be created. You can check on aws web Console.
>>> Now in the same vpc.tf file add code to create public and private Subnet. In public subnet we have given public ip for ssh so that we can go inside instance and make the changes and in private subnet there is no public ip (because our database is in private subnet )so that no one can login to our database instance and access our data.
>>> Now run you can use this command terraform apply -auto-approve (This command will not ask you to enter yes to approve it will automatically approve ). From now i will use terraform apply -auto-approve
>>> Subnet will be created.
>>> Unamed subnet is part of default VPC.
>>> Now write code for creating internet gateway.
>>> Now run
>>> Now create Routing Table and apply.
>>> Now associate routing table with subnet1 to make subnet1 public.
>>> Create Security Group which allow port 22 for ssh , port 80 for our wordpress site instance and port 3306 for our mysql database instance.
>>> Now Finally create an instance for wordpress which is in public subnet and an instance for mysql which is in private subnet.
>>> You can chose any AMI and install wordpress but the AMI i chossen is pre created wordpress and Mysql as well. For practice you can use this AMI ID .
NOTE : If you use this AMI ami-7e257211 for creating wordpress instance. You may get this error. When you apply terraform code.
>>> Then copy the selected link and past it into browser and subscribe this AMI.
>>> Now you can check wordpress instance has public ip and public DNS also.
>>> And Mysql instance has no public ip and no public DNS.
>>> Now copy the public DNS name of wordpress instance and paste it on browser.
>>> You will be landed into a page. Here you have to enter the instance id of wordpress instance then create a wordpress password. (This step is only if you use the AMI which i used .)
>>> Now enter submit and you are on wordpress press.
>>> Now scroll down and click on login to wordpress Dashboard So that you can write or edit your first blog.
>>> Here if you used the AMI for wordpress which i used then username will always aurora and password which you created.
>>> Now you can edit , customise or any thing you want to do.
>>> Some Snapshots of my blog.
>>> Now In one click Whole infrastructure will be deleted using terraform destroy -auto-approve
>>> Now check instance will be terminated and our vpc , routing table , subnet , internet Gateway every thing will also deleted.
>>> If you want to Create key pair and save it into your pc using terraform then here is code.
Associate Dev-Ops Engineer || Terraform || AWS Cloud || Ansible || Docker || Kubernetes || Python
4 年good job