/
authorize.sh
executable file
·27 lines (23 loc) · 1.23 KB
/
authorize.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
#!/bin/bash
######################################################################
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. #
# SPDX-License-Identifier: MIT-0 #
######################################################################
. ec2.conf
# Create security group if it does not exist
echo ""
echo "Checking security group $EC2_SG_NAME ..."
aws ec2 describe-security-groups --query "SecurityGroups[*].{GroupId:GroupId,GroupName:GroupName,VpcId:VpcId}" --output table | grep $EC2_SG_NAME > /dev/null
if [ "$?" == "0" ]; then
echo "Security group $EC2_SG_NAME already exists"
EC2_SG_ID=$(aws ec2 describe-security-groups --query "SecurityGroups[*].{GroupId:GroupId,GroupName:GroupName,VpcId:VpcId}" --output table | grep $EC2_SG_NAME | cut -d '|' -f 2 | cut -d ' ' -f 3)
IP=$(curl -s https://checkip.amazonaws.com)
echo "Authorizing connections from client IP $IP ..."
aws ec2 authorize-security-group-ingress --group-id $EC2_SG_ID --protocol tcp --port 22 --cidr ${IP}/32
if [ "$?" == "0" ]; then
echo "Authorized."
fi
else
echo "Security group $EC2_SG_NAME not found ..."
echo "Cannot authorize."
fi