AWS ,VPC ,Internet Gateway Subnet, Routing table and launch Wordpress and Mysql using Terraform

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:

  1. Create a VPC
  2. Then we create 2 subnet in our VPC . >> one is public Subnet and other is Private.
  3. Then we create an internet gateway and connect it to only public subnet.
  4. Then we create a routing table for the internet gateway then update and associate it with public subnet.
  5. 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.
  6. 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.

No alt text provided for this image

>>> 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 .

No alt text provided for this image

>>> 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.

No alt text provided for this image
No alt text provided for this image

>>> Now run our code using this terraform apply command.

No alt text provided for this image

>>> Enter yes to approve.

No alt text provided for this image

>>> In one or two minute your vpc will be created.

No alt text provided for this image

>>> now our vpc will be created. You can check on aws web Console.

No alt text provided for this image

>>> 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.

No alt text provided for this image

>>> 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

No alt text provided for this image


>>> Subnet will be created.

No alt text provided for this image
No alt text provided for this image

>>> Unamed subnet is part of default VPC.

>>> Now write code for creating internet gateway.

No alt text provided for this image

>>> Now run

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

>>> Now create Routing Table and apply.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

>>> Now associate routing table with subnet1 to make subnet1 public.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

>>> Create Security Group which allow port 22 for ssh , port 80 for our wordpress site instance and port 3306 for our mysql database instance.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

>>> 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.

No alt text provided for this image

>>> Then copy the selected link and past it into browser and subscribe this AMI.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

>>> Now you can check wordpress instance has public ip and public DNS also.

No alt text provided for this image

>>> And Mysql instance has no public ip and no public DNS.

No alt text provided for this image

>>> Now copy the public DNS name of wordpress instance and paste it on browser.

No alt text provided for this image

>>> 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 .)

No alt text provided for this image
No alt text provided for this image

>>> Now enter submit and you are on wordpress press.

No alt text provided for this image

>>> Now scroll down and click on login to wordpress Dashboard So that you can write or edit your first blog.

No alt text provided for this image

>>> Here if you used the AMI for wordpress which i used then username will always aurora and password which you created.

No alt text provided for this image

>>> Now you can edit , customise or any thing you want to do.

No alt text provided for this image

>>> Some Snapshots of my blog.

No alt text provided for this image
No alt text provided for this image

>>> Now In one click Whole infrastructure will be deleted using terraform destroy -auto-approve

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

>>> Now check instance will be terminated and our vpc , routing table , subnet , internet Gateway every thing will also deleted.

No alt text provided for this image

>>> If you want to Create key pair and save it into your pc using terraform then here is code.

No alt text provided for this image

Thanks for reading my article .I hope it will help you

Bhaskar Prajapati

Associate Dev-Ops Engineer || Terraform || AWS Cloud || Ansible || Docker || Kubernetes || Python

4 年

good job

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了