From ecf449fb5feab3dd8c8e7476dbda54d7ff82a6c7 Mon Sep 17 00:00:00 2001 From: Mayur Pawar <139702403+mayurpwr7@users.noreply.github.com> Date: Mon, 26 Aug 2024 11:32:03 +0530 Subject: [PATCH 1/2] Update deploy_django_app.sh --- day03/deploy_django_app.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/day03/deploy_django_app.sh b/day03/deploy_django_app.sh index 4aad1f2..1e88eb1 100755 --- a/day03/deploy_django_app.sh +++ b/day03/deploy_django_app.sh @@ -8,7 +8,7 @@ code_clone() { if [ -d "django-notes-app" ]; then echo "The code directory already exists. Skipping clone." else - git clone https://github.com/LondheShubham153/django-notes-app.git || { + git clone https://github.com/mayurpwr7/django-notes-app.git || { echo "Failed to clone the code." return 1 } From 7543172e824c96179d334f7c9bafad307723cdba Mon Sep 17 00:00:00 2001 From: Mayur Pawar <139702403+mayurpwr7@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:06:34 +0530 Subject: [PATCH 2/2] Create my_own_instance_creation_file.sh --- day04/my_own_instance_creation_file.sh | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 day04/my_own_instance_creation_file.sh diff --git a/day04/my_own_instance_creation_file.sh b/day04/my_own_instance_creation_file.sh new file mode 100644 index 0000000..11cb484 --- /dev/null +++ b/day04/my_own_instance_creation_file.sh @@ -0,0 +1,94 @@ +#!/bin/bash +#Ec2 Instance +set -euo pipefail + +check_awscli() { + if ! command -v aws &> /dev/null; then + echo "AWS CLI is not installed. Please install it first." >&2 + return 1 + fi +} + +install_awscli() { + echo "Installing AWS CLI v2 on Linux..." + + # Download and install AWS CLI v2 + curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" + sudo apt-get install -y unzip &> /dev/null + unzip -q awscliv2.zip + sudo ./aws/install + + # Verify installation + aws --version + + # Clean up + rm -rf awscliv2.zip ./aws +} + +wait_for_instance() { + local instance_id="$1" + echo "Waiting for instance $instance_id to be in running state..." + + while true; do + state=$(aws ec2 describe-instances --instance-ids "$instance_id" --query 'Reservations[0].Instances[0].State.Name' --output text) + if [[ "$state" == "running" ]]; then + echo "Instance $instance_id is now running." + break + fi + sleep 10 + done +} + +create_ec2_instance() { + local ami_id="$1" + local instance_type="$2" + local key_name="$3" + local subnet_id="$4" + local security_group_ids="$5" + local instance_name="$6" + + # Run AWS CLI command to create EC2 instance + instance_id=$(aws ec2 run-instances \ + --image-id "$ami_id" \ + --instance-type "$instance_type" \ + --key-name "$key_name" \ + --subnet-id "$subnet_id" \ + --security-group-ids "$security_group_ids" \ + --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$instance_name}]" \ + --query 'Instances[0].InstanceId' \ + --output text + ) + + if [[ -z "$instance_id" ]]; then + echo "Failed to create EC2 instance." >&2 + exit 1 + fi + + echo "Instance $instance_id created successfully." + + # Wait for the instance to be in running state + wait_for_instance "$instance_id" +} + +main() { + check_awscli || install_awscli + if ! check_awscli ; then + install aws_cli || exit 1 + fi + echo "Creating EC2 instance..." + + # Specify the parameters for creating the EC2 instance + AMI_ID="ami-0522ab6e1ddcc7055" + INSTANCE_TYPE="t2.micro" + KEY_NAME="july" + SUBNET_ID="subnet-058699d5e4022d57d" + SECURITY_GROUP_IDS="sg-0e5cb3867ea4d1bea" # Add your security group IDs separated by space + INSTANCE_NAME="Shell-Script-EC2-Demo" + + # Call the function to create the EC2 instance + create_ec2_instance "$AMI_ID" "$INSTANCE_TYPE" "$KEY_NAME" "$SUBNET_ID" "$SECURITY_GROUP_IDS" "$INSTANCE_NAME" + + echo "EC2 instance creation completed." +} + +main "$@"