Create an EC2 Instance using Custom VPC

Nivas DevSecOps
7 min readAug 19, 2024

--

In this blog, I will show you how you can create an EC2 instance in AWS custom VPC using the AWS management console

What you will learn in this lesson?

  • Create a custom VPC
  • Create Public and Private subnets
  • Create Internet Gateway (IGW)
  • Attach IGW to the VPC
  • Create Route table
  • Subnet association to route table
  • Enable auto-assign public IPv4 for subnets
  • Create an EC2 machine using Custom VPC

Before going to the Demo let’s understand some of these services.

VPC

AWS VPC stands for Amazon Web Services Virtual Private Cloud. It is a virtual network that you can create within your AWS account. A VPC allows you to control your network environment, including IP address ranges, subnets, and routing tables. AWS VPC provides a secure and isolated environment for your resources while giving you complete control over your network configuration.

Subnet

A subnet is a range of IP addresses in your VPC (Virtual Private Cloud) that you can use to deploy your resources. A subnet is a logical subdivision of an IP network. When you create a VPC, you can divide it into one or more subnets, each of which can be in a different Availability Zone.

A subnet can be public or private. A public subnet is one that has a direct route to the internet, and resources in this subnet can be accessed from the public internet. A private subnet, on the other hand, does not have a direct route to the internet, and its resources can only be accessed through a NAT (Network Address Translation) instance or a VPN (Virtual Private Network) connection.

Each subnet has its own route table, and you can control the flow of traffic between subnets using security groups and network access control lists (ACLs)

Route Table

A route table is a set of rules, called routes, that are used to determine where network traffic is directed in a VPC. Each subnet in a VPC must be associated with a route table, and a route table can be associated with multiple subnets. By default, a VPC comes with a main route table that is associated with all the subnets in the VPC.

A route table contains a set of destination CIDR blocks and the target for each CIDR block. The target can be an internet gateway, a virtual private gateway, a network interface, a VPC peering connection, or a NAT gateway.

Internet Gateway (IGW)

IGW allows resources in your VPC to communicate with the internet, and vice versa. An IGW is attached to a VPC, and it provides a target for routes in the VPC’s route table that are meant for internet-bound traffic. When an instance in a subnet with a public IP address sends a request to the internet, the request is routed to the IGW, which then forwards it to the appropriate destination on the internet. Similarly, when a response is received from the internet, it is sent to the IGW, which then forwards it to the instance in the VPC.

🤙Now it’s time for the Demo:

🎯 Create a custom VPC using the AWS management console

Login to your AWS account and search VPC and follow the below steps;

🎯 Create subnets

If you don’t know how to calculate CIDR then open the subnet calculator or click here. Enter your VPC CIDR and select a number of subnets based on the requirement. I selected No. of subnets as 16.

Choose the first subnet address and give CIDR notation /20 as you see below. I named this subnet as public which doesn’t mean that this subnet is public. You will see how to configure this as a public subnet in this demo.

Now we have created 2 subnets and named them public and private. By default the subnet we create is private. Let’s see how we can configure our public subnet to make it public. Before that, we need to create IGW and attach IGW to the VPC.

🎯 Create IGW

🎯 Now let’s create a Route table

By default, every subnet we create is associated with the main route table. First, we can create a public route table and we can associate the public subnet with this route. Let’s see how we create;

Open the route table we created → Subnet association → Edit subnet association

We have associated our public subnet with Public RT.

Is the subnet public now?🤔

And the answer is No.

Ok then how we can make it public.

👇

Now we can see our public route table has 2 targets.

📌 Create Private RT

🎯 Enable auto-assign public IPv4 for public subnet

If you want to enable DNS you can enable it from here

🎯 Create an EC2 machine using a public subnet.

Name the instance and make sure that select the AMI and Instance type (t2.micro) are comes under the free tier.

Dropdown and select the VPC we created for this demo. Make sure you selected public subnet.

🧹 Do not forget to clean up the resources we created

📌 Terminate Instance

📌 Delete VPC

CONGRATULATIONS👏

--

--

Nivas DevSecOps
Nivas DevSecOps

Written by Nivas DevSecOps

Cloud | DevSecOps| AWS ⭐Passionate Cloud and DevOps . 🎯 Like to stay up-to-date with the latest trends and insights.

No responses yet