Brief About our task.

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 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 Required Tools.

1. AWS command line interface

2.Terraform

First SETUP YOUR PROFILE of AWS using Terminal

  • Firstly, you would need to create an IAM user (ankit in my case). Note down the Access Key ID and Secret Access Key (you will need it to configure the profile on was cli).
  • Create a key pair and a security group that allows inbound SSH and HTTP.
  • Download the AWS cli and Terraform onto your system.

Use aws configure --profile ankit (profilename) and paste your access keys here.

No alt text provided for this image

Now create a folder using name anywhere inside that folder create one more folder for separate the projesct.(In my case i am creating a folder on Desktop (Folder name terraform) inside the folder i created one more folder (Folder name task). Inside task folder create a file using .tf extension in my case i created aws.tf file. Because whole code must be in .tf file .(Use commands according to your Operating system in My case i have MacOS).

No alt text provided for this imageNow start writing code.
open the aws.tf file in notepad(any editor) in my case i am using apple TextEditor.
Step 1. Give service provider name and create key pair and store locally in the system for login through SSH.
No alt text provided for this image

Step 2. We will create Security group for access through http role for login from SSH for ping and inbound rule and lastly a tag name.

No alt text provided for this image

Step 3. Creating an instance then connect through SSH after that install http, php git because our website in php and we r using apache web server the we restart our server and then enable permanent.

No alt text provided for this image

Step 4. Create a ebs volume attach it from instance connect from instance through SSH form format the volume then mount to /var/www/html folder (because by default apache access webpages through this folder) then clean our webpage from GitHub (because we created our webpage in GitHub repo).

No alt text provided for this image

Step 5. Create S3 bucket and upload our image to S3 bucket from GitHub repo where we create a new repo and upload image on that repo and save that image locally. 

No alt text provided for this image

Step 6. Now we create cloudfront image police public the connect through SSH and upload from local system to our webpage(i created webpage called ankit.html).

No alt text provided for this image

For getting output of your IpAddress.

No alt text provided for this image

Now the file the go back to your terminal and before apply the code run a commandterraform init this command will download all your plugins to connect from aws cloud.

If run terraform apply command before downloading plugins then you get this error. So Download plugins first using terraform init command.

No alt text provided for this image

Plugins takes some time to download depends upon your internet speed.

No alt text provided for this image

Now plugins successfully downloaded now run terraform apply command to run your code. Enter yes on the place of Enter a value to auto approve.

No alt text provided for this image

Some output of process. Filnal creation will take some time. (4 to 5 min).

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

Finally our instance will we created and we get out Ip in output.

No alt text provided for this image

Now we search our ip/ankit.html(13.235.247.252/ankit.html) in any browser and can access our webpage.

No alt text provided for this imageNow if you want to destroy the whole infrastructure in just a command run this command terraform destroy and enter yes when it ask to approve the destruction.
No alt text provided for this imageAfter some time finally our whole infrastructure is destroyed in just a command (The power of Automation). 
No alt text provided for this image

And again if you run terraform apply again your whole infrastructure in created. We just need to write this code once.