!ref:: - : - :::::::::: - :::::::::: !ref: !ref:::::::: !ref
!ref:: - : - :::::::::: - :::::::::: !ref: !ref:::::::: !ref
Code (IaC) tools like AWS CloudFormation, Terraform, or AWS CDK. Below are step-by-step
guides for automating VPC creation using these tools:
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"
}
tags = {
Name = "MyAutomatedVPC"
}
}
tags = {
Name = "PublicSubnet"
}
}
tags = {
Name = "MyInternetGateway"
}
}
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "PublicRouteTable"
}
}
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):
# 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
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.