1+ #! /bin/bash
2+
3+ # Create VPC
4+ VPC_ID=$( aws ec2 create-vpc --cidr-block 172.31.0.0/16 \
5+ --instance-tenancy default \
6+ --tag-specifications ResourceType=vpc,Tags=' [{Key=Name,Value=web-vpc}]' \
7+ | jq -r ' .Vpc."VpcId"' )
8+
9+ # WARNING: Enable DNS hostnames from console!!!
10+
11+ echo VPC_ID=$VPC_ID
12+
13+ # Create web tier subnets
14+ WEB_3A=$( aws ec2 create-subnet --vpc-id $VPC_ID \
15+ --cidr-block 172.31.1.0/24 \
16+ --availability-zone eu-west-3a \
17+ --tag-specifications ResourceType=subnet,Tags=' [{Key=Name,Value=web-3a}]' \
18+ | jq -r ' .Subnet."SubnetId"' )
19+
20+ WEB_3B=$( aws ec2 create-subnet --vpc-id $VPC_ID \
21+ --cidr-block 172.31.2.0/24 \
22+ --availability-zone eu-west-3b \
23+ --tag-specifications ResourceType=subnet,Tags=' [{Key=Name,Value=web-3b}]' \
24+ | jq -r ' .Subnet."SubnetId"' )
25+
26+ echo WEB_3A=$WEB_3A
27+ echo WEB_3B=$WEB_3B
28+
29+ # Create app tier subnets
30+ APP_3A=$( aws ec2 create-subnet --vpc-id $VPC_ID \
31+ --availability-zone eu-west-3a \
32+ --cidr-block 172.31.101.0/24 \
33+ --tag-specifications ResourceType=subnet,Tags=' [{Key=Name,Value=app-3a}]' \
34+ | jq -r ' .Subnet."SubnetId"' )
35+
36+ APP_3B=$( aws ec2 create-subnet --vpc-id $VPC_ID \
37+ --availability-zone eu-west-3b \
38+ --cidr-block 172.31.102.0/24 \
39+ --tag-specifications ResourceType=subnet,Tags=' [{Key=Name,Value=app-3b}]' \
40+ | jq -r ' .Subnet."SubnetId"' )
41+
42+ echo APP_3A=$APP_3A
43+ echo APP_3B=$APP_3B
44+
45+ # Create internet gateway
46+ IGW=$( aws ec2 create-internet-gateway \
47+ --tag-specifications ResourceType=internet-gateway,Tags=' [{Key=Name,Value=webapp-igw}]' \
48+ | jq -r ' .InternetGateway."InternetGatewayId"' )
49+
50+ aws ec2 attach-internet-gateway --internet-gateway-id $IGW --vpc-id $VPC_ID
51+
52+ echo IGW=$IGW
53+
54+ # Create route table and associate with subnets
55+ RT=$( aws ec2 create-route-table --vpc-id $VPC_ID \
56+ --tag-specifications ResourceType=route-table,Tags=' [{Key=Name,Value=webapp-rt}]' \
57+ | jq -r ' .RouteTable."RouteTableId"' )
58+
59+ echo RT=$RT
60+
61+ aws ec2 associate-route-table --route-table-id $RT --subnet-id $WEB_3A
62+ aws ec2 associate-route-table --route-table-id $RT --subnet-id $WEB_3B
63+ aws ec2 associate-route-table --route-table-id $RT --subnet-id $APP_3A
64+ aws ec2 associate-route-table --route-table-id $RT --subnet-id $APP_3B
65+
66+ # Add default routes
67+ aws ec2 create-route \
68+ --route-table-id $RT \
69+ --destination-cidr-block 0.0.0.0/0 \
70+ --gateway-id $IGW
71+
72+ # Create security groups
73+ WEBSG=$( aws ec2 create-security-group \
74+ --group-name web-sg \
75+ --description " web-sg" \
76+ --vpc-id $VPC_ID | jq -r ' .GroupId' )
77+
78+ APPSG=$( aws ec2 create-security-group \
79+ --group-name app-sg \
80+ --description " app-sg" \
81+ --vpc-id $VPC_ID | jq -r ' .GroupId' )
82+
83+ DBSG=$( aws ec2 create-security-group \
84+ --group-name db-sg \
85+ --description " db-sg" \
86+ --vpc-id $VPC_ID | jq -r ' .GroupId' )
87+
88+ echo WEBSG=$WEBSG
89+ echo APPSG=$APPSG
90+ echo DBSG=$DBSG
91+
92+ # Set up permissions on Security Groups
93+ aws ec2 authorize-security-group-ingress \
94+ --group-id $WEBSG \
95+ --ip-permissions ' [{"IpProtocol":"tcp","FromPort":80,"ToPort":80,"IpRanges":[{"CidrIp":"0.0.0.0/0"}]},{"IpProtocol":"tcp","FromPort":443,"ToPort":443,"IpRanges": [{"CidrIp":"0.0.0.0/0"}]},{"IpProtocol":"tcp","FromPort":8443,"ToPort":8443,"IpRanges":[{"CidrIp":"172.31.1.0/24"},{"CidrIp":"172.31.2.0/24"},{"CidrIp":"172.31.101.0/24"},{"CidrIp":"172.31.102.0/24"}]},{"IpProtocol": "tcp","FromPort":81,"ToPort":81,"IpRanges":[{"CidrIp":"172.31.0.0/16"}]},{"IpProtocol":"tcp","FromPort":22,"ToPort": 22,"IpRanges":[{"CidrIp": "0.0.0.0/0"}]}]'
96+
97+ aws ec2 authorize-security-group-ingress \
98+ --group-id $APPSG \
99+ --ip-permissions ' [{"IpProtocol":"tcp","FromPort":8080,"ToPort":8080,"IpRanges":[{"CidrIp":"172.31.1.0/24"},{"CidrIp":"172.31.2.0/24"},{"CidrIp":"172.31.101.0/24"},{"CidrIp":"172.31.102.0/24"}]},{"IpProtocol":"tcp","FromPort":8443,"ToPort":8443,"IpRanges":[{"CidrIp":"172.31.1.0/24"},{"CidrIp":"172.31.2.0/24"},{"CidrIp":"172.31.101.0/24"},{"CidrIp":"172.31.102.0/24"}]},{"IpProtocol":"tcp","FromPort":22,"ToPort":22,"IpRanges":[{"CidrIp":"0.0.0.0/0"}]}]'
100+
101+ aws ec2 authorize-security-group-ingress \
102+ --group-id $DBSG \
103+ --ip-permissions ' [{"IpProtocol":"tcp","FromPort":27017,"ToPort":27017,"IpRanges":[{"CidrIp":"172.31.101.0/24"},{"CidrIp":"172.31.102.0/24"}]},{"IpProtocol":"tcp","FromPort":22,"ToPort":22,"IpRanges":[{"CidrIp": "0.0.0.0/0"}]}]'
104+
105+ # Set instance defaults
106+ INSTANCE_TYPE=t3.micro
107+ IMAGE_ID=ami-0302f42a44bf53a45
108+
109+ # Create web instances
110+ aws ec2 run-instances \
111+ --image-id $IMAGE_ID \
112+ --instance-type $INSTANCE_TYPE \
113+ --subnet-id $WEB_3A \
114+ --key-name devops_trainer_key \
115+ --security-group-ids $WEBSG \
116+ --associate-public-ip-address \
117+ --private-ip-address 172.31.1.21 \
118+ --tag-specifications ResourceType=instance,Tags=' [{Key=Name,Value=web1}]'
119+
120+ aws ec2 run-instances \
121+ --image-id $IMAGE_ID \
122+ --instance-type $INSTANCE_TYPE \
123+ --subnet-id $WEB_3B \
124+ --key-name devops_trainer_key \
125+ --security-group-ids $WEBSG \
126+ --associate-public-ip-address \
127+ --private-ip-address 172.31.2.22 \
128+ --tag-specifications ResourceType=instance,Tags=' [{Key=Name,Value=web2}]'
129+
130+ aws ec2 run-instances \
131+ --image-id $IMAGE_ID \
132+ --instance-type $INSTANCE_TYPE \
133+ --subnet-id $WEB_3B \
134+ --key-name devops_trainer_key \
135+ --security-group-ids $WEBSG \
136+ --associate-public-ip-address \
137+ --private-ip-address 172.31.2.23 \
138+ --tag-specifications ResourceType=instance,Tags=' [{Key=Name,Value=web3}]'
139+
140+ # Create app instances
141+ aws ec2 run-instances \
142+ --image-id $IMAGE_ID \
143+ --instance-type $INSTANCE_TYPE \
144+ --subnet-id $APP_3A \
145+ --key-name devops_trainer_key \
146+ --security-group-ids $APPSG \
147+ --associate-public-ip-address \
148+ --private-ip-address 172.31.101.21 \
149+ --tag-specifications ResourceType=instance,Tags=' [{Key=Name,Value=app1}]'
150+
151+ aws ec2 run-instances \
152+ --image-id $IMAGE_ID \
153+ --instance-type $INSTANCE_TYPE \
154+ --subnet-id $APP_3B \
155+ --key-name devops_trainer_key \
156+ --security-group-ids $APPSG \
157+ --associate-public-ip-address \
158+ --private-ip-address 172.31.102.22 \
159+ --tag-specifications ResourceType=instance,Tags=' [{Key=Name,Value=app2}]'
160+
161+ aws ec2 run-instances \
162+ --image-id $IMAGE_ID \
163+ --instance-type $INSTANCE_TYPE \
164+ --subnet-id $APP_3B \
165+ --key-name devops_trainer_key \
166+ --security-group-ids $APPSG \
167+ --associate-public-ip-address \
168+ --private-ip-address 172.31.102.23 \
169+ --tag-specifications ResourceType=instance,Tags=' [{Key=Name,Value=app3}]'
170+
171+ # Create db instance
172+ aws ec2 run-instances \
173+ --image-id $IMAGE_ID \
174+ --instance-type $INSTANCE_TYPE \
175+ --subnet-id $APP_3A \
176+ --key-name devops_trainer_key \
177+ --security-group-ids $DBSG \
178+ --associate-public-ip-address \
179+ --private-ip-address 172.31.101.99 \
180+ --tag-specifications ResourceType=instance,Tags=' [{Key=Name,Value=db}]'
0 commit comments