Skip to content

Instantly share code, notes, and snippets.

@danisla
Last active October 25, 2016 22:24
Show Gist options
  • Save danisla/2e80003e6e6de80d5c64007f98ee68f1 to your computer and use it in GitHub Desktop.
Save danisla/2e80003e6e6de80d5c64007f98ee68f1 to your computer and use it in GitHub Desktop.
Makefile for AWS Networking
SHELL := /bin/bash
VPC_CIDR ?= 10.240.0.0/16
SUBNET_CIDR ?= 10.240.0.0/24
aws-vars:
ifndef AWS_DEFAULT_REGION
$(error AWS_DEFAULT_REGION is not set)
else ifndef AWS_ACCESS_KEY_ID
$(error AWS_ACCESS_KEY_ID is not set)
else ifndef AWS_SECRET_ACCESS_KEY
$(error AWS_SECRET_ACCESS_KEY is not set)
else ifndef VPC_CIDR
$(error VPC_CIDR is not set)
endif
### Get Targets ###
get-vpc-id-by-tag-%: aws-vars
$(eval VPC_ID := $(shell aws ec2 describe-vpcs --filters "Name=tag:Name,Values=$*" | jq -r '.Vpcs[].VpcId'))
get-dhcp-options-set-by-tag-%: aws-vars
$(eval DHCP_OPTIONS_SET_ID := $(shell aws ec2 describe-dhcp-options --filters "Name=tag:Name,Values=$*" | jq -r '.DhcpOptions[].DhcpOptionsId'))
get-subnet-id-by-tag-%: aws-vars
$(eval SUBNET_ID := $(shell aws ec2 describe-subnets --filters "Name=tag:Name,Values=$*" | jq -r '.Subnets[].SubnetId'))
get-igw-id-by-tag-%: aws-vars
$(eval IGW_ID := $(shell aws ec2 describe-internet-gateways --filters "Name=tag:Name,Values=$*" | jq -r '.InternetGateways[].InternetGatewayId'))
get-igw-attachment-state-by-tag-%: aws-vars
$(eval IGW_ATTACH_STATE := $(shell aws ec2 describe-internet-gateways --filters "Name=tag:Name,Values=$*" | jq -r '.InternetGateways[].Attachments[].State'))
get-route-table-id-by-tag-%: aws-vars
$(eval ROUTE_TABLE_ID := $(shell aws ec2 describe-route-tables --filters "Name=tag:Name,Values=$*" | jq -r '.RouteTables[].RouteTableId'))
get-route-table-association-id-by-tag-%: aws-vars
$(eval ROUTE_TABLE_ASSOCIATION_ID := $(shell aws ec2 describe-route-tables --filters "Name=tag:Name,Values=$*" | jq -r '.RouteTables[].Associations[].RouteTableAssociationId'))
### Create Targets ###
create-vpc-%: aws-vars get-vpc-id-by-tag-%
@if [[ -z "$(VPC_ID)" ]]; then \
echo "Creating VPC with CIDR $(VPC_CIDR) and tag: $*" && \
VPC_ID=$$(aws ec2 create-vpc --cidr-block $(VPC_CIDR) | jq -r .Vpc.VpcId) && \
aws ec2 create-tags --resources $${VPC_ID} --tags Key=Name,Value=$* && \
aws ec2 modify-vpc-attribute --vpc-id $${VPC_ID} --enable-dns-support '{"Value": true}' && \
aws ec2 modify-vpc-attribute --vpc-id $${VPC_ID} --enable-dns-hostnames '{"Value": true}' ; \
else \
echo "VPC with CIDR $(VPC_CIDR) and tag '$*' already exists." ; \
fi
create-dhcp-options-set-%: aws-vars get-dhcp-options-set-by-tag-%
@if [[ -z "$(DHCP_OPTIONS_SET_ID)" ]]; then \
echo "Creating DHCP Options set with tag: $*" && \
DHCP_OPTIONS_SET_ID=$$(aws ec2 create-dhcp-options --dhcp-configuration "Key=domain-name,Values=$(AWS_DEFAULT_REGION).compute.internal" \
"Key=domain-name-servers,Values=AmazonProvidedDNS" | \
jq -r '.DhcpOptions.DhcpOptionsId') && \
aws ec2 create-tags --resources $${DHCP_OPTIONS_SET_ID} --tags Key=Name,Value=$* ; \
else \
echo "DHCP Options with tag '$*' already exists." ; \
fi
associate-dhcp-options-with-vpc-%: aws-vars get-vpc-id-by-tag-% get-dhcp-options-set-by-tag-%
@if [[ -z "$(DHCP_OPTIONS_SET_ID)" ]]; then echo "ERROR: DHCP Options set with tag '$*' not found, create it with: make create-dhcp-options-set-$*" ; exit 1; fi
@if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
aws ec2 associate-dhcp-options --dhcp-options-id $(DHCP_OPTIONS_SET_ID) --vpc-id $(VPC_ID)
create-subnet-%: aws-vars get-subnet-id-by-tag-% get-vpc-id-by-tag-%
@if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
@if [[ -z "$(SUBNET_ID)" ]]; then \
echo "Creating Subnet with CIDR $(SUBNET_CIDR) in VPC $(VPC_ID) and tag: $*" && \
SUBNET_ID=$$(aws ec2 create-subnet --vpc-id $(VPC_ID) --cidr-block $(SUBNET_CIDR) | jq -r '.Subnet.SubnetId') && \
aws ec2 create-tags --resources $${SUBNET_ID} --tags Key=Name,Value=$* ; \
else \
echo "Subnet with tag '$*' already exists." ; \
fi
create-igw-%: aws-vars get-igw-id-by-tag-%
@if [[ -z "$(IGW_ID)" ]]; then \
echo "Creating IGW with tag: $*" && \
INTERNET_GATEWAY_ID=$$(aws ec2 create-internet-gateway | jq -r '.InternetGateway.InternetGatewayId') && \
aws ec2 create-tags --resources $${INTERNET_GATEWAY_ID} --tags Key=Name,Value=$* ; \
else \
echo "Internet Gateway with tag '$*' already exists." ; \
fi
attach-igw-%: aws-vars get-igw-id-by-tag-% get-vpc-id-by-tag-% get-igw-attachment-state-by-tag-%
@if [[ -z "$(IGW_ID)" ]]; then echo "ERROR: Internet Gateway with tag '$*' not found, create it with: make create-igw-$*" ; exit 1; fi
@if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
@if [[ -z "$(IGW_ATTACH_STATE)" ]]; then \
echo "Attaching IGW $(IGW_ID) to VPC $(VPC_ID)" && \
aws ec2 attach-internet-gateway --internet-gateway-id $(IGW_ID) --vpc-id $(VPC_ID) ; \
else \
echo "Internet Gateway $(IGW_ID) is already attached" ; \
fi
create-route-table-%: aws-vars get-route-table-id-by-tag-% get-vpc-id-by-tag-%
@if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
@if [[ -z "$(ROUTE_TABLE_ID)" ]]; then \
echo "Creating Route Table in VPC $(VPC_ID) with tag: $*" && \
ROUTE_TABLE_ID=$$(aws ec2 create-route-table --vpc-id $(VPC_ID) | jq -r '.RouteTable.RouteTableId') && \
aws ec2 create-tags --resources $${ROUTE_TABLE_ID} --tags Key=Name,Value=$* ; \
else \
echo "Route Table with tag '$*' already exists." ; \
fi
associate-route-table-%: aws-vars get-route-table-id-by-tag-% get-subnet-id-by-tag-% get-route-table-association-id-by-tag-%
@if [[ -z "$(ROUTE_TABLE_ID)" ]]; then echo "ERROR: Route Table with tag '$*' not found, create it with: make create-route-table-$*" ; exit 1; fi
@if [[ -z "$(SUBNET_ID)" ]]; then echo "ERROR: Subnet with tag '$*' not found, create it with: make create-subnet-$*" ; exit 1; fi
@if [[ -z "$(ROUTE_TABLE_ASSOCIATION_ID)" ]]; then \
echo "Associating route table $(ROUTE_TABLE_ID) with subnet $(SUBNET_ID)" && \
aws ec2 associate-route-table --route-table-id $(ROUTE_TABLE_ID) --subnet-id $(SUBNET_ID) ; \
else \
echo "Route Table $(ROUTE_TABLE_ID) already associated with subnet $(SUBNET_ID)" ; \
fi
create-route-to-igw-%: get-route-table-id-by-tag-% get-igw-id-by-tag-%
@if [[ -z "$(ROUTE_TABLE_ID)" ]]; then echo "ERROR: Route Table with tag '$*' not found, create it with: make create-route-table-$*" ; exit 1; fi
@if [[ -z "$(IGW_ID)" ]]; then echo "ERROR: Internet Gateway with tag '$*' not found, create it with: make create-igw-$*" ; exit 1; fi
aws ec2 create-route --route-table-id $(ROUTE_TABLE_ID) --destination-cidr-block 0.0.0.0/0 --gateway-id $(IGW_ID)
### Delete targets ###
delete-vpc-%: aws-vars get-vpc-id-by-tag-%
@if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
aws ec2 delete-vpc --vpc-id $(VPC_ID)
delete-dhcp-options-set-%: aws-vars get-dhcp-options-set-by-tag-%
@if [[ -z "$(DHCP_OPTIONS_SET_ID)" ]]; then echo "ERROR: DHCP Options set with tag '$*' not found, create it with: make create-dhcp-options-set-$*" ; exit 1; fi
aws ec2 delete-dhcp-options --dhcp-options-id $(DHCP_OPTIONS_SET_ID)
delete-subnet-%: aws-vars get-subnet-id-by-tag-%
@if [[ -z "$(SUBNET_ID)" ]]; then echo "ERROR: Subnet with tag '$*' not found, create it with: make create-subnet-$*" ; exit 1; fi
aws ec2 delete-subnet --subnet-id $(SUBNET_ID)
detach-igw-%: aws-vars get-igw-id-by-tag-% get-vpc-id-by-tag-%
@if [[ -z "$(IGW_ID)" ]]; then echo "ERROR: Internet Gateway with tag '$*' not found, create it with: make create-igw-$*" ; exit 1; fi
@if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
aws ec2 detach-internet-gateway --internet-gateway-id $(IGW_ID) --vpc-id $(VPC_ID)
delete-igw-%: aws-vars get-igw-id-by-tag-%
@if [[ -z "$(IGW_ID)" ]]; then echo "ERROR: Internet Gateway with tag '$*' not found, create it with: make create-igw-$*" ; exit 1; fi
aws ec2 delete-internet-gateway --internet-gateway-id $(IGW_ID)
delete-route-table-%: aws-vars get-route-table-id-by-tag-%
@if [[ -z "$(ROUTE_TABLE_ID)" ]]; then echo "ERROR: Route Table with tag '$*' not found, create it with: make create-route-table-$*" ; exit 1; fi
aws ec2 delete-route-table --route-table-id $(ROUTE_TABLE_ID)
### Super targets ###
checkset = $(if $(1),\xe2\x9c\x94,\xe2\x9d\x8c)
get-aws-network-%: get-vpc-id-by-tag-% get-dhcp-options-set-by-tag-% get-subnet-id-by-tag-% get-igw-id-by-tag-% get-route-table-id-by-tag-%
@echo "AWS Network Components tagged '$*':"
@printf " $(call checkset,$(VPC_ID)) VPC_ID:\t\t\t$(VPC_ID)\n"
@printf " $(call checkset,$(DHCP_OPTIONS_SET_ID)) DHCP_OPTIONS_SET_ID:\t$(DHCP_OPTIONS_SET_ID)\n"
@printf " $(call checkset,$(SUBNET_ID)) SUBNET_ID:\t\t\t$(SUBNET_ID)\n"
@printf " $(call checkset,$(IGW_ID)) IGW_ID:\t\t\t$(IGW_ID)\n"
@printf " $(call checkset,$(ROUTE_TABLE_ID)) ROUTE_TABLE_ID:\t\t$(ROUTE_TABLE_ID)\n"
create-aws-network-%:
make create-vpc-$*
make create-dhcp-options-set-$*
make associate-dhcp-options-with-vpc-$*
make create-subnet-$*
make create-igw-$*
make attach-igw-$*
make create-route-table-$*
make create-route-to-igw-$*
delete-aws-network-%: get-vpc-id-by-tag-%
@if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
@IN="" && until [[ "$$IN" =~ [yn] ]]; do read -p "Destroy VPC with tag '$*' in region $(AWS_DEFAULT_REGION)? (y/n): " IN; done ;
make delete-subnet-$*
make delete-route-table-$*
make detach-igw-$*
make delete-igw-$*
make delete-vpc-$*
make delete-dhcp-options-set-$*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment