arvind.020606@gmail.com

The Power of AWS VPC - 04/02/2024

AWS VPC components with cli hands on

VPC Networking Components :

Subnet

Internet Gateway

Route Table

A route table contains a set of rules, called routes, that are used to determine where network traffic is directed.

Security Group

NACL

A network access control list (ACL) is an optional layer of security for our VPC that acts as a firewall for controlling traffic in and out of one or more subnets.

DHCP Option Set

The Dynamic Host Configuration Protocol (DHCP) provides a standard for passing configuration information to hosts on a TCP/IP network.

NAT Gateway

You can use a network address translation (NAT) gateway to enable instances in a private subnet to connect to the internet or other AWS services, but prevent the internet from initiating a connection with those instances.

Egress only Internet Gateway

An Egress only Internet Gateway is a horizontally scaled, redundant, and highly available VPC component that allows outbound communication over IPv6 from instances in your VPC to the internet and prevents the internet from initiating an IPv6 connection with your instances.

Elastic IP Addresses

VPC End Points

A VPC endpoint enables you to privately connect your VPC to supported AWs services.

VPC endpoint services powered by PrivateLink without requiring an internet gateway, NAT device, VPN connection, or AWS Direct Connect connection.

VPC Peering

Lets discuss some of this in more detailed way

Default VPC

Security Groups

Network ACLs

NAT Gateways

You can use a network address translation (NAT) gateway to enable instances in a private subnet to connect to the internet or other AWS services, but prevent the internet from initiating a connection with those instances.

NAT Instance

Alt text

Creating VPC Components Using AWS CLI

Things we are going to made :

VPC

2 or 3 subnets

Internet Gateaway -associate it with one of the subnet to create public subnet

security group

EC 2 instance

SSH to EC2

create a VPC

aws ec2 create-vpc --cidr-block 192.168.0.0/16

copy VpcId from the output of previous command.

ex VpcId: ID = vpc-00d41a71782c5eef

Now create subnet :

aws ec2 create-subnet --vpc-id vpc-[ID] --cidr-block 192.168.0.0/24

and replace ID with 00d41a71782c5eef (something like this)

so , the command will become :

aws ec2 create-subnet --vpc-id vpc-00d41a71782c5eef --cidr-block 192.168.0.0/24

copy the subnetId from the output of the previous command.

Use this command to describe your route table

aws ec2 describe-route-tables --route-table-id rtb-XXXXX

it will look like this : "SubnetId" : "subnet-XXXXXXXXXX"

Now, create another subnet using same command and copy its SubnetId.

Now create internet gateway :

aws ec2 create-internet-gateway

And copy the InternetGatewayId. It is looking something like this "igw-XXXXXXXXXXXX"

Currently our IGW is in detached mode. Now attach it to VPC. For doing that we need vpc id and internet gateway id

aws ec2 aatch-internet-gateway --vpc-id vpc-XXXXXX --internet-gateway-id --igw-XXXXXX

now, create a routing table

aws ec2 create-route-table --vpc-id vpc-XXXXXXXXXXX

Now, you will see 2 routing table in Route Tables section, the first one is the default one, don’t delete that.

copy the RouteTableId from the output. It looks like this rtb-XXXXXXXX

aws ec2 create-route --route-table-id rtb-XXXX --destination0cidr-block 0.0.0.0./0 --gateway-id igw-XXXXXX

You will get two routes, one is local route. (it is maintained by AWS). And other is the route we just created for IGW for connecting it to internet.

Now assoicate this particular route table to desired subnet.

aws ec2 associate-route-table --subnet-id subnet-XXXXX123 --route-table-id rtb-XXXXX

You will get this as output

{
 "AssociationId": "rtbassoc-XXXXX"
}

This command associate the route table with subnet and will make subnet-XXXXX123 a public subnet.

Now assign a public ip address to that subnet id.

aws ec2 modify-subnet-attribute --subnet-id subnet-XXXXX123 --map-public-ip-on-launch

Now create a security group :

aws ec2 create-security-group --group-name SG_SSHAccess --description "SSH access" --vpc-id vpc-XXXX

You will get something this like this in output

{
"GroupId": "sg-XXXXX"
}

Copy that GroupId and save it.

Now we will add inbound rules to this security group to allow ssh.

aws ec2 authorize-security-group-ingress --group-id sg-XXXXX --protocol tcp --port 22 --cidr 0.0.0.0/0

Now create a EC2 instance.

aws ec2 run-instances --image-id ami-XXXXXX --count 1 --instance-type t2.micfro --key-name [your-aws-ssh-key] --security-group-ids sg-XXXXX --subnet-id subnet-XXXX

Copy InstanceId from the output.

Run this command to describe your ec2 instance.

aws ec2 describe-instances --instance-id XXXXXX