Skip to content

Commit de783db

Browse files
authored
Update aws-ssm-ssh-proxy-command.sh
1 parent 3936bff commit de783db

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

aws-ssm-ssh-proxy-command.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@ set -eu
77
#
88
################################################################################
99

10+
REGION_SEPARATOR='--'
11+
INSTANCE_ID_PATTERN='^m?i-[0-9a-f]{8,17}$'
12+
13+
instance_name="$1"
14+
ssh_user="$2"
15+
ssh_port="$3"
16+
ssh_public_key_path="$4"
17+
1018
getInstanceId() {
1119
local instance_name="$1"
1220
local instance_id=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=${instance_name}" --query "Reservations[].Instances[?State.Name == 'running'].InstanceId" --output text)
1321

1422
echo "${instance_id}"
1523
}
1624

17-
instance_name="$1"
18-
ssh_user="$2"
19-
ssh_port="$3"
20-
ssh_public_key_path="$4"
25+
if [[ $instance_name =~ $REGION_SEPARATOR ]]; then
26+
export AWS_REGION="${instance_name##*"${REGION_SEPARATOR}"}"
27+
instance_name="${instance_name%%"$REGION_SEPARATOR"*}"
28+
fi
2129

22-
ec2InstanceIdPattern='^m?i-[0-9a-f]{8,17}$'
23-
if [[ $instance_name =~ $ec2InstanceIdPattern ]]; then
30+
if [[ $instance_name =~ $INSTANCE_ID_PATTERN ]]; then
2431
instance_id=$instance_name
2532
else
2633
instance_id=$( getInstanceId "$instance_name" )
@@ -33,13 +40,6 @@ else
3340
fi
3441
fi
3542

36-
REGION_SEPARATOR='--'
37-
if echo "$instance_id" | grep -q -e "${REGION_SEPARATOR}"
38-
then
39-
export AWS_REGION="${instance_id##*"${REGION_SEPARATOR}"}"
40-
instance_id="${instance_id%%"$REGION_SEPARATOR"*}"
41-
fi
42-
4343
>/dev/stderr echo "Add public key ${ssh_public_key_path} for ${ssh_user} at instance ${instance_id} for 10 seconds"
4444
ssh_public_key="$(cat "${ssh_public_key_path}")"
4545
aws ssm send-command \

0 commit comments

Comments
 (0)