115115
116116# Create VPC
117117echo " Creating VPC with CIDR block 10.0.0.0/16..."
118- VPC_ID=$( aws ec2 create-vpc --cidr-block 10.0.0.0/16 --tag-specifications ' ResourceType=vpc,Tags=[{Key=Name,Value=MyVPC}]' --query ' Vpc.VpcId' --output text)
118+ VPC_ID=$( aws ec2 create-vpc --cidr-block 10.0.0.0/16 --tag-specifications ' ResourceType=vpc,Tags=[{Key=Name,Value=MyVPC},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' --query ' Vpc.VpcId' --output text)
119119
120120if [ -z " $VPC_ID " ]; then
121121 handle_error " Failed to create VPC"
@@ -146,7 +146,7 @@ PUBLIC_SUBNET_AZ1=$(aws ec2 create-subnet \
146146 --vpc-id " $VPC_ID " \
147147 --cidr-block 10.0.0.0/24 \
148148 --availability-zone " $AZ1 " \
149- --tag-specifications ' ResourceType=subnet,Tags=[{Key=Name,Value=Public-Subnet-AZ1}]' \
149+ --tag-specifications ' ResourceType=subnet,Tags=[{Key=Name,Value=Public-Subnet-AZ1},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
150150 --query ' Subnet.SubnetId' \
151151 --output text)
152152
@@ -162,7 +162,7 @@ PUBLIC_SUBNET_AZ2=$(aws ec2 create-subnet \
162162 --vpc-id " $VPC_ID " \
163163 --cidr-block 10.0.1.0/24 \
164164 --availability-zone " $AZ2 " \
165- --tag-specifications ' ResourceType=subnet,Tags=[{Key=Name,Value=Public-Subnet-AZ2}]' \
165+ --tag-specifications ' ResourceType=subnet,Tags=[{Key=Name,Value=Public-Subnet-AZ2},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
166166 --query ' Subnet.SubnetId' \
167167 --output text)
168168
@@ -179,7 +179,7 @@ PRIVATE_SUBNET_AZ1=$(aws ec2 create-subnet \
179179 --vpc-id " $VPC_ID " \
180180 --cidr-block 10.0.2.0/24 \
181181 --availability-zone " $AZ1 " \
182- --tag-specifications ' ResourceType=subnet,Tags=[{Key=Name,Value=Private-Subnet-AZ1}]' \
182+ --tag-specifications ' ResourceType=subnet,Tags=[{Key=Name,Value=Private-Subnet-AZ1},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
183183 --query ' Subnet.SubnetId' \
184184 --output text)
185185
@@ -195,7 +195,7 @@ PRIVATE_SUBNET_AZ2=$(aws ec2 create-subnet \
195195 --vpc-id " $VPC_ID " \
196196 --cidr-block 10.0.3.0/24 \
197197 --availability-zone " $AZ2 " \
198- --tag-specifications ' ResourceType=subnet,Tags=[{Key=Name,Value=Private-Subnet-AZ2}]' \
198+ --tag-specifications ' ResourceType=subnet,Tags=[{Key=Name,Value=Private-Subnet-AZ2},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
199199 --query ' Subnet.SubnetId' \
200200 --output text)
201201
@@ -209,7 +209,7 @@ echo "Private subnet created in $AZ2 with ID: $PRIVATE_SUBNET_AZ2"
209209# Create Internet Gateway
210210echo " Creating Internet Gateway..."
211211IGW_ID=$( aws ec2 create-internet-gateway \
212- --tag-specifications ' ResourceType=internet-gateway,Tags=[{Key=Name,Value=MyIGW}]' \
212+ --tag-specifications ' ResourceType=internet-gateway,Tags=[{Key=Name,Value=MyIGW},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
213213 --query ' InternetGateway.InternetGatewayId' \
214214 --output text)
215215
@@ -228,7 +228,7 @@ aws ec2 attach-internet-gateway --internet-gateway-id "$IGW_ID" --vpc-id "$VPC_I
228228echo " Creating public route table..."
229229PUBLIC_RT=$( aws ec2 create-route-table \
230230 --vpc-id " $VPC_ID " \
231- --tag-specifications ' ResourceType=route-table,Tags=[{Key=Name,Value=Public-RT}]' \
231+ --tag-specifications ' ResourceType=route-table,Tags=[{Key=Name,Value=Public-RT},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
232232 --query ' RouteTable.RouteTableId' \
233233 --output text)
234234
@@ -266,7 +266,7 @@ CREATED_RESOURCES+=("ROUTE_TABLE_ASSOCIATION:$PUBLIC_RT_ASSOC_2")
266266echo " Creating private route table..."
267267PRIVATE_RT=$( aws ec2 create-route-table \
268268 --vpc-id " $VPC_ID " \
269- --tag-specifications ' ResourceType=route-table,Tags=[{Key=Name,Value=Private-RT}]' \
269+ --tag-specifications ' ResourceType=route-table,Tags=[{Key=Name,Value=Private-RT},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
270270 --query ' RouteTable.RouteTableId' \
271271 --output text)
272272
@@ -298,7 +298,7 @@ CREATED_RESOURCES+=("ROUTE_TABLE_ASSOCIATION:$PRIVATE_RT_ASSOC_2")
298298
299299# Allocate Elastic IP for NAT Gateway
300300echo " Allocating Elastic IP for NAT Gateway..."
301- EIP_ALLOC=$( aws ec2 allocate-address --domain vpc --query ' AllocationId' --output text)
301+ EIP_ALLOC=$( aws ec2 allocate-address --domain vpc --tag-specifications ' ResourceType=elastic-ip,Tags=[{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs}] ' -- query ' AllocationId' --output text)
302302
303303if [ -z " $EIP_ALLOC " ]; then
304304 handle_error " Failed to allocate Elastic IP"
@@ -312,7 +312,7 @@ echo "Creating NAT Gateway in public subnet in $AZ1..."
312312NAT_GW=$( aws ec2 create-nat-gateway \
313313 --subnet-id " $PUBLIC_SUBNET_AZ1 " \
314314 --allocation-id " $EIP_ALLOC " \
315- --tag-specifications ' ResourceType=natgateway,Tags=[{Key=Name,Value=MyNATGateway}]' \
315+ --tag-specifications ' ResourceType=natgateway,Tags=[{Key=Name,Value=MyNATGateway},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
316316 --query ' NatGateway.NatGatewayId' \
317317 --output text)
318318
@@ -344,6 +344,7 @@ WEB_SG=$(aws ec2 create-security-group \
344344 --group-name " WebServerSG-$( date +%s) " \
345345 --description " Security group for web servers" \
346346 --vpc-id " $VPC_ID " \
347+ --tag-specifications ' ResourceType=security-group,Tags=[{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs}]' \
347348 --query ' GroupId' \
348349 --output text)
349350
@@ -370,6 +371,7 @@ DB_SG=$(aws ec2 create-security-group \
370371 --group-name " DBServerSG-$( date +%s) " \
371372 --description " Security group for database servers" \
372373 --vpc-id " $VPC_ID " \
374+ --tag-specifications ' ResourceType=security-group,Tags=[{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs}]' \
373375 --query ' GroupId' \
374376 --output text)
375377
@@ -428,7 +430,7 @@ echo "Deploying EC2 instances..."
428430# Create key pair for SSH access
429431KEY_NAME=" vpc-tutorial-key-$( date +%s) "
430432echo " Creating key pair $KEY_NAME ..."
431- aws ec2 create-key-pair --key-name " $KEY_NAME " --query ' KeyMaterial' --output text > " ${KEY_NAME} .pem" || handle_error " Failed to create key pair"
433+ aws ec2 create-key-pair --key-name " $KEY_NAME " --tag-specifications ' ResourceType=key-pair,Tags=[{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs}] ' -- query ' KeyMaterial' --output text > " ${KEY_NAME} .pem" || handle_error " Failed to create key pair"
432434chmod 400 " ${KEY_NAME} .pem"
433435echo " Key pair saved to ${KEY_NAME} .pem"
434436CREATED_RESOURCES+=(" KEY_PAIR:$KEY_NAME " )
@@ -456,7 +458,7 @@ WEB_INSTANCE=$(aws ec2 run-instances \
456458 systemctl start httpd
457459 systemctl enable httpd
458460 echo "<h1>Hello from $(hostname -f) in the public subnet</h1>" > /var/www/html/index.html' \
459- --tag-specifications ' ResourceType=instance,Tags=[{Key=Name,Value=WebServer}]' \
461+ --tag-specifications ' ResourceType=instance,Tags=[{Key=Name,Value=WebServer},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
460462 --query ' Instances[0].InstanceId' \
461463 --output text) || handle_error " Failed to launch web server"
462464echo " Web server instance created with ID: $WEB_INSTANCE "
@@ -486,7 +488,7 @@ DB_INSTANCE=$(aws ec2 run-instances \
486488 yum install -y mariadb-server
487489 systemctl start mariadb
488490 systemctl enable mariadb' \
489- --tag-specifications ' ResourceType=instance,Tags=[{Key=Name,Value=DBServer}]' \
491+ --tag-specifications ' ResourceType=instance,Tags=[{Key=Name,Value=DBServer},{Key=project,Value=doc-smith},{Key=tutorial,Value=vpc-gs} ]' \
490492 --query ' Instances[0].InstanceId' \
491493 --output text) || handle_error " Failed to launch database server"
492494echo " Database server instance created with ID: $DB_INSTANCE "
0 commit comments