-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_to_ec2.sh
executable file
·47 lines (33 loc) · 1.43 KB
/
connect_to_ec2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
if ! type "aws" > /dev/null; then
echo "aws cli is not installed. Please install and configure it first."
exit 1
fi
echo "Fetching running EC2 instances.."
if [ -z "$1" ]; then
echo "Usage: $0 <aws-profile>."
exit 1
fi
profile_name=$1
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].{ID:InstanceId,Type:InstanceType,State:State.Name}" \
--profile "$profile_name" --output table
#read user input for instance id
read -p "Enter the instance id: " instance_id
instances=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].[InstanceId,PublicDnsName]" \
--profile "$profile_name" --output text)
instance_address=$(echo "$instances" | awk -v id="$instance_id" '$1 == id { print $2 }')
if [ -z "$instance_address" ]; then
"Cannot find instance address for given instance ID: $instance_id"
exit 1
fi
echo "Connecting to Instance $instance_id at address: $instance_address"
#launch the script inside directory where ssh key is stored
ssh -o IdentitiesOnly=yes -i "my-web-server.pem" ubuntu@${instance_address} "nginx -v; docker -v"
#aws ec2-instance-connect ssh --instance-id "$instance_id"
if [ $? -ne 0 ]; then
echo "Error occurred when connecting to EC2 instance. Check your SSH key, instance ID, and network settings."
exit 1
fi
echo "Verification complete."