From b1de4cefcfcd7bf922285fd159507a4c1e6597de Mon Sep 17 00:00:00 2001 From: JustFiesta Date: Tue, 21 May 2024 14:46:53 +0200 Subject: [PATCH] Fixup: Tags for internet gateway added at creation, changed reads order, fixed security group id fetch --- prepare_aws_enviroment.sh | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/prepare_aws_enviroment.sh b/prepare_aws_enviroment.sh index 8eabc45e9..77f26f8b6 100644 --- a/prepare_aws_enviroment.sh +++ b/prepare_aws_enviroment.sh @@ -19,13 +19,13 @@ echo "" read -p "Enter owner name: " OWNER && export OWNER read -p "Enter project name: " PROJECT && export PROJECT read -p "Enter VPC name: " VPC_NAME && export VPC_NAME +read -p "Enter security group name: " SECURITY_GROUP_NAME && export SECURITY_GROUP_NAME +read -p "Enter internet gateway name: " INTERNERT_GATEWAY_NAME && export INTERNERT_GATEWAY_NAME read -p "Enter ECR repository name: " ECR_NAME && export ECR_NAME read -p "Enter EC2 instance name: " INSTANCE_NAME && export INSTANCE_NAME read -p "Enter key pair name: " KEY_PAIR_NAME && export KEY_PAIR_NAME -read -p "Enter security group name: " SECURITY_GROUP_NAME && export SECURITY_GROUP_NAME read -p "Enter Elastic IP name: " EIP_NAME && export EIP_NAME - echo "" echo "---------------------------------------" echo "" @@ -74,7 +74,6 @@ echo "" echo "---------------------------------------" echo "" - # Create Security Group echo "Creating Security Group..." SECURITY_GROUP_ID=$(aws ec2 create-security-group \ @@ -83,9 +82,11 @@ SECURITY_GROUP_ID=$(aws ec2 create-security-group \ --vpc-id "$VPC_ID" \ --tag-specifications 'ResourceType=security-group,Tags=[{Key=Name,Value='"$SECURITY_GROUP_NAME"'},{Key=Owner,Value='"$OWNER"'},{Key=Project,Value='"$PROJECT"'}]' \ --region "$REGION" \ - --query 'SecurityGroups[*].[GroupId]' \ + --query 'GroupId' \ --output text) +echo "Security Group ID: $SECURITY_GROUP_ID" + if [ -z "$SECURITY_GROUP_ID" ]; then echo "Error during Security Group creation." exit 1 @@ -103,6 +104,35 @@ aws ec2 authorize-security-group-ingress \ --region "$REGION" echo "Inbound SSH access has been allowed for Security Group." +echo "" +echo "---------------------------------------" +echo "" + +echo "Creating Internet Gateway..." +INTERNET_GATEWAY_ID=$(aws ec2 create-internet-gateway \ + --tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value='"$VPC_NAME"'},{Key=Owner,Value='"$OWNER"'},{Key=Project,Value='"$PROJECT"'}]' \ + --region "$REGION" \ + --query 'InternetGateway.InternetGatewayId' \ + --output text) + +if [ -z "$INTERNET_GATEWAY_ID" ]; then + echo "Error during Internet Gateway creation." + exit 1 +fi + +echo "Internet Gateway with ID $INTERNET_GATEWAY_ID has been created and tagged." + +# Attach Internet Gateway to VPC +aws ec2 attach-internet-gateway --internet-gateway-id "$INTERNET_GATEWAY_ID" --vpc-id "$VPC_ID" --region "$REGION" + +if [ $? -ne 0 ]; then + echo "Error during attaching Internet Gateway to VPC." + exit 1 +fi + +echo "Internet Gateway has been attached to VPC." + + echo "" echo "---------------------------------------" echo ""