Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions create_ec2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash
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

if [[ "$instance_name" == "Shell-Script-EC2-Demo" ]]; then
echo "Same instance name is present, if you want change name of 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() {

if ! check_awscli; then
install_awscli || exit 1
fi


echo "Creating EC2 instance..."

# Specify the parameters for creating the EC2 instance
AMI_ID="ami-084568db4383264d4"
INSTANCE_TYPE="t2.micro"
KEY_NAME="shell-project-keypair"
SUBNET_ID="subnet-0e2ab1c36757c9201"
SECURITY_GROUP_IDS="sg-03bc7b302f02f0ecd" # 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 "$@"
21 changes: 15 additions & 6 deletions day04/create_ec2.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -euo pipefail
check_awscli() {
if ! command -v aws &> /dev/null; then
echo "AWS CLI is not installed. Please install it first." >&2
exit 1
return 1
fi
}

Expand Down Expand Up @@ -63,23 +63,32 @@ create_ec2_instance() {
exit 1
fi

if [[ "$instance_name" == "Shell-Script-EC2-Demo" ]]; then
echo "Same instance name is present, if you want change name of 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_awscli || exit 1
fi


echo "Creating EC2 instance..."

# Specify the parameters for creating the EC2 instance
AMI_ID=""
AMI_ID="ami-084568db4383264d4"
INSTANCE_TYPE="t2.micro"
KEY_NAME=""
SUBNET_ID=""
SECURITY_GROUP_IDS="" # Add your security group IDs separated by space
KEY_NAME="shell-project-keypair"
SUBNET_ID="subnet-0e2ab1c36757c9201"
SECURITY_GROUP_IDS="sg-03bc7b302f02f0ecd" # Add your security group IDs separated by space
INSTANCE_NAME="Shell-Script-EC2-Demo"

# Call the function to create the EC2 instance
Expand Down