mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-21 07:15:49 +00:00
Adding tags on recource creation
This commit is contained in:
parent
6fd739bfd8
commit
911e499e9c
1 changed files with 52 additions and 30 deletions
|
@ -26,43 +26,52 @@ read -p "Enter Elastic IP name: " EIP_NAME && export EIP_NAME
|
||||||
read -p "Enter key pair name: " KEY_PAIR_NAME && export KEY_PAIR_NAME
|
read -p "Enter key pair name: " KEY_PAIR_NAME && export KEY_PAIR_NAME
|
||||||
|
|
||||||
|
|
||||||
# Create VPC
|
|
||||||
echo "---------------------------------------"
|
|
||||||
|
|
||||||
echo "Creating VPC..."
|
echo "Creating VPC..."
|
||||||
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --region "$REGION" --query 'Vpc.VpcId' --output text)
|
VPC_ID=$(aws ec2 create-vpc \
|
||||||
|
--cidr-block 10.0.0.0/16 \
|
||||||
|
--region "$REGION" \
|
||||||
|
--tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value='"$VPC_NAME"'}{Key=Owner,Value='"$OWNER"'}{Key=Project,Value='"$PROJECT"'}]' \
|
||||||
|
--query 'Vpc.VpcId' \
|
||||||
|
--output text)
|
||||||
|
|
||||||
if [ -z "$VPC_ID" ]; then
|
if [ -z "$VPC_ID" ]; then
|
||||||
echo "Error during VPC creation."
|
echo "Error during VPC creation."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "VPC with ID $VPC_ID has been created."
|
echo "VPC with ID $VPC_ID has been created and tagged."
|
||||||
|
|
||||||
# Add tags to VPC
|
|
||||||
aws ec2 create-tags --resources "$VPC_ID" --tags Key=Name,Value="$VPC_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
|
|
||||||
echo "VPC is now correctly configured."
|
echo "VPC is now correctly configured."
|
||||||
|
|
||||||
|
|
||||||
# Create Subnet
|
# Create Subnet
|
||||||
echo "---------------------------------------"
|
echo "---------------------------------------"
|
||||||
echo "Creating Subnet..."
|
echo "Creating Subnet..."
|
||||||
SUBNET_ID=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.0.0.0/24 --availability-zone "$REGION"a --query 'Subnet.SubnetId' --output text)
|
SUBNET_ID=$(aws ec2 create-subnet \
|
||||||
|
--vpc-id "$VPC_ID" \
|
||||||
|
--cidr-block 10.0.0.0/24 \
|
||||||
|
--availability-zone "$REGION"a \
|
||||||
|
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value='"$VPC_NAME-Subnet"'}{Key=Owner,Value='"$OWNER"'}{Key=Project,Value='"$PROJECT"'}]' \
|
||||||
|
--query 'Subnet.SubnetId' \
|
||||||
|
--output text)
|
||||||
|
|
||||||
if [ -z "$SUBNET_ID" ]; then
|
if [ -z "$SUBNET_ID" ]; then
|
||||||
echo "Error during Subnet creation."
|
echo "Error during Subnet creation."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "Subnet with ID $SUBNET_ID has been created."
|
echo "Subnet with ID $SUBNET_ID has been created and tagged."
|
||||||
|
|
||||||
# Add tags to Subnet
|
|
||||||
aws ec2 create-tags --resources "$SUBNET_ID" --tags Key=Name,Value="$VPC_NAME-Subnet" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
|
|
||||||
echo "Subnet is now correctly configured."
|
echo "Subnet is now correctly configured."
|
||||||
|
|
||||||
|
|
||||||
# Create Elastic Container Registry (ECR)
|
# Create Elastic Container Registry (ECR)
|
||||||
echo "---------------------------------------"
|
echo "---------------------------------------"
|
||||||
echo "Creating Elastic Container Registry (ECR)..."
|
echo "Creating Elastic Container Registry (ECR)..."
|
||||||
ECR_REPO_JSON=$(aws ecr create-repository --repository-name "$ECR_NAME" --region "$REGION" --query 'repository' --output json)
|
ECR_REPO_JSON=$(aws ecr create-repository \
|
||||||
|
--repository-name "$ECR_NAME" \
|
||||||
|
--region "$REGION" \
|
||||||
|
--tag-specifications 'ResourceType=repository,Tags=[{Key=Name,Value='"$ECR_NAME"'}{Key=Owner,Value='"$OWNER"'}{Key=Project,Value='"$PROJECT"'}]' \
|
||||||
|
--query 'repository' \
|
||||||
|
--output json)
|
||||||
ECR_REPO_URI=$(echo "$ECR_REPO_JSON" | jq -r '.repositoryUri')
|
ECR_REPO_URI=$(echo "$ECR_REPO_JSON" | jq -r '.repositoryUri')
|
||||||
ECR_REPO_ARN=$(echo "$ECR_REPO_JSON" | jq -r '.repositoryArn')
|
ECR_REPO_ARN=$(echo "$ECR_REPO_JSON" | jq -r '.repositoryArn')
|
||||||
|
|
||||||
|
@ -72,15 +81,19 @@ if [ -z "$ECR_REPO_URI" ] || [ -z "$ECR_REPO_ARN" ]; then
|
||||||
fi
|
fi
|
||||||
echo "ECR repository created: $ECR_REPO_URI"
|
echo "ECR repository created: $ECR_REPO_URI"
|
||||||
|
|
||||||
# Add tags to Elastic Container Registry
|
|
||||||
aws ecr tag-resource --resource-arn "$ECR_REPO_ARN" --tags Key=Name,Value="$ECR_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
|
|
||||||
echo "Tags added to ECR repository."
|
echo "Tags added to ECR repository."
|
||||||
|
|
||||||
|
|
||||||
# Create Security Group
|
# Create Security Group
|
||||||
echo "---------------------------------------"
|
echo "---------------------------------------"
|
||||||
echo "Creating Security Group..."
|
echo "Creating Security Group..."
|
||||||
SECURITY_GROUP_ID=$(aws ec2 create-security-group --group-name "$SECURITY_GROUP_NAME" --description "Security group for devOps internship assesment" --vpc-id "$VPC_ID" --region "$REGION" --output text)
|
SECURITY_GROUP_ID=$(aws ec2 create-security-group \
|
||||||
|
--group-name "$SECURITY_GROUP_NAME" \
|
||||||
|
--description "Security group for devOps internship assesment" \
|
||||||
|
--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" \
|
||||||
|
--output text)
|
||||||
|
|
||||||
if [ -z "$SECURITY_GROUP_ID" ]; then
|
if [ -z "$SECURITY_GROUP_ID" ]; then
|
||||||
echo "Error during Security Group creation."
|
echo "Error during Security Group creation."
|
||||||
|
@ -88,12 +101,15 @@ if [ -z "$SECURITY_GROUP_ID" ]; then
|
||||||
fi
|
fi
|
||||||
echo "Security Group with ID $SECURITY_GROUP_ID has been created."
|
echo "Security Group with ID $SECURITY_GROUP_ID has been created."
|
||||||
|
|
||||||
# Add tags to Security Group
|
|
||||||
aws ec2 create-tags --resources "$SECURITY_GROUP_ID" --tags Key=Name,Value="$SECURITY_GROUP_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
|
|
||||||
echo "Security Group is now correctly configured."
|
echo "Security Group is now correctly configured."
|
||||||
|
|
||||||
# Allow inbound SSH access (port 22) from anywhere
|
# Allow inbound SSH access (port 22) from anywhere
|
||||||
aws ec2 authorize-security-group-ingress --group-id "$SECURITY_GROUP_ID" --protocol tcp --port 22 --cidr 0.0.0.0/0 --region "$REGION"
|
aws ec2 authorize-security-group-ingress \
|
||||||
|
--group-id "$SECURITY_GROUP_ID" \
|
||||||
|
--protocol tcp \
|
||||||
|
--port 22 \
|
||||||
|
--cidr 0.0.0.0/0 \
|
||||||
|
--region "$REGION"
|
||||||
echo "Inbound SSH access has been allowed for Security Group."
|
echo "Inbound SSH access has been allowed for Security Group."
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,7 +129,19 @@ newgrp docker
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
|
|
||||||
INSTANCE_ID=$(aws ec2 run-instances --image-id ami-0ac67a26390dc374d --count 1 --instance-type t3.micro --key-name "$KEY_PAIR_NAME" --security-group-ids "$SECURITY_GROUP_ID" --subnet-id "$SUBNET_ID" --region "$REGION" --user-data "$USER_DATA_SCRIPT" --iam-instance-profile Name=allow_ec2_ecr --query 'Instances[0].InstanceId' --output text)
|
INSTANCE_ID=$(aws ec2 run-instances \
|
||||||
|
--image-id ami-0ac67a26390dc374d \
|
||||||
|
--count 1 \
|
||||||
|
--instance-type t3.micro \
|
||||||
|
--key-name "$KEY_PAIR_NAME" \
|
||||||
|
--security-group-ids "$SECURITY_GROUP_ID" \
|
||||||
|
--subnet-id "$SUBNET_ID" \
|
||||||
|
--region "$REGION" \
|
||||||
|
--user-data "$USER_DATA_SCRIPT" \
|
||||||
|
--iam-instance-profile Name=allow_ec2_ecr \
|
||||||
|
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value='"$INSTANCE_NAME"'}{Key=Owner,Value='"$OWNER"'}{Key=Project,Value='"$PROJECT"'}]' \
|
||||||
|
--query 'Instances[0].InstanceId' \
|
||||||
|
--output text)
|
||||||
export INSTANCE_ID
|
export INSTANCE_ID
|
||||||
|
|
||||||
if [ -z "$INSTANCE_ID" ]; then
|
if [ -z "$INSTANCE_ID" ]; then
|
||||||
|
@ -122,8 +150,6 @@ if [ -z "$INSTANCE_ID" ]; then
|
||||||
fi
|
fi
|
||||||
echo "EC2 instance with ID $INSTANCE_ID has been created."
|
echo "EC2 instance with ID $INSTANCE_ID has been created."
|
||||||
|
|
||||||
# Add tags to EC2 instance
|
|
||||||
aws ec2 create-tags --resources "$INSTANCE_ID" --tags Key=Name,Value="$INSTANCE_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
|
|
||||||
echo "EC2 instance is now correctly configured."
|
echo "EC2 instance is now correctly configured."
|
||||||
|
|
||||||
|
|
||||||
|
@ -131,7 +157,11 @@ echo "EC2 instance is now correctly configured."
|
||||||
echo "---------------------------------------"
|
echo "---------------------------------------"
|
||||||
echo "Allocating and associating public IP address with EC2 instance..."
|
echo "Allocating and associating public IP address with EC2 instance..."
|
||||||
|
|
||||||
EIP_ALLOCATION_JSON=$(aws ec2 allocate-address --domain vpc --region "$REGION" --output json)
|
EIP_ALLOCATION_JSON=$(aws ec2 allocate-address \
|
||||||
|
--domain vpc \
|
||||||
|
--region "$REGION" \
|
||||||
|
--tag-specifications 'ResourceType=elastic-ip,Tags=[{Key=Name,Value='"$EIP_NAME"'}{Key=Owner,Value='"$OWNER"'}{Key=Project,Value='"$PROJECT"'}]' \
|
||||||
|
--output json)
|
||||||
|
|
||||||
# Check if the allocation was successful
|
# Check if the allocation was successful
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
|
@ -160,14 +190,6 @@ fi
|
||||||
|
|
||||||
echo "Public IP address has been associated with EC2 instance: $PUBLIC_IP"
|
echo "Public IP address has been associated with EC2 instance: $PUBLIC_IP"
|
||||||
|
|
||||||
# Add tags to the Elastic IP
|
|
||||||
aws ec2 create-tags --resources "$EIP_ALLOCATION_ID" --tags Key=Name,Value="$EIP_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
|
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error during tagging Elastic IP."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Tags added to Elastic IP."
|
echo "Tags added to Elastic IP."
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue