-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathec2-sg-create.sh
executable file
·62 lines (53 loc) · 1.89 KB
/
ec2-sg-create.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
######################################################################
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. #
# SPDX-License-Identifier: Apache-2.0 #
######################################################################
source ./ec2.conf
function usage() {
echo ""
echo "Usage: $0 <security_group_name> [vpc_id] [security_group_desc]"
echo ""
echo " security_group_name - The name of the security group to create (required)"
echo " vpc_id - The vpc_id where to create the security group (required)."
echo " If not specified, will try to retrieve from ec2.conf file's"
echo " current_vpc or EC2_SUBNET_ID setting."
echo " security_group_desc - Description of the security group (optional)"
echo ""
}
if [ "$1" == "" ]; then
usage
exit 1
fi
DESCRIPTION="$3"
if [ "$DESCRIPTION" == "" ]; then
DESCRIPTION="Security group $1 created by aws-do-cli"
fi
VPC_ID="$2"
if [ "$VPC_ID" == "" ]; then
VPC_ID=$current_vpc
fi
if [ "$VPC_ID" == "" ]; then
# Retrieve VPC from SubnetID
if [ "$EC2_SUBNET_ID" == "" ]; then
usage
else
VPC_ID=$(aws ec2 describe-subnets --subnet-ids $EC2_SUBNET_ID --query Subnets[*].VpcId --output text)
fi
fi
CMD="aws ec2 create-security-group --group-name \"$1\" --description \"$DESCRIPTION\" --vpc-id \"$VPC_ID\" --query GroupId --output text"
echo "$CMD"
GROUP_ID=$(eval "$CMD")
# Add rule to allow ssh from current host if public ip is assigned
if [ "$EC2_ASSIGN_PUBLIC_IP" == "true" ]; then
echo ""
echo "Adding ssh rule to security group $GROUP_ID ..."
IP=$(curl https://checkip.amazonaws.com)
echo "Allow TCP to port 22 from $IP "
CMD="aws ec2 authorize-security-group-ingress --group-id $GROUP_ID --protocol tcp --port 22 --cidr ${IP}/32"
echo "$CMD"
eval "$CMD"
fi
echo ""
echo "Created SecurityGroup ID: $GROUP_ID"
echo ""