AWS EC2 Instance, EBS, Key-pair, SG, S3, Cloud Front, Snapshot creation using One single CMD !!

AWS EC2 Instance, EBS, Key-pair, SG, S3, Cloud Front, Snapshot creation using One single CMD !!

In this article you gonna come to know how we can create AWS EC2 Instance, EBS, Key-pair, Security-Group, S3, Cloud Front, Snapshot just by one single CMD using Terraform.

What is Cloud Computing Automation???

In simple words whatever we create on any cloud we can create that whole infrastructure using Terraform. But the biggest advantage of Terraform is that we only have to create the Terraform code once and in Next time only one Single CMD and Terraform will create that complete Infrastructure for us. Also the best way to create one infrastructure is to keep an documentation of that cause in future we might need to track something. Now as we create One Terraform code so it works as a documentation and we need to worry of loosing anything or any step we did previously.

So, At first let's see the problem statement :

This is a task given by World Record Holder Mr. Vimal Daga Sir under Hybrid Multi Cloud Program.

Task 1 : Have to create/launch Application using Terraform

1. Create the key and security group which allow the port 80.

2. Launch EC2 instance.

3. In this Ec2 instance use the key and security group which we have created in step 1.

4. Launch one Volume (EBS) and mount that volume into /var/www/html

5. Developer have uploaded the code into GitHub repo also the repo has some images.

6. Copy the GitHub repo code into /var/www/html

7. Create S3 bucket, and copy/deploy the images from GitHub repo into the s3 bucket and change the permission to public readable.

8 Create a CloudFront using s3 bucket(which contains images) and use the CloudFront URL to update in code in /var/www/html

Optional

1) Those who are familiar with Jenkins or are in DevOps AL have to integrate Jenkins in this task wherever you feel can be integrated

2) create snapshot of ebs

Above task should be done using terraform

Let's straight dig into the Terraform code and try to understand each bit of the code :

I am proceeding in a way like at first I will pick one step from the problem statement next I will show the code and next I will tell what that code is doing and How. Also in each discussion I provided the official documentation links.

Step 1 : Create the key and security group which allow the port 80.

No alt text provided for this image

Creating RSA Key :

  • Here I created one tls private key which in next step we gonna push to AWS. I used one algorithm called RSA as AWS support that. If you wanna know more about this you can go to Official Terraform Documentation on Creating tls private key. Also you can see the official AWS documentation on TLS on Amazon Linux.

Creating AWS key pair :

Creating Security Group :

  • Here this “depends on” I used because I want Terraform to create Security Group after It create this AWS key-pair. Cause sometimes we have to tell Terraform how we want to make the pipeline. In this security group code the two keyword “ingress” and “egress” means which kind of network inbound and outbound we want to provide to our Instance. Here in ingress I allowed two ports 22 and 80 cause using 22 port I will connect my Instance using SSH to pass commands inside the Instance. And as I am configuring one webserver so I expose the 80 port so that my webserver can take requests. In egress we allow all ports cause we need to install many software in our Instance and for that we need to download them from internet. For documents you can see this AWS security group by Terraform.

Step 2 : Launch EC2 instance. In this Ec2 instance use the key and security group which we have created in step 1.

No alt text provided for this image

Launching EC2 instance :

  • Here I lunches one ec2 instance by providing required information like AMI id, instance_type, key_name, security_groups etc. In key_name I provided the name of my key that I create in previous code. In security_groups I passed the name of my security_group that I create in previous code. Official AWS resource Terraform documentation.

Installing Required Programs :

  • Next I do SSH login inside my container using my created private key and install required software for running webserver. At the same time after installation I started the webserver. Also I enabled the webserver means if due to any reason server stop It will automatically start the server. SSH login Terraform Documentation. Remote Executor Terraform Documentation.

Step 3 : Launch one Volume (EBS) and attach that to the EC2 instance.

Creating EBS Volume :

  • In AWS you can only attach a volume to an Instance if both are on same Availability Zone. That's why after launching the EC2 instance I pass that's Availability Zone to the EBS creation code. I selected the size to 1GB. EBS volume creation Terraform Documentation.

Attaching the Volume :

  • Next I attach the volume to my instance. Here I give my volume id from my created volume variable and instance id get from the instance creation variable. This Force Detach means when I will destroy my environment using Terraform it gonna at first forcefully detach the volume cause otherwise Terraform won't be able to destroy the volume. EBS Volume Attach Terraform Documentation.

Step 4 : Mount that volume into /var/www/html and Copy the GitHub repo code into /var/www/html.

Remotely running few commands :

  • Next again I do remote SSH login inside the instance and at first I format the attached EBS volume and next I mount that volume to my webserver. Httpd webserver stores it's html files inside one folder called “/var/www/html” so to make the data of that folder persistent I mount the volume to that location and I clone my html codes from GitHub to this folder. Null Resources Terraform Documentation.

Step 5 : Create S3 bucket, and copy/deploy the images in s3 bucket and change the permission to public readable.

Creating S3 bucket and adding one Image in It :

Step 6 : Creating Cloud Front Distribution.

Configuring CDN :

  • Here I only provide three information. 1st locals means I only created one local variable to use it on many positions. I only created one variable which contains the id of my S3. 2nd In origin name I provide the domain name of my S3 bucket so that I can connect my Cloud Front with my S3 bucket. 3rd in origin id I provide my local variable which contains the S3 bucket id. AWS CDN Terraform Documentation.
  • You might be thinking what about other options. Yeah we can customize lots of options on CDN, but these setting are by default and I copied this code from Terraform website documentation.

Step 7 : Adding Cloud Front URL to my html code :

Html Code Changing using SSH :

  • Here I changed my html code which my git cloned from the GitHub. I mainly do SSH login and add one line to the html code. Here this line contains my CDN url of my S3 file. Now each time I create new Infrastructure CDN url gonna change that's why I used the variable here.

Step 8 : Creating Snapshot :

  • Here after I changed the code I created one snapshot of my attached 1GB volume. I created this so that in future I can get my previous codes. Also It's not only always about code. Sometimes there might be some important files I added in my EBS, so it's one kind of backup. Snapshot Terraform Documentation.

Step 9 (Last Step) : Opening Local Browser :

  • Finally this is a local provisioning. Here I opened my local machine browser and also I directly jump into the IP address of my instance and opened the html page. Local Executor Terraform Documentation.

Cheers…. Finally it's done…..

So now here is the total final code which you can run easily. Now, If you want to see what happens when we run the code or how to run the code for that I have made one video. You can check this video on the below mentioned LinkedIn Link.

Still thinking what is that single command which will create all this infrastructure in AWS. Here is the command.

terraform apply -auto-approve

Here is the Final code :

Last but not least Thank you so much Vimal Daga Sir. Alright everyone so much thanks for reading, I hope you learn something great from it. It would be great help if you comment and tell me what are your views about this task.

Github URL: https://github.com/uditagarwal1305/HCtask1.git


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

Udit Agarwal的更多文章

社区洞察

其他会员也浏览了