Deployment of webpage on AWS using HCL - Terraform
Saranya Chattopadhyay
Full Stack Developer - DevSecOps @IBM ? DevOps Practitioner ? Ex Intern @CommVault, HighRadius ? 2x GCP, 1x Microsoft, 1x RedHat Certified Engineer
Hey guys!! Currently under a training on Hybrid Multi Cloud, my first basic project on the same has been successfully deployed; and this time obviously not on our local system, but on AWS! And believe me guys, working and researching on this was damn interesting. Let me not waste more of your time and jump directly onto the project.
AWS or Amazon Web Services is a subsidiary of Amazon that provides cloud computing platform where we can launch a new OS without wasting even 1 byte of RAM or utilising any CPU from our system. AWS has an amazing user-friendly WebUI to work on. But we also have a possibility of deploying our work on AWS without visiting its GUI even once, if our AWS account is fully configured. This is nothing but Infrastructure As A Code (IAAS), which is achieved using Terraform. Terraform is a IAAS software maintained by the company HashiCorp, and the coding language used in Terraform is HCL (HashiCorp Configuration Language).
So now coming to the problem statement that I tried to achieve, along with the solution performed by me:-
STEP-1 : To create a key-pair and security group which allows port 80 (HTTP)
Code for key-pair generation.
We find that a new key has been generated with name rg-env-key.
Code for creating security group.
Desired security group with name httpfirewall has been created.
STEP-2 : To launch an EC2 instance using the created key and security group. Here I have made such provision that the public IP of the launched instance will be showed as output. This instance should also have all the required programs installed as well as enabled required for the deployment of a webpage and working of git command.
On the AWS console, we find that our instance has been launched successfully with name terraformos2 (that is running). I have highlighted the instance IP in this screenshot for reference.
STEP-3 : To launch an EBS volume and mount the volume into /var/www/html of the instance. Since we need to deploy a webpage, I have made a basic html code and committed it into a github repository (link given in post) along with a random image. In this same step, I have cloned the github repo and copied its contents into /var/www/html folder.
The launched EBS volume with name newvol and size 1 GiB can be found on the AWS console.
STEP-4 : To create an s3 bucket and copy the image uploaded in the github repo into it. For public access, the s3 bucket must be public-readable.
The s3 bucket as viewed on the AWS console is -
STEP-5 : To create a CloudFront using the s3 bucket created and use the CloudFront URL to update in the html code so that the image can be viewed in our webpage.
From the AWS console, I can confirm that my CloudFront has been created. The domain name prefixed with https:// and suffixed with the image name acts as the CloudFront URL.
The infrastructure for the project has been set up. Now comes its deployment. For this, we start with configuring our IAM account of AWS using CLI. Command for the same is aws configure. here we need to provide the credentials that are given in a CSV file while creating the IAM user.
Next, we move to the folder where the terraform code is placed with extension .tf and run a command terraform init. This command initialises terraform in the folder and installs all the plugins that are required for the terraform code to run.
Once terraform is initialised, our next step is to see the validation of our code. This is done using terraform validate command. If there are any errors in the code, running this command shows them and we can debug our code accordingly.
Code validated? Yayy! We are good to go!! The final command to be run to form the infrastructure is terraform apply -auto-approve. On running this command, we can see the infrastructure setup.
Resources being created
Programs being installed.
And finally apply is complete with the public IP as output. We can see that the IP shown here and the IP that I had highlighted earlier are same.
The final webpage that has been deployed is :-
Since many of the resources under AWS fall in the free tier category only upto a certain limit and Amazon starts charging us for using those resources beyond the free tier limit, we need to terminate all the resources launched once our task is fulfilled. This is done using terraform destroy -auto-approve command.
That was all about the project implemented.
Software Engineer 1 at Dell Technologies || Google Certified Associate Cloud Engineer || Redhat certified Engineer || EX-180 Certified || ARTH-2020 Learner @LW
4 年damn cool!!! keep it up
Software Engineer, Capgemini India ???? Guidewire Certified Associate Developer ?
4 年Great work.Congrats.