In this project below tasks have been done in AWS using only terraform.
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
8. Create a Cloudfront using s3 bucket(which contains images) and use the Cloudfront URL to update in code in /var/www/html
9. Generating Bucket Policy to allow only users from Cloudfront URL.
Pre-requisites: Jenkins, Terraform should be installed and aws profile should be configured.
How to use:
The code is very dynamic and i have used jenkins trigger to directly download the repo whenever there is any change.
NOTE: Include main.tf file from my repository and put images in images folder as i have done. But you have to do some Path changes in the code. Refer below instructions.
Step 1: So you have to create a jenkin job just like below
My github URL : https://github.com/Apeksh742/EC2_instance_with_terraform.git
Step 2: Come to the downloaded folder and use commands:
terraform init # it will download all necessary plugins
terraform apply -auto-approve # it will run main.tf and will do all th build up process for you and it takes few minutes to deploy services
Step 3: To get public URL and Cloudfront Domain so that you can use that URL in your web code for access to your objects in s3 bucket.
Two files are separately created one containing public IP and one with Cloudfront Domain.
Once you get Cloudfront URL you can update it in your code and push again to your github repo . Here i have not used automation so that user can use images according to his/her need.
Here is a example of how you can use your Cloudfront URL in your code.
This is it! . Now just hit the public URL from your browser and you can see the image which is loaded using Cloudfront distribution not from your local files.
Final Step: When you are done please use command:
terraform destroy and confirm by typing yes when prompted
So that whole distribution will be destroyed otherwise aws will charge you.
— — — — — — — — — — — -Development Of Code — — — — — — — — — —
This is for Development purpose and for those who have knowledge of terraform and aws they can refer my code
I have tried my code to keep it clean and has written comments to make it more understandable.