Web Development on Aws using Terraform
Hritick Goyal
Data Engineer @ Jio | Immediate Joiner l Serving Notice Period | Microsoft azure data engineer associate DP203
In this article we will see how to do web development on aws .For all configurations we will use terraform instead of doing manually.
Steps for writing terraform code :
1. First of all login to aws account with user credentials.
2. Create one Key pair in aws.
3. Create one security group in aws that allow all traffic .
4. Launch one ec2 instance that uses above created key pair and security group.
5. Create one ebs volume and attach that with ec2 instance.
6. Install all necessary softwares inside ec2 instance required for web development
7. Download the code from github into /var/www/html folder and mount it with ebs volume.
8. Create one s3 bucket and put a object inside that bucket.
9. After that create cloudfront of that s3 bucket and put the ip of cloudfront inside file present in /var/www/html.
10.Create one snapshot of ebs volume .
So we will follow all above steps to achieve our task.
Step 1 -> For login into aws account instead of giving our access key and secret key we can create one profile and used that profile to login.
Now terraform there has various providers for various platform.For aws it has provider called as aws.
here we have given region name ,profile containing credentials .
Step 2 -> After successful login into aws account we need to create one key pair .Terraform provide one resource name tls_private key it will create private key .After that we can create public key using resource called aws_key_pair.
In this we have created one dependency because if private key is not created then there is no means of public key .After created aws key pair we can save it in our system by using local_file resource provided by terraform.
Step 3 -> For creating security group in aws terraform provide resource named as aws_security_group.
here ingress is for incoming traffic while outgress for outgoing traffic.We have allowed only 80 and 22 port while all ports in outgress.
Step 4 & 6 -> For launching ec2 instance we need ami id and instance type.
Here we have used connection and remote_exec provisioner to connect to ec2 using saved private key and download all necessary software .
Step 5 & 7 -> We can create ebs volume by using aws_ebs_volume provided by terraform.
Step 6 -> Now we attach our ebs volume to ec2
here we have used force_detach = true because if we destroyed ebs using terraform we cant do till we have not declare it true.
here we have used null resource because provisioner always be a part of some resource and we do not want to create any resource so we have used null resource . We have mount our ebs to folder and then used git clone.
Step 8 -> Terraform provide aws_s3_bucket for creating bucket.
we have created s3 bucket with acl of public read which means anyone can access this bucket.Now we need to put object inside the s3 bucket.
here also we have given acl equals to public read due to same reason.
Step 9 -> For creating cloud front we have used aws_cloud_distribution resource.
here we have given name of countries which can see this s3 object using cloudfront ip in whitelist .And to see ip of cloudfront we have print it in the console using output resource.
Step 10 -> Terraform provide resource for snapshot also.
After all this we have successfully write code for web development in aws by terraform .
Now we need to run our code but before it we need to install all plugins required by terraform by using terraform init command.After that run terraform apply command.
We have successfully created our web development .Now just need to put this cloudfront ip inside file .
Now we can see our output with the public ip of ec2 instance.
After all this we can delete all the resources of Aws by one click
we have successfully deleted all resources from aws.
Hope this article will be useful in getting some knowldege.
Thanks
Hritick Goyal