File tree Expand file tree Collapse file tree 1 file changed +13
-13
lines changed Expand file tree Collapse file tree 1 file changed +13
-13
lines changed Original file line number Diff line number Diff line change @@ -7,20 +7,27 @@ set -eu
7
7
#
8
8
# ###############################################################################
9
9
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
+
10
18
getInstanceId () {
11
19
local instance_name=" $1 "
12
20
local instance_id=$( aws ec2 describe-instances --filters " Name=tag:Name,Values=${instance_name} " --query " Reservations[].Instances[?State.Name == 'running'].InstanceId" --output text)
13
21
14
22
echo " ${instance_id} "
15
23
}
16
24
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
21
29
22
- ec2InstanceIdPattern=' ^m?i-[0-9a-f]{8,17}$'
23
- if [[ $instance_name =~ $ec2InstanceIdPattern ]]; then
30
+ if [[ $instance_name =~ $INSTANCE_ID_PATTERN ]]; then
24
31
instance_id=$instance_name
25
32
else
26
33
instance_id=$( getInstanceId " $instance_name " )
33
40
fi
34
41
fi
35
42
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
-
43
43
> /dev/stderr echo " Add public key ${ssh_public_key_path} for ${ssh_user} at instance ${instance_id} for 10 seconds"
44
44
ssh_public_key=" $( cat " ${ssh_public_key_path} " ) "
45
45
aws ssm send-command \
You can’t perform that action at this time.
0 commit comments