Hosting a Static website on aws Using Terraform
What is Terraform?
Terraform is an open source “Infrastructure as Code” tool, created by HashiCorp.A declarative coding tool, Terraform enables developers to use a high-level configuration language called HCL (HashiCorp Configuration Language) to describe the desired “end-state” cloud or on-premises infrastructure for running an application. It then generates a plan for reaching that end-state and executes the plan to provision the infrastructure.
Steps to create the Infrastructure
- Create the key and security group which allow the port 80.
- Launch EC2 instance.
- In this Ec2 instance use the key and security group which we have created in step 1.
- Launch one Volume (EBS) and mount that volume into /var/www/html
- Developer have to uploded the code into github repo also the repo has some images.
- Copy the github repo code into /var/www/html
- Create S3 bucket, and copy/deploy the images from github repo into the s3 bucket and change the permission to public readable.
- Create a Cloudfront using s3 bucket(which contains images) and use the Cloudfront URL to update in code in /var/www/html
First we give terraform the provider details.
After that we create the EC2 key-pair
Creating the security groups that allow port 80 and port 22.
In step 2 we are going to launch the ec2 instance with the help of key and the security groups that we have generated.
Here we are creating ec2 instance and and also pre-installing the httpd ,php and git.
here we are going to Launch one Volume (EBS) and attaching that volume to the instance that we have created earlier
After attaching the volume we have to format it and mount it to /var/www/html and also cloning the github repo that has code of our webpages to /var/www/html/ so that we can host our website.
In this step we are going to create S3 bucket , and we store images of our website here.Because S3 is an object storage service that offers industry-leading scalability, data availability, security, and performance
we dont give the direct link of S3 bucket to the our webpage code. We create a CDN service using cloudfront ,so that we can deliver our website fastly and reduce the latency.
Creating a cloudfront
Now we have created the whole infrastructure using terraform.
And run the following commands.
- terraform init
- terraform apply
Now you can see the website
THANK YOU !!