AWS Project #16 – Creating a VPC with Subnets Using AWS CloudFormation

Listen to this Post

In this article, we will explore how to create a Virtual Private Cloud (VPC) with subnets using AWS CloudFormation. This hands-on lab is ideal for those interested in Infrastructure as Code (IaC), AWS networking, and cloud automation.

Key Tasks Completed:

1. Created a VPC with an Internet Gateway.

2. Deployed public and private subnets using CloudFormation.

3. Associated subnets with appropriate route tables.

  1. Updated the stack to add more subnets across multiple AZs.

You Should Know:

To get started, you need to have the AWS CLI installed and configured on your machine. Below are the steps and commands to create a VPC with subnets using AWS CloudFormation.

Step 1: Install and Configure AWS CLI


<h1>Install AWS CLI</h1>

sudo apt-get update
sudo apt-get install awscli

<h1>Configure AWS CLI</h1>

aws configure

You will be prompted to enter your AWS Access Key, Secret Key, region, and output format.

Step 2: Create a CloudFormation Template

Create a YAML file named `vpc_template.yaml` with the following content:

AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyVPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: '10.0.0.0/16'
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: MyVPC

MyInternetGateway:
Type: 'AWS::EC2::InternetGateway'
Properties:
Tags:
- Key: Name
Value: MyInternetGateway

AttachGateway:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref MyVPC
InternetGatewayId: !Ref MyInternetGateway

PublicSubnet:
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref MyVPC
CidrBlock: '10.0.1.0/24'
AvailabilityZone: 'us-east-1a'
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: PublicSubnet

PrivateSubnet:
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref MyVPC
CidrBlock: '10.0.2.0/24'
AvailabilityZone: 'us-east-1b'
Tags:
- Key: Name
Value: PrivateSubnet

PublicRouteTable:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: PublicRouteTable

PrivateRouteTable:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: PrivateRouteTable

PublicRoute:
Type: 'AWS::EC2::Route'
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: '0.0.0.0/0'
GatewayId: !Ref MyInternetGateway

SubnetRouteTableAssociationPublic:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PublicSubnet
RouteTableId: !Ref PublicRouteTable

SubnetRouteTableAssociationPrivate:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PrivateSubnet
RouteTableId: !Ref PrivateRouteTable

Step 3: Deploy the CloudFormation Stack

Use the following command to deploy the stack:

aws cloudformation create-stack --stack-name MyVPCStack --template-body file://vpc_template.yaml --capabilities CAPABILITY_NAMED_IAM

Step 4: Verify the Stack Creation

You can verify the stack creation using the following command:

aws cloudformation describe-stacks --stack-name MyVPCStack

Step 5: Update the Stack

To update the stack and add more subnets, modify the `vpc_template.yaml` file and use the following command:

aws cloudformation update-stack --stack-name MyVPCStack --template-body file://vpc_template.yaml --capabilities CAPABILITY_NAMED_IAM

What Undercode Say:

Creating a VPC with subnets using AWS CloudFormation is a powerful way to manage your cloud infrastructure as code. This approach not only automates the deployment process but also ensures consistency and repeatability. By mastering these skills, you can significantly enhance your cloud automation capabilities, making you a valuable asset in the DevOps and cloud engineering fields.

Expected Output:

  • A fully functional VPC with public and private subnets.
  • Properly configured route tables and internet gateway.
  • Automated deployment and updates using AWS CloudFormation.

Useful URLs:

References:

Reported By: Omeatai Aws – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image