AWS VPC Creation - Step by Step
Tags: aws, linux, ec2, ubuntu
Published: 2022-07-16 14:27:43

![Architecture Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nti327hmr7a642l03njz.png)
Firstly, go through Architecture Diagram. AWS provides many services for manage the Virtual network. In this guide, you can create AWS VPC step by step.

In AWS Management Console search and go VPC services after that click creates vpc button.
![Create Vpc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhqpum5za28ss9pyve2x.jpg)

Give the name for VPC and add IPv4 CIDR. You can use the 10.0.0.0/16 CIDR range.
![CIDR add VPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fc6iypvs89705iuov6gu.jpg)

Create Two Subnets. Firstly create a public subnet. You can use the 10.0.1.0/24 CIDR range for the public subnet. Select the availability zone and give the name for the subnet.
![Create Public Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isa5gsphnpfq8sgq1fzs.jpg)

Now the same interface, click add new subnet button for private subnet creation.
![Click add new subnet button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8gu6lbf5gz1ir9kg07h.jpg)

Add IPv4 CIDR as 10.0.2.0/24. After that give the subnet name and select the availability zone. finally, click create subnet button.
![Subnet Creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9d0f7b4vfpsztqf7mqv3.jpg)

Now create an internet gateway for public subnet internet access. In VPC Console select Internet Gateway and create internet gateway. Give the name of Internet Gateway and hit create button.
![Create IGW](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01ivd969kl96gdsqyrks.jpg)

Next, we want to attach this internet gateway for VPC. You can select the internet gateway and click Attach to VPC.
![IGW attach VPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0e0fhhn91rawgmf68oj.jpg)

Next, select the previously created VPC and click attach internet gateway button.
![IGW attach VPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkq87foky5lrtlvriust.jpg)

Next, Go to the route table and create a route table for the public subnet. give the name for the route table and select the previously created VPC. The next click creates the route table button.
![Create Public Route Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l89o80ovlu3n4imnls66.jpg)

Click the public subnet route table click the Route tab and add route 0.0.0.0/0 and select the previously created Internet Gateway. next hit the save changes button.
![Add route for public route table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7v8dmdl589iyhjt9kuv3.jpg)

In the public subnet route table click the subnet association section and click edit "Explicit subnet associations" section.
![Subnet associations for public route table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ks94pqdvqwod88k4jow8.jpg)

Next, select public subnet and click the save association button.
![Subnet Associated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rb1ae9fbu2idou68ard.jpg)

Now we go to public subnet and click subnet settings and enable "Enable auto-assign public IPv4 address" and click save.
![Enable auto-assign public IPv4 address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eykj88r7cftvjt1fm1ml.jpg)

Next, we want to create 2 ec2 instances inside public and private subnets. Use ubuntu server 20.04 LTS AMI.
![EC2 Creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yun31u4oqa0ondar6tew.jpg)

You can see the ec2 instance is up and running.
![EC2 Up & Running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9514kt4vsoa3p5s2fcxw.jpg)

In the EC2 console click Elastic ip and create ELastic ip like follwing.
![Eip create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dan6fd3zt9q14vmknlbh.jpg)

Next, select the newly created Elastic ip and click allocate elastic ip button.
![Allocate Eip](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkhytg1f32r0rnf8dj9c.jpg)

Select previosly created public ec2 and click the associate button.
![Select EC2 in Eip](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxwdlq3fkh51hws68f1n.jpg)

Next, Connect public EC2 via SSH client or EC2 Instance Connect. Try to update ubuntu. Working it means public ec2 can connect internet.
![Public EC2 install update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8j16g7xx7nvl2mcg45l.jpg)

Now, we try to SSH into EC2 in Private Subnet. (It means bastion host). In Linux Terminal You can using the following steps.

- Configuring the SSH agent using the following command.
`ssh-add -L {{ssh-keyfile-name.pem}}`

- Next, connect the bastion host(Public EC2) using this command.
`ssh –A ubuntu@{{Bastion-IP-address or DNS}}`

- Connect to the private instances from the bastion host (Agent Forwarding).
`ssh ec2-user@{{InstanceIP or DNS}}`

Inside the private ec2 terminal "ping google.com" it's not given any response because it doesn't connect internet.
![Private EC2 ping response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/807xj1y4f6ju1t8oq7lp.jpg)

Next, go to the VPC console and create NAT Gateway. In NAT gateway creation select subnet as public subnet and give the name for NAT Gateway. After that click Allocate Elastic IP button and finally click create nat gateway button.
![Create NAT Gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kl7e6z6dx1g9fmzoao7.jpg)

Wait a few minutes for Nat Gateway up and running.
![NAT Up & Running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlt6jtwzhvtg3un9wmpk.jpg)

Next, Create a private route table for the private subnet. Use previous Public Route table creation same scenario. After creating the Private Route table and adding the follwing route for accessing private EC2 Internet. (select previously created nat gateway)
![Create Private Route Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dav2vx4tz50z11xta1s8.jpg)

Next, add subnet association. Select private subnet and click save association button.
![Subnet association for private route table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyvqxzi3wkmdo4zywz1d.jpg)

Finally, try to "ping google.com" inside a private subnet terminal. You can see the following output.
![ping google.com in private EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4sbxjtc1zct11r990fg.jpg)

Congratulations, Now you can create AWS VPC.

For clean up
- Terminate instances
- Release Elastic IP
- Delete NAT Gateway
- Delete Internet Gateway
- Delete VPC

Thanks for reading the Article.

Resources
https://gist.github.com/sanju2/5273a22f68342080ced33b7b12fef14b