· 9 years ago · Oct 26, 2016, 06:28 AM
1SHELL := /bin/bash
2
3VPC_CIDR ?= 10.240.0.0/16
4SUBNET_CIDR ?= 10.240.0.0/24
5
6aws-vars:
7ifndef AWS_DEFAULT_REGION
8 $(error AWS_DEFAULT_REGION is not set)
9else ifndef AWS_ACCESS_KEY_ID
10 $(error AWS_ACCESS_KEY_ID is not set)
11else ifndef AWS_SECRET_ACCESS_KEY
12 $(error AWS_SECRET_ACCESS_KEY is not set)
13else ifndef VPC_CIDR
14 $(error VPC_CIDR is not set)
15endif
16
17### Get Targets ###
18
19get-vpc-id-by-tag-%: aws-vars
20 $(eval VPC_ID := $(shell aws ec2 describe-vpcs --filters "Name=tag:Name,Values=$*" | jq -r '.Vpcs[].VpcId'))
21
22get-dhcp-options-set-by-tag-%: aws-vars
23 $(eval DHCP_OPTIONS_SET_ID := $(shell aws ec2 describe-dhcp-options --filters "Name=tag:Name,Values=$*" | jq -r '.DhcpOptions[].DhcpOptionsId'))
24
25get-subnet-id-by-tag-%: aws-vars
26 $(eval SUBNET_ID := $(shell aws ec2 describe-subnets --filters "Name=tag:Name,Values=$*" | jq -r '.Subnets[].SubnetId'))
27
28get-igw-id-by-tag-%: aws-vars
29 $(eval IGW_ID := $(shell aws ec2 describe-internet-gateways --filters "Name=tag:Name,Values=$*" | jq -r '.InternetGateways[].InternetGatewayId'))
30
31get-igw-attachment-state-by-tag-%: aws-vars
32 $(eval IGW_ATTACH_STATE := $(shell aws ec2 describe-internet-gateways --filters "Name=tag:Name,Values=$*" | jq -r '.InternetGateways[].Attachments[].State'))
33
34get-route-table-id-by-tag-%: aws-vars
35 $(eval ROUTE_TABLE_ID := $(shell aws ec2 describe-route-tables --filters "Name=tag:Name,Values=$*" | jq -r '.RouteTables[].RouteTableId'))
36
37get-route-table-association-id-by-tag-%: aws-vars
38 $(eval ROUTE_TABLE_ASSOCIATION_ID := $(shell aws ec2 describe-route-tables --filters "Name=tag:Name,Values=$*" | jq -r '.RouteTables[].Associations[].RouteTableAssociationId'))
39
40### Create Targets ###
41
42create-vpc-%: aws-vars get-vpc-id-by-tag-%
43 @if [[ -z "$(VPC_ID)" ]]; then \
44 echo "Creating VPC with CIDR $(VPC_CIDR) and tag: $*" && \
45 VPC_ID=$$(aws ec2 create-vpc --cidr-block $(VPC_CIDR) | jq -r .Vpc.VpcId) && \
46 aws ec2 create-tags --resources $${VPC_ID} --tags Key=Name,Value=$* && \
47 aws ec2 modify-vpc-attribute --vpc-id $${VPC_ID} --enable-dns-support '{"Value": true}' && \
48 aws ec2 modify-vpc-attribute --vpc-id $${VPC_ID} --enable-dns-hostnames '{"Value": true}' ; \
49 else \
50 echo "VPC with CIDR $(VPC_CIDR) and tag '$*' already exists." ; \
51 fi
52
53create-dhcp-options-set-%: aws-vars get-dhcp-options-set-by-tag-%
54 @if [[ -z "$(DHCP_OPTIONS_SET_ID)" ]]; then \
55 echo "Creating DHCP Options set with tag: $*" && \
56 DHCP_OPTIONS_SET_ID=$$(aws ec2 create-dhcp-options --dhcp-configuration "Key=domain-name,Values=$(AWS_DEFAULT_REGION).compute.internal" \
57 "Key=domain-name-servers,Values=AmazonProvidedDNS" | \
58 jq -r '.DhcpOptions.DhcpOptionsId') && \
59 aws ec2 create-tags --resources $${DHCP_OPTIONS_SET_ID} --tags Key=Name,Value=$* ; \
60 else \
61 echo "DHCP Options with tag '$*' already exists." ; \
62 fi
63
64associate-dhcp-options-with-vpc-%: aws-vars get-vpc-id-by-tag-% get-dhcp-options-set-by-tag-%
65 @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
66 @if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
67 aws ec2 associate-dhcp-options --dhcp-options-id $(DHCP_OPTIONS_SET_ID) --vpc-id $(VPC_ID)
68
69create-subnet-%: aws-vars get-subnet-id-by-tag-% get-vpc-id-by-tag-%
70 @if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
71 @if [[ -z "$(SUBNET_ID)" ]]; then \
72 echo "Creating Subnet with CIDR $(SUBNET_CIDR) in VPC $(VPC_ID) and tag: $*" && \
73 SUBNET_ID=$$(aws ec2 create-subnet --vpc-id $(VPC_ID) --cidr-block $(SUBNET_CIDR) | jq -r '.Subnet.SubnetId') && \
74 aws ec2 create-tags --resources $${SUBNET_ID} --tags Key=Name,Value=$* ; \
75 else \
76 echo "Subnet with tag '$*' already exists." ; \
77 fi
78
79create-igw-%: aws-vars get-igw-id-by-tag-%
80 @if [[ -z "$(IGW_ID)" ]]; then \
81 echo "Creating IGW with tag: $*" && \
82 INTERNET_GATEWAY_ID=$$(aws ec2 create-internet-gateway | jq -r '.InternetGateway.InternetGatewayId') && \
83 aws ec2 create-tags --resources $${INTERNET_GATEWAY_ID} --tags Key=Name,Value=$* ; \
84 else \
85 echo "Internet Gateway with tag '$*' already exists." ; \
86 fi
87
88attach-igw-%: aws-vars get-igw-id-by-tag-% get-vpc-id-by-tag-% get-igw-attachment-state-by-tag-%
89 @if [[ -z "$(IGW_ID)" ]]; then echo "ERROR: Internet Gateway with tag '$*' not found, create it with: make create-igw-$*" ; exit 1; fi
90 @if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
91 @if [[ -z "$(IGW_ATTACH_STATE)" ]]; then \
92 echo "Attaching IGW $(IGW_ID) to VPC $(VPC_ID)" && \
93 aws ec2 attach-internet-gateway --internet-gateway-id $(IGW_ID) --vpc-id $(VPC_ID) ; \
94 else \
95 echo "Internet Gateway $(IGW_ID) is already attached" ; \
96 fi
97
98create-route-table-%: aws-vars get-route-table-id-by-tag-% get-vpc-id-by-tag-%
99 @if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
100 @if [[ -z "$(ROUTE_TABLE_ID)" ]]; then \
101 echo "Creating Route Table in VPC $(VPC_ID) with tag: $*" && \
102 ROUTE_TABLE_ID=$$(aws ec2 create-route-table --vpc-id $(VPC_ID) | jq -r '.RouteTable.RouteTableId') && \
103 aws ec2 create-tags --resources $${ROUTE_TABLE_ID} --tags Key=Name,Value=$* ; \
104 else \
105 echo "Route Table with tag '$*' already exists." ; \
106 fi
107
108associate-route-table-%: aws-vars get-route-table-id-by-tag-% get-subnet-id-by-tag-% get-route-table-association-id-by-tag-%
109 @if [[ -z "$(ROUTE_TABLE_ID)" ]]; then echo "ERROR: Route Table with tag '$*' not found, create it with: make create-route-table-$*" ; exit 1; fi
110 @if [[ -z "$(SUBNET_ID)" ]]; then echo "ERROR: Subnet with tag '$*' not found, create it with: make create-subnet-$*" ; exit 1; fi
111 @if [[ -z "$(ROUTE_TABLE_ASSOCIATION_ID)" ]]; then \
112 echo "Associating route table $(ROUTE_TABLE_ID) with subnet $(SUBNET_ID)" && \
113 aws ec2 associate-route-table --route-table-id $(ROUTE_TABLE_ID) --subnet-id $(SUBNET_ID) ; \
114 else \
115 echo "Route Table $(ROUTE_TABLE_ID) already associated with subnet $(SUBNET_ID)" ; \
116 fi
117
118create-route-to-igw-%: get-route-table-id-by-tag-% get-igw-id-by-tag-%
119 @if [[ -z "$(ROUTE_TABLE_ID)" ]]; then echo "ERROR: Route Table with tag '$*' not found, create it with: make create-route-table-$*" ; exit 1; fi
120 @if [[ -z "$(IGW_ID)" ]]; then echo "ERROR: Internet Gateway with tag '$*' not found, create it with: make create-igw-$*" ; exit 1; fi
121 aws ec2 create-route --route-table-id $(ROUTE_TABLE_ID) --destination-cidr-block 0.0.0.0/0 --gateway-id $(IGW_ID)
122
123### Delete targets ###
124
125delete-vpc-%: aws-vars get-vpc-id-by-tag-%
126 @if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
127 aws ec2 delete-vpc --vpc-id $(VPC_ID)
128
129delete-dhcp-options-set-%: aws-vars get-dhcp-options-set-by-tag-%
130 @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
131 aws ec2 delete-dhcp-options --dhcp-options-id $(DHCP_OPTIONS_SET_ID)
132
133delete-subnet-%: aws-vars get-subnet-id-by-tag-%
134 @if [[ -z "$(SUBNET_ID)" ]]; then echo "ERROR: Subnet with tag '$*' not found, create it with: make create-subnet-$*" ; exit 1; fi
135 aws ec2 delete-subnet --subnet-id $(SUBNET_ID)
136
137detach-igw-%: aws-vars get-igw-id-by-tag-% get-vpc-id-by-tag-%
138 @if [[ -z "$(IGW_ID)" ]]; then echo "ERROR: Internet Gateway with tag '$*' not found, create it with: make create-igw-$*" ; exit 1; fi
139 @if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
140 aws ec2 detach-internet-gateway --internet-gateway-id $(IGW_ID) --vpc-id $(VPC_ID)
141
142delete-igw-%: aws-vars get-igw-id-by-tag-%
143 @if [[ -z "$(IGW_ID)" ]]; then echo "ERROR: Internet Gateway with tag '$*' not found, create it with: make create-igw-$*" ; exit 1; fi
144 aws ec2 delete-internet-gateway --internet-gateway-id $(IGW_ID)
145
146delete-route-table-%: aws-vars get-route-table-id-by-tag-%
147 @if [[ -z "$(ROUTE_TABLE_ID)" ]]; then echo "ERROR: Route Table with tag '$*' not found, create it with: make create-route-table-$*" ; exit 1; fi
148 aws ec2 delete-route-table --route-table-id $(ROUTE_TABLE_ID)
149
150### Super targets ###
151
152checkset = $(if $(1),\xe2\x9c\x94,\xe2\x9d\x8c)
153get-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-%
154 @echo "AWS Network Components tagged '$*':"
155 @printf " $(call checkset,$(VPC_ID)) VPC_ID:\t\t\t$(VPC_ID)\n"
156 @printf " $(call checkset,$(DHCP_OPTIONS_SET_ID)) DHCP_OPTIONS_SET_ID:\t$(DHCP_OPTIONS_SET_ID)\n"
157 @printf " $(call checkset,$(SUBNET_ID)) SUBNET_ID:\t\t\t$(SUBNET_ID)\n"
158 @printf " $(call checkset,$(IGW_ID)) IGW_ID:\t\t\t$(IGW_ID)\n"
159 @printf " $(call checkset,$(ROUTE_TABLE_ID)) ROUTE_TABLE_ID:\t\t$(ROUTE_TABLE_ID)\n"
160
161create-aws-network-%:
162 make create-vpc-$*
163 make create-dhcp-options-set-$*
164 make associate-dhcp-options-with-vpc-$*
165 make create-subnet-$*
166 make create-igw-$*
167 make attach-igw-$*
168 make create-route-table-$*
169 make create-route-to-igw-$*
170
171delete-aws-network-%: get-vpc-id-by-tag-%
172 @if [[ -z "$(VPC_ID)" ]]; then echo "ERROR: VPC with tag '$*' not found, create it with: make create-vpc-$*" ; exit 1 ; fi
173 @IN="" && until [[ "$$IN" =~ [yn] ]]; do read -p "Destroy VPC with tag '$*' in region $(AWS_DEFAULT_REGION)? (y/n): " IN; done ;
174 make delete-subnet-$*
175 make delete-route-table-$*
176 make detach-igw-$*
177 make delete-igw-$*
178 make delete-vpc-$*
179 make delete-dhcp-options-set-$*