END to END Automation of Website hosting in AWS using Terraform.

Saumik Satapathy
3 min readJun 13, 2020
  1. First, we create a separate folder for storing our code and necessary files.

$ mkdir terraform && cd terraform

2. Clone the Github repository where the terraform code resides.

$ git clone https://github.com/saumik8763/automation-in-terraform.git

3. go inside the automation-in-terraform directory.

$ cd automation-in-terraform

4. Initialize the terraform for getting the provider plugins.

$ terraform init

5. Create security key-pairs(public and private).

$ ssh-keygen -f terraform

6. Change the ‘mykey12345' to ‘terraform’ in every occurrence of ec2.tf file.

$ sed -ir ‘s/mykey12345/terraform/g’ ec2.tf

7. Create the Security group for allowing SSH(22) and HTTP(80).

Security group code

8. Launching the EC2 Instance in Default VPC, Security Group and Keypair created earlier.

9. Install the ‘Git’, ‘php’ and ‘http’ in the instance after launching.

10. Launch an EBS Volume and attach that in the running instance.

11. Mount the new volume inside “/var/www/html” directory.

Mount the volume inside “/var/www/html” directory.

12. Create an S3 bucket to upload the image. Upload the image to S3.

Creating and uploading an image into S3.

13. Change the file permission inside bucket to publically readable.

S3 Bucket policy

14. Create a CloudFront Distribution For Fast Content Delivery.

CloudFront Content

15. Update the index.html file inside the EC2 to use CloudFront URL for serving Static Content.

16. Check the terraform code if any error present.

$ terraform validate

17. Run terraform plan and then terraform apply.

$ terraform plan

$ terraform apply -auto-approve

18. Once terraform run successfully copy the Public IP and go-to browser. Paste the public IP and hit enter.

19. Congratulations !!! You have successfully created a web page with Edge location enabled for better performance without going to the web console.

20. Github URL: https://github.com/saumik8763/automation-in-terraform

https://youtu.be/U8OK7Vj09eg

--

--

Saumik Satapathy

A passionate software Engineer with good hands on experience in the field of DevOps/SRE. Love to share knowledge and intersted to learn from others.