Hosting Webpage on AWS EC2 using Terraform

Hello, readers. Today we will be looking into the concept of hosting a static webpage by making use of EC2 instance, with the help of Terraform.

Here is how the process flow will be for the objective:

  • Launching a VPC
  • Create EC2 instance in this VPC
  • Launch Webserver using apache 
  • Create an index.html file having content about your skills. 
  • Start the webserver on port 80
  • Create the snapshot of volume attached with the EC2 instance. 
  • Destroy all the resources created in this task. 

Step 1: Launching VPC

There are two methods to go through, either manually creating the required VPC, or using terraform code to create one

Manual Method:

In this method, you just need to give a name for the VPC and define the value for the IPv4 CIDR block. The VPC automatically gets created in your default region.

Code Method:

provider "aws" {
          region = "ap-south-1"
          profile = "default"
}


//creating the vpc
resource "aws_vpc" "main" {
  cidr_block = "192.168.0.0/16"
  instance_tenancy = "default"
  tags = {
Name = "task3-vpc"
}
}

In this method, you can also see we define Name for the VPC, as well as region and CIDR block.


Step 2: Creating EC2 instance in the defined VPC

Now, we will make use AWS platform for creating the required instance, the steps are displayed via representation images below:







At the final step, when you click for launch, you get prompted up to create or use key pairs. Go for creating a new key-pair, and then downloading it. After that, proceed to launch the instance.




Step 3: Launch Webserver using apache

We launch the webserver using apache, by making ssh connection via the key pair that was formed during the launch of the EC2 instance, as well as the installation of Apache with the help of implicit code in Terraform itself.

resource "aws_instance" "webos1" {
  ami           = "ami-011c99152163a87ae"
  instance_type = "t2.micro"
  security_groups = ["webport-allow"]
  key_name = "my-keypair-new"

  tags = {
    Name = "Web Server by TF"
  }


connection {
    type     = "ssh"
    user     = "ec2-user"
    private_key = file("C:/Users/hp/Downloads/my-keypair-new.pem")
    host     = aws_instance.webos1.public_ip
  }

provisioner "remote-exec" {
    inline = [
  "sudo yum install httpd -y",
  "sudo yum install php -y",
  "sudo systemctl start httpd",
  "sudo systemctl start httpd"  
    ]
  }

}

It is important to give the file path correctly for the key pair file.


Step 4: Create an index.html file having content about your skills. 

For this step, we want to host or static webpage on the ec2 instance. Thus, we can create the .php file on GitHub and include it into our terraform code for ease of action.

Initially make a Github repo, which will contain your index.php file. Put your skills in the PHP code and commit the changes accordingly.

Now save the link for the repo for further use.

Create a Null Resource, which will consist of your webpage, which will be required for establishing the connection via SSH for hosting the page.

resource "null_resource" "nullremote3" {
connection {
    type     = "ssh"
    user     = "ec2-user"
    private_key = file("C:/Users/hp/Downloads/my-keypair-new.pem")
    host     = aws_instance.webos1.public_ip
  }

provisioner "remote-exec" {
    inline = [
  "sudo yum install git -y",
  "sudo git clone https://github.com/tithichoudhary/gitphptest.git   /var/www/html/web"
    ]
  }
}


Step 5: Create the snapshot of the volume attached with the EC2 instance. 

Here, we attach an EBS volume to the instance that we are working with,

resource "aws_ebs_volume" "example" {
  availability_zone = aws_instance.webos1.availability_zone
  size              = 1

  tags = {
    Name = "WEB SERVER HD"
  }
}

resource "aws_volume_attachment" "ebs_att" {
  device_name = "/dev/sdc"
  volume_id   = aws_ebs_volume.example.id
  instance_id = aws_instance.webos1.id
}

We also add one further step of integration and automation. We include the link to our index.php page when the complete terraform execution has taken place, and the final result comes out to be our webpage, that contains our skills.

resource "null_resource" "nullremote4" {


provisioner "local-exec" {
    command = "chrome http://13.234.136.158/web/index.php"
  }
}

Thus the page comes out to be as follows:


Step 6: Destroy all the resources created in this task. 



And thus, we come to the end of the task.

Thanks for reading the post!

Comments

Popular posts from this blog

How Unilever Benefitted by Transforming into Cloud-Based Digital-Market Empire

Creating custom Terraform module - creating submodule for s3 bucket resource from the AWS provider.