LAUNCHING WEBSERVER USING EFS AND AWS IN TERRAFORM
Amazon Web Services (AWS) is a subsidiary of Amazon that provides on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. In aggregate, these cloud computing web services provide a set of primitive abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers.In aws we have one service known as EFS .Amazon Elastic File System (Amazon EFS) provides a simple, scalable, fully managed elastic NFS file system for use with AWS Cloud services and on-premises resources. It is built to scale on demand to petabytes without disrupting applications, growing and shrinking automatically as you add and remove files, eliminating the need to provision and manage capacity to accommodate growth . We have another service which is S3( amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web) and cloudfront ,(Amazon CloudFront is a content delivery network (CDN) offered by Amazon Web Services. Content delivery networks provide a globally-distributed network of proxy servers which cache content, such as web videos or other bulky media, more locally to consumers, thus improving access speed for downloading the content).Hence in this task we will integrate the s3,ec2,cloudfront,efs service to make a web-server.For this we will just make a web server which has a security group and key made by us in this webserver we attach an EFS and we run a code (taken from github url) and we also have an image (taken from github url) and we will send this image to s3 and then from s3 we will use cloudfront to deploy the image.So we will do this in several steps:
1. Create Security group which allow the port 80.
2. Launch EC2 instance
3. In this Ec2 instance use the existing key or provided key and security group which we have created in step 1.
4. Launch one Volume using the EFS service and attach it in your vpc, then mount that volume into /var/www/html
5. Developer have uploded 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
Some Prerequisites: We must have aws cli downloaded and profile configured , we must have git downloaded and added to path , we must have terraform downloaded and added to path , we must have github url for code and image.
LET`S BEGIN:)
Step 1:) Firstly we have to configure our aws and tells terraform that we are using this provider hence we add a resource named provider .Just like this
Now the main work is creating a key and a security group for our instances , we can create a key using tls_private_key resource , we use module and we create our key just like this:
Now we have to create a security group for this we use aws_security_group resource , here we have to pre-decide what we want to do so we have to add rules according to this so i know that for making web server i need an ssh on port 22,http for port 80 and nfs on port 2049 so according to the given info i will make my ingress rule for the security group but since we know that we need a vpc id for security group hence we will take or import the data from the data sources just like this(here i want subnet ids later):
After this we will just create a security group like this:
Step 2:) and Step 3:) These steps can be done collectively all we need is the same resource block and we will create the instance ,since we know that after creation we have to install some apps/services so i am just doing it in instance resource just like this ,(here i will do a ssh connection via key made in step 1, and after this we will just do a remote execution and install apache webserver , git and enable httpd services so that we can configure it as a web server);
Here we are launching the instance in default vpc so we can provide name also but it is a good practice to provide group ids.
Step 4:) Now we have to create an efs and we have to create mount targets and we have to mount our folder /var/www/html to the aws instance , here i am creating three mount targets and it is a good practice to create mount targets in all three subnets so that we can use same efs in every region , we can also do one thing we can create mount target in same region by providing subnet id of instance but here i create three mount targets . To providing subnet id i already used data sources in step 2.NOTE: DATA EXTRACTED IS IN THE FORM OF SETS SO FOR EXTRACTING WE CONVERT INTO LIST BY tolist AND THAN WE EXTRACT LIST ELEMENT USING element FUNCTION .
Now we mount efs with aws instance using a null resource or we can add this in our alpha resource . Here i comment out second way because by giving too many options sometimes it contradicts , We add fstab entries so that mounting done by us becomes permanent .
Step 5:)HERE we add a s3 bucket (i made this bucket public But you can also add policies), and than we add object to the bucket for this we first clone the github image to our local os and than from local os to s3 bucket (here the path is mine where i want image to be downloaded but you can select your own path) ,after our work we delete the file in local os so that id doesnot becomes permanent.
Step 5:) Now the only work left is setting up the cloudfront which is easy (NOTE I HAVE NOT ADDED ANY PRESEDENCE BEHAVIOUR CACHE BECAUSE FOR ONE OBJECT WE DOES IT WITH DEFAULT ONE)
Here we also added the image to our html code
Step 6:) AT last we run local browser command
four our sake i also take output of public ip
HOW TO APPLY:)
It is simple to apply first you have to make a folder and in this folder you have to make a tf file for doing this run the command notepad terraform.tf name can be anything but the extention must be tf , and then we have to finally paste the code /make the code in this file and after completing if we run the terraform for the first time we need to run a command terraform init this downloads the necessary plug ins in our folder and then we have to run terraform apply this will make our code run and for destroying the code we have to run terraform destroy NOTE:: We can add some option for ex:-auto-approve so that these commands does not needs an approval of yes or no , we can run command terraform validate to check that the code is written in the write syntax or not.
AFTER TERRAFORM apply with auto approval you got this
IT MEANS OUR RESOURCES ARE RUNNING AND HENCE AFTER SOME TIME IT AUTOMATICALLY OPENS THE LOCAL BROWSER AND YOUR WEB PAGE something just like this.
THANKS FOR READING!!!
FULL CODE IS IN THE GITHUB URL MENTIONED BELOW.