AWS: Run an EC2 instance inside a VPC

Hassan Sheikh
4 min readNov 9, 2020

Let’s learn how to start an EC2 instance inside a VPC network, VPC stands for Virtual Private Cloud. It is an AWS service, to make your application isolated from the thousands of applications that are running on AWS. Other applications can communicate via an IGW (Internet Gateway). We will start by creating a VPC first, and then create a subnet inside the VPC. You can create multiple subnets. We will see how to assigns IP address ranges to these subnets. Finally, we will see how to run an EC2 instance and create an IGW. Let’s start…

FREE PEXELS from pexels.com

TABLE OF CONTENTS

What is a VPC

A Virtual Private Cloud (VPC) provides a frame or a box for your application to run. The box isolates your application from the millions of other applications running on AWS. The box makes sure that no access is allowed in it unless you provide specific rights to allow a certain type of traffic to access the application inside of the box. To create a VPC, you need to select a region that suits best your application, a range of Private IP addresses that will be working inside of the VPC. To start off easy, we will use the IP address starting from 10.10.0.0/16, the subnets will be created from a subset of these IP address ranges.

Subnets and VPC

Subnets, that live inside the VPC, make the communication between different assets of your application. Without these subnets, it will be difficult to manage the communication inside of the VPC. They are also used to allow traffic from IGW to your specific application asset. You can also allow certain traffic to be blocked using the subnets.

Starting A VPC

AWS VPC Dashboard
AWS VPS Dashboard

To start a VPC, go to the AWS management console and sign up with your details. Search for “VPC”, click on the link and you will be taken to the VPC dashboard. Select “Launch VPC Wizard”. Select “Create VPC with a Single Public Subnet”.

AWS Creating A VPC
AWS Creating A VPC

CIDR ranges are used for Inter-Domain Routing. We will use the 10.10.0.0/16 IP address ranges in this. You need to name this VPC, and then choose the subnets CIDR range. For this, we will use the first wild card range and provide this with 10.10.1.0/24 ranges. This means that the first 24 bits are frozen, and the subnet will use the IP addresses from 10.10.1.0 to 10.10.1.255. Choose an availability zone, and create the VPC.

Setting IGW

We have successfully created our VPC and our subnet. Now we need to allow traffic inside of the VPC and forward the traffic to the VPC we just created. To do so, in the VPC dashboard, click on Internet Gateways, and select “Create Internet Gateway”. Name the gateway accurately, because there can be multiple Internet Gateways. Make sure you know what gateway you want to use. Click “Create Gateway” and it’s done. You have created the Gateway. Now we need to attach it with the VPC. To do so, select, the gateway you just created, go to Actions, and then Attach a VPC. Select the VPC you just created and click Attach.

Also, you need to create a routing table for the traffic that is incoming to the VPC. To do so, click on Route Tables, name the Route Table and select the VPC, then select “Yes, Create”. Edit this Route Table to allow traffic from the Internet.

AWS Editing Routing Tables
AWS Editing Routing Tables

In the Destination, use the IP address 0.0.0.0/0. This represents incoming traffic from the internet. and select your VPC as Target. and Save this Routing Table. Also, our EC2 instance will be running inside of the subnet, so we need to associate our subnet to this Routing Table. In the Subnet Associations tab, edit the association and select the subnet you created. Click Save.

Conclusion

Everything is now complete. You need to create an EC2 instance, we have covered this in this post, make sure you read this. Now you have an EC2 instance in a VPC, that is up and running for the internet and can be accessed publically.

--

--