Skip to main content

Command Palette

Search for a command to run...

πŸš€πŸ“… Day 55 DevOps Challenge - Creating a VPC and Launching a Website on AWS Using Terraform

Published
β€’3 min read
πŸš€πŸ“… Day 55 DevOps Challenge - Creating a VPC and Launching a Website on AWS Using Terraform
A

Passionate software engineering student | DevOps enthusiast | Seeking innovation and excellence in software engineering! πŸ‘¨β€πŸ’»

Welcome to our guide on creating a Virtual Private Cloud (VPC) and launching a website on Amazon Web Services (AWS) using Terraform. In this tutorial, we will walk you through the process of setting up a VPC, and subnets, launching an EC2 instance, and hosting a simple website.

Step 1: Set up the VPC and Subnets

First, let's set up the foundation of our infrastructure - the VPC and its associated subnets.

We'll use Terraform to define the infrastructure as code and create the VPC with the desired CIDR blocks.

terraform {
    required_providers {
        aws = {
            source = "hashicorp/aws"
            version = "5.19.0"
        }
    }
}
provider "aws" {
    region = "ap-south-1"
}
resource "aws_vpc" "example" {
    cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "public" {
    vpc_id                  = aws_vpc.example.id
    cidr_block              = "10.0.1.0/24"
}

resource "aws_subnet" "private" {
    vpc_id                  = aws_vpc.example.id
    cidr_block              = "10.0.2.0/24"
}

Step 2: Set up Internet Connectivity

Next, we need to set up internet connectivity by creating an Internet Gateway (IGW) and associating it with the VPC.

resource "aws_internet_gateway" "example" {
  vpc_id = aws_vpc.example.id
}

Step 3: Configure Route Table and Associate with Public Subnet

We need to configure a route table for the public subnet and associate it to enable internet access.

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.example.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.example.id
  }
}

resource "aws_route_table_association" "public" {
  subnet_id      = aws_subnet.public.id
  route_table_id = aws_route_table.public.id
}

Step 4: Create a Security Group

resource "aws_security_group" "web" {
  vpc_id = aws_vpc.example.id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

Step 5: Launch an EC2 Instance

Now, let's launch an EC2 instance in the public subnet and set it up to host a simple website.

resource "aws_instance" "webserver" {
    ami           = "ami-0f5ee92e2d63afc18"
    instance_type = "t2.micro"
    availability_zone = "ap-south-1b"
    subnet_id     = aws_subnet.public.id

    user_data = <<-EOF
                #!/bin/bash
                sudo apt update -y
                sudo apt install nginx -y
                EOF

    vpc_security_group_ids = [aws_security_group.web.id]
}

Step 6: Create an Elastic IP and Associate with the EC2 Instance

Lastly, let's create an Elastic IP and associate it with the EC2 instance for a static public IP.

resource "aws_eip" "example" {
    domain = "vpc"
    instance = aws_instance.webserver.id
}

Step 7: Verification

After applying the Terraform configuration, visit the website hosted on the EC2 instance by opening the Elastic IP in a web browser.

Congratulations! You have successfully created a VPC, set up subnets, launched an EC2 instance, and hosted a website on AWS using Terraform. This infrastructure is scalable and can serve as a starting point for more complex setups and applications. Happy coding!

More from this blog

Adarsh Jha

65 posts