LAUNCHING A WEBSERVER ON AWS(EC2,S3,CloudFront) USING TERRAFORM AND GITHUB. Also mounting the AWS EFS to an instance using terraform.

LAUNCHING A WEBSERVER ON AWS(EC2,S3,CloudFront) USING TERRAFORM AND GITHUB. Also mounting the AWS EFS to an instance using terraform.

No alt text provided for this image

Terraform is an open-source infrastructure as code software tool created by HashiCorp. It enables users to define and provision a datacenter infrastructure using a high-level configuration language known as Hashicorp Configuration Language, or optionally JSON. In this blog I have used HCL (Hashicorp Configuration Language).

Main point of using terraform are-

> It is the time of automation and nobody wants to do things manually which is inefficient.

>You can easily deploy and share your product in the form of code which is the need of today's world. This is called Infrastructure as Code(IaC).

Lets get started

PART 1 - Using AWS EBS

DESCRIPTION -

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

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. The developer has uploaded the code into GitHub repo and other repo with some images.

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

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

PROCEDURE -

CONFIGURE TERRAFORM AND AWS -

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

Created a key for our ec2 instance using putty gen and uploaded the public key to aws.

No alt text provided for this image

Created a security group for our ec2 instance and allowed ingress traffic from port 22(for ssh) and 80(for httpd) and allowed egress traffic to anywhere.

No alt text provided for this image

Launched an ec2 instance and attached the above security group and key.

No alt text provided for this image

Created an ebs volume and attached it to our instance. Also allowed forced detach so that we don't need to unmount it when we destroy our environment.

No alt text provided for this image

Now in order to use our ebs volume as webserver data we need to mount it to /var/www/html and download the git repository. Moreover we installed httpd, git and php.

No alt text provided for this image

Now we created a s3 bucket and uploaded a image(object) to the bucket. In this we also mentioned the content type of the object to update it's metadata and we can easily access it using url.

No alt text provided for this image

To link our s3 bucket to cloudfront and to restrict access to our s3 object we created an object access identity (oai).

No alt text provided for this image

Created a Cloudfront distribution for s3 bucket and mentioned the oai created above.

No alt text provided for this image

Created IAM policy and updated bucket policy of our s3 bucket.

No alt text provided for this image

Finally uploaded the cloudfront link to our code and using provisioner locally launched our website.

No alt text provided for this image

Finally our site is working !!!

No alt text provided for this image


PART 2 - Mount AWS EFS to an instance.

In the above code we will only change the portion of the code where we created ebs and attached it to our instance and mounted it permanently using remote execution.

Create an aws efs and create efs mount target so that it is launched in the subnet of our instance and attach our security group.

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

Using webui we can see our efs is created in the specified subnet with our security group.

No alt text provided for this image

Now we mount the efs permanently to our instance using remote execution. In order to do so we inserted an entry in fstab and then using dns name of our efs mounted it to folder of httpd.

No alt text provided for this image

Rest of the code is same as mentioned above and both the codes are uploaded on github.

Use this link to see my code and feel free to make contributions

Some basic terraform commands-

> terraform init

>terraform validate

>terraform apply

>terraform destroy

For understanding these > terraform --help

Also go through this documentation

I hope this is going to help you !!!


To view or add a comment, sign in

More articles by Rohan Chaudhary

  • AMAZON EKS

    Objectives- Create a kubernetes cluster using amazon eks and on that cluster launched a webserver using ELB,EBS. Deploy…

Explore content categories