0% found this document useful (0 votes)
4 views5 pages

!ref:: - : - :::::::::: - :::::::::: !ref: !ref:::::::: !ref

The document provides step-by-step guides for automating the creation of a Virtual Private Cloud (VPC) using Infrastructure as Code (IaC) tools such as AWS CloudFormation, Terraform, AWS CDK, and AWS CLI. Each section includes example templates or scripts and deployment instructions for the respective tools. It concludes with a summary of the best use cases for each tool, helping users choose the most suitable option for their needs.

Uploaded by

malikmdnurani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

!ref:: - : - :::::::::: - :::::::::: !ref: !ref:::::::: !ref

The document provides step-by-step guides for automating the creation of a Virtual Private Cloud (VPC) using Infrastructure as Code (IaC) tools such as AWS CloudFormation, Terraform, AWS CDK, and AWS CLI. Each section includes example templates or scripts and deployment instructions for the respective tools. It concludes with a summary of the best use cases for each tool, helping users choose the most suitable option for their needs.

Uploaded by

malikmdnurani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Creating a VPC (Virtual Private Cloud) using automation typically involves using Infrastructure as

Code (IaC) tools like AWS CloudFormation, Terraform, or AWS CDK. Below are step-by-step
guides for automating VPC creation using these tools:

1. Using AWS CloudFormation


AWS CloudFormation is a native AWS service for automating infrastructure deployment using JSON
or YAML templates.
Example CloudFormation Template (YAML):
yaml Copy
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: MyAutomatedVPC

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

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

GatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref MyVPC
InternetGatewayId: !Ref InternetGateway

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

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

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

Steps to Deploy:
. Save the template as vpc-template.yaml .
. Deploy the stack using the AWS CLI:
bash Copy
aws cloudformation create-stack --stack-name MyVPCStack --template-body file://vpc-template.ya
ml --region us-east-1
. Monitor the stack creation in the AWS Management Console or using the CLI:
bash Copy
aws cloudformation describe-stacks --stack-name MyVPCStack
2. Using Terraform
Terraform is a popular open-source IaC tool that supports multiple cloud providers.
Example Terraform Configuration ( main.tf ):
hcl Copy
provider "aws" {
region = "us-east-1"
}

resource "aws_vpc" "my_vpc" {


cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true

tags = {
Name = "MyAutomatedVPC"
}
}

resource "aws_subnet" "public_subnet" {


vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"

tags = {
Name = "PublicSubnet"
}
}

resource "aws_internet_gateway" "igw" {


vpc_id = aws_vpc.my_vpc.id

tags = {
Name = "MyInternetGateway"
}
}

resource "aws_route_table" "public_route_table" {


vpc_id = aws_vpc.my_vpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}

tags = {
Name = "PublicRouteTable"
}
}

resource "aws_route_table_association" "public_subnet_association" {


subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_route_table.id
}

Steps to Deploy:
. Initialize Terraform:
bash Copy
terraform init
. Preview the changes:
bash Copy
terraform plan
. Apply the configuration:
bash Copy
terraform apply
. Confirm the deployment by typing yes .
3. Using AWS CDK (Cloud Development Kit)
AWS CDK allows you to define cloud infrastructure using programming languages like Python,
TypeScript, Java, etc.
Example CDK Code (Python):
python Copy
from aws_cdk import (
aws_ec2 as ec2,
core,
)

class VpcStack(core.Stack):

def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:


super().__init__(scope, id, **kwargs)

# Create a VPC
vpc = ec2.Vpc(self, "MyVPC",
cidr="10.0.0.0/16",
max_azs=2,
subnet_configuration=[
ec2.SubnetConfiguration(
name="PublicSubnet",
subnet_type=ec2.SubnetType.PUBLIC,
cidr_mask=24
)
]
)

# Add tags
core.Tags.of(vpc).add("Name", "MyAutomatedVPC")

app = core.App()
VpcStack(app, "VpcStack")
app.synth()

Steps to Deploy:
. Install the AWS CDK:
bash Copy
npm install -g aws-cdk
. Bootstrap your AWS account (if not already done):
bash Copy
cdk bootstrap
. Synthesize the template:
bash Copy
cdk synth
. Deploy the stack:
bash Copy
cdk deploy
4. Using AWS CLI (Manual Automation)
If you prefer scripting, you can use the AWS CLI to create a VPC and related resources.
Example Script:
bash Copy
# Create VPC
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query 'Vpc.VpcId' --output text)
aws ec2 create-tags --resources $VPC_ID --tags Key=Name,Value=MyAutomatedVPC
# Create Subnet
SUBNET_ID=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.1.0/24 --availability-zone u
s-east-1a --query 'Subnet.SubnetId' --output text)
aws ec2 create-tags --resources $SUBNET_ID --tags Key=Name,Value=PublicSubnet

# Create Internet Gateway


IGW_ID=$(aws ec2 create-internet-gateway --query 'InternetGateway.InternetGatewayId' --output t
ext)
aws ec2 attach-internet-gateway --vpc-id $VPC_ID --internet-gateway-id $IGW_ID

# Create Route Table


ROUTE_TABLE_ID=$(aws ec2 create-route-table --vpc-id $VPC_ID --query 'RouteTable.RouteTableId'
--output text)
aws ec2 create-route --route-table-id $ROUTE_TABLE_ID --destination-cidr-block 0.0.0.0/0 --gatewa
y-id $IGW_ID
aws ec2 associate-route-table --subnet-id $SUBNET_ID --route-table-id $ROUTE_TABLE_ID

Steps to Run:
. Save the script as create-vpc.sh .
. Make it executable:
bash Copy
chmod +x create-vpc.sh
. Run the script:
bash Copy
./create-vpc.sh
Summary
CloudFormation: Best for native AWS integration.
Terraform: Best for multi-cloud or complex setups.
AWS CDK: Best for developers who prefer coding over YAML/JSON.
AWS CLI: Best for quick, script-based automation.

Choose the tool that best fits your workflow!

You might also like