Skip to main content

AWS Command line script for creating basic VPC environment.

#!/bin/bash
###################################
#Created on 27-Jun-2018
#Purpose : This script will Create a VPC/Subnet/Routetable/Internetgateway/Natgateway and associate them to the corresponding subnets
#Modified on : 13-Sep-2018
####################################

vpcName="My-VPC"
vpcCidrBlock="10.0.0.0/16"
PubsubNetCidrBlock="10.0.1.0/24"
PrvsubNetCidrBlock="10.0.2.0/24"
pubAvailabilityZone="ap-northeast-1a"
prvAvailabilityZone="ap-northeast-1c"
pubSubnetName="PublicSubnet-My"
prvSubnetName="PrivateSubnet-My"
PubRouteTableName="MyPublicRoute"
PrvRouteTableName="MyPrivateRoute"
destinationCidrBlock="0.0.0.0/0"


#Create a VPC with a 10.0.0.0/16 CIDR block.
aws_response=$(aws ec2 create-vpc --cidr-block "$vpcCidrBlock" --output json)
vpcId=$(echo -e "$aws_response" |  /usr/bin/jq '.Vpc.VpcId' | tr -d '"')


#name the vpc
aws ec2 create-tags --resources "$vpcId" --tags Key=Name,Value="$vpcName"

#create internet gateway
gateway_response=$(aws ec2 create-internet-gateway --output json)
gatewayId=$(echo -e "$gateway_response" |  /usr/bin/jq '.InternetGateway.InternetGatewayId' | tr -d '"')

#name the internet gateway
aws ec2 create-tags --resources "$gatewayId" --tags Key=Name,Value=My-Gateway

#attach gateway to vpc
attach_response=$(aws ec2 attach-internet-gateway --internet-gateway-id "$gatewayId"  --vpc-id "$vpcId")

#create Public subnet for vpc with /24 cidr block
pub_subnet_response=$(aws ec2 create-subnet --cidr-block "$PubsubNetCidrBlock" --availability-zone "$pubAvailabilityZone" --vpc-id "$vpcId" --output json)
pubsubnetId=$(echo -e "$pub_subnet_response" |  /usr/bin/jq '.Subnet.SubnetId' | tr -d '"')

#name the Public subnet
aws ec2 create-tags --resources "$pubsubnetId" --tags Key=Name,Value="$pubSubnetName"

#enable public ip on public subnet
modify_response=$(aws ec2 modify-subnet-attribute --subnet-id "$pubsubnetId" --map-public-ip-on-launch)

#create Private subnet for vpc with /24 cidr block
prv_subnet_response=$(aws ec2 create-subnet --cidr-block "$PrvsubNetCidrBlock" --availability-zone "$prvAvailabilityZone" --vpc-id "$vpcId" --output json)
prvsubnetId=$(echo -e "$prv_subnet_response" |  /usr/bin/jq '.Subnet.SubnetId' | tr -d '"')

#name the Private subnet
aws ec2 create-tags --resources "$prvsubnetId" --tags Key=Name,Value="$prvSubnetName"


#create public route table for vpc
route_table_response=$(aws ec2 create-route-table --vpc-id "$vpcId" --output json)
pubrouteTableId=$(echo -e "$route_table_response" |  /usr/bin/jq '.RouteTable.RouteTableId' | tr -d '"')

#name the public route table
aws ec2 create-tags --resources "$pubrouteTableId" --tags Key=Name,Value="$PubRouteTableName"

#add route for the internet gateway
route_response=$(aws ec2 create-route --route-table-id "$pubrouteTableId" --destination-cidr-block "$destinationCidrBlock" --gateway-id "$gatewayId")


#Associate public subnet to public route table
associate_response=$(aws ec2 associate-route-table --subnet-id "$pubsubnetId" --route-table-id "$pubrouteTableId")


#create private route table for vpc
prv_route_table_response=$(aws ec2 create-route-table --vpc-id "$vpcId" --output json)
prvrouteTableId=$(echo -e "$prv_route_table_response" |  /usr/bin/jq '.RouteTable.RouteTableId' | tr -d '"')

#name the public route table
aws ec2 create-tags --resources "$prvrouteTableId" --tags Key=Name,Value="$PrvRouteTableName"


#Associate private subnet to private route table
prv_associate_response=$(aws ec2 associate-route-table --subnet-id "$prvsubnetId" --route-table-id "$prvrouteTableId")


#Allocate Elastic ip for NatGateway.
aws ec2 allocate-address --domain vpc

#Create Nategateway with association to the above created Elastic IP
aws ec2 create-nat-gateway --subnet-id "$prvsubnetId" --allocation-id eipalloc-04f4f6fdff6cefdc8

Comments

Popular posts from this blog

Setting /etc/hosts entries during the initial deployment of an Application using k8s yaml file

Some times we have to enter specific hosts file entries to the container running inside the POD of a kubernetes deployment during the initial deployment stage itself. If these entries are not in place, the application env variables mentioned in the yaml file , as hostnames , will not resolve to the IP address and the application will not start properly. So to make sure the /etc/hosts file entries are already there after the spin up of the POD you can add the below entries in your yaml file. cat > api-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: spec:   template:     metadata:     spec:       volumes:       containers:       - image: registryserver.jinojoseph.com:5000/jinojosephimage:v1.13         lifecycle:           postStart:             exec:               command:...

K8s External Secrets integration between AWS EKS and Secrets Manager(SM) using IAM Role.

What is K8s External Secrets and how it will make your life easier? Before saying about External Secrets we will say about k8s secrets and how it will work. In k8s secrets we will create key value pairs of the secrets and set this as either pod env variables or mount them as volumes to pods. For more details about k8s secrets you can check my blog http://jinojoseph.blogspot.com/2020/08/k8s-secrets-explained.html   So in this case if developers wants to change the ENV variables , then we have to edit the k8s manifest yaml file, then we have to apply the new files to the deployment. This is a tiresome process and also chances of applying to the wrong context is high if you have multiple k8s clusters for dev / stage and Prod deployments. So in-order to make this easy , we can add all the secrets that is needed in the deployment, in the AWS Secret Manager and with the help of External secrets we can fetch and create those secrets in the k8s cluster. So what is K8s external Secret? It i...

Running K8s cluster service kubelet with Swap Memory Enabled

For enabling swap memory check the below link : https://jinojoseph.blogspot.com/2019/10/enable-swap-memory-using-swapfile-in.html # sudo vi /etc/systemd/system/kubelet.service.d/10-kubeadm.conf Add the KUBELET_EXTRA_ARGS line as below: ---------------------------------------- Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false" ExecStart= ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS Now kubelet.service changed on disk. Run 'systemctl daemon-reload' to reload units # sudo systemctl daemon-reload # sudo systemctl restart kubelet # sudo systemctl status kubelet That is all cheers :p