-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bash_aliases_aws
1862 lines (1805 loc) · 96.7 KB
/
.bash_aliases_aws
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# file: ~/.bash_aliases_aws - sourced by ~/.bashrc_aliases (if enabled)
# these are functions/aliases specific to AWS
# shellcheck disable=SC1090,SC2029,SC2034,SC2139,SC2142,SC1117,SC2155,SC2086,SC2046
# -------------------- global variables --------------------
AWS_BA_FILE=".bash_aliases_aws"
# if interactive shell - display message
[ -n "$PS1" ] && echo -en "${GRN}$AWS_BA_FILE${NRM} "
# -------------------- define functions --------------------
function _get_aws_profile_setting {
# returns value of specified setting from a profile in AWS config
# usage: _get_aws_profile_setting profile setting
local _AWS_CFG=$HOME/.aws/config
local _PROFILE=$1
local _SETTING=$2
local _def
local _val
_def=$(awk -v adp="${_PROFILE}]" -v key="$_SETTING" '$2==adp {pfound="true"; next}; (pfound=="true" && $1==key) {print $0; exit}; (pfound=="true" && $1~/profile/) {exit}' "$_AWS_CFG")
_val=$(cut -d'=' -f2 <<< "$_def" | cut -c 2-)
[[ -n "$_val" ]] && echo "$_val"
}
function _get_saml_setting {
# returns value of specified setting from a profile in AWS config
# usage: _get_saml_setting profile setting
local _SAML_CFG=$HOME/.aws/.saml2aws
local _PROFILE=$1
local _SETTING=$2
local _def
local _val
_def=$(awk -v adp="[${_PROFILE}]" -v key="$_SETTING" '$1==adp {pfound="true"; next}; (pfound=="true" && $1==key) {print $0; exit}; (pfound=="true" && $1~/^\[/) {exit}' "$_SAML_CFG")
_val=$(cut -d'=' -f2 <<< "$_def" | cut -c 2-)
[[ -n "$_val" ]] && echo "$_val"
}
function _set_aws_prompt_colors {
# sets AWS environment related prompt attribute colors depending on env
local _ENV=$1
if [[ "$COLOR_PROMPT" == "yes" ]]; then
case $_ENV in
dev) PS_AWS_COL="$PCYN";;
devops) PS_AWS_COL="$PBBG";;
mine) PS_AWS_COL="$PGBG";;
mixed) PS_AWS_COL="$PYLW";;
prod) PS_AWS_COL="$PRBG";;
qa) PS_AWS_COL="$PMBG";;
test) PS_AWS_COL="$PCYN";;
*) PS_AWS_COL="$PNRM";;
esac
case $_ENV in
dev|devops|mine|mixed|prod|qa|test)
PS_AWS="[${PS_AWS_COL}${AWS_DEFAULT_PROFILE}${PNRM}:${PWHTB}${AWS_DEFAULT_REGION:-n/a}${PNRM}] ";;
*)
PS_AWS="$PNRM";;
esac
fi
}
function _show_aws_environment {
# show AWS environment [variables]
echo -n "--- AWS Environment "
[[ -n "$AWS_DEFAULT_PROFILE" ]] || { [[ -n "$AWS_ACCESS_KEY_ID" ]] && [[ -n "$AWS_SECRET_ACCESS_KEY" ]]; } && echo "Settings ---" || echo "(NOT set) ---"
echo "AWS_ENVIRONMENT = ${AWS_ENVIRONMENT:-N/A}"
echo "AWS_DEFAULT_PROFILE = ${AWS_DEFAULT_PROFILE:-N/A}"
# obfuscate the KEYs with some *'s
echo "AWS_ACCESS_KEY_ID = ${AWS_ACCESS_KEY_ID:-N/A}" | sed 's:[F-HO-QT-V3-8]:*:g'
echo "AWS_SECRET_ACCESS_KEY = ${AWS_SECRET_ACCESS_KEY:-N/A}" | sed 's:[d-np-zF-HO-QU-V4-9+]:*:g'
echo "AWS_DEFAULT_REGION = ${AWS_DEFAULT_REGION:-N/A}"
}
function _unset_aws_environment {
# unsets AWS environment related variables
unset AWS_ACCESS_KEY_ID
unset AWS_ACCOUNT_ID
unset AWS_ACCOUNT
unset AWS_CREDENTIAL_EXPIRATION
unset AWS_DEFAULT_PROFILE
unset AWS_DEFAULT_REGION
unset AWS_ENVIRONMENT
unset AWS_SECRET_ACCESS_KEY
unset AWS_SECURITY_TOKEN
unset AWS_SESSION_TOKEN
unset AWS_STS_EXPIRES_TS
unset SAML2AWS_PROFILE
echo "AWS environment has been unset"
}
function _verify_aws_profile {
# verify profile exists in AWS configs
local _AWS_CFG=$HOME/.aws/config
local _PROFILE=$1
local _AWS_PROFILES=$(\grep '^\[profile' "$_AWS_CFG" | awk '{print $2}' | tr -s ']\n' ' ')
local _VALID_PROFILES=$(tr ' ' ':' <<< "${_AWS_PROFILES}unset")
if [[ ! $_VALID_PROFILES =~ (^|:)$_PROFILE(:|$) ]]; then
echo -e "WTF? Try again... Only these profiles exist (or use 'unset'):\n " "$_AWS_PROFILES"
return 2
fi
}
function _verify_saml_profile {
# verify profile exists in AWS configs
local _SAML_CFG=$HOME/.aws/.saml2aws
local _PROFILE=$1
local _AWS_PROFILES=$(\grep '^\[' "$_SAML_CFG" | awk -F'[][]' '{print $2}' | tr -s '\n' ' ')
local _VALID_PROFILES=$(tr ' ' ':' <<< "${_AWS_PROFILES}unset")
if [[ ! $_VALID_PROFILES =~ (^|:)$_PROFILE(:|$) ]]; then
echo -e "WTF? Try again... Only these profiles exist (or use 'unset'):\n " "$_AWS_PROFILES"
return 2
fi
}
function awsacct {
# get AWS account name by ID or ID by name
local _AWS_ACCTS="$HOME/.aws/accounts.json"
local _name_or_id=$1
if [[ -z "$_name_or_id" ]] || [[ "$_name_or_id" == "all" ]]; then
jq -r '.[] | .name + ": " + .id' "$_AWS_ACCTS"
fi
if \grep -Ewq '[0-9]+' <<< "$_name_or_id"; then
local _aws_acct_name=$(jq -r '.[] | select(.id=="'"$_name_or_id"'") | .name' "$_AWS_ACCTS")
else
local _aws_acct_id=$(jq -r '.[] | select(.name=="'"$_name_or_id"'") | .id' "$_AWS_ACCTS")
fi
if [[ -n "$_aws_acct_name" ]] || [[ -n "$_aws_acct_id" ]]; then
echo -n "${_aws_acct_name}${_aws_acct_id}" | pbcopy
pbpaste
fi
}
function awsconsole {
# fire up AWS Console if AWS Environment Variables are all set
local _CONSOLE_LOGIN_SCRIPT="$HOME/repos/pataraco/scripts/aws/aws_sso_console_login.py"
local _CONSOLE_DEST="$1"
local _aws_signin_url
if [[ -z "$AWS_ACCESS_KEY_ID" ]] || [[ -z "$AWS_SECRET_ACCESS_KEY" ]] || [[ -z "$AWS_SESSION_TOKEN" ]]; then
echo "can't log in to console - AWS environment not set up"
return
fi
if [[ -x "$_CONSOLE_LOGIN_SCRIPT" ]]; then
_aws_signin_url=$($_CONSOLE_LOGIN_SCRIPT)
else
echo "can't log in to console - console login script doesn't exist: $_CONSOLE_LOGIN_SCRIPT"
return
fi
if [[ -n "$_aws_signin_url" ]]; then
case $_CONSOLE_DEST in
api) _CONSOLE_DEST="apigateway";;
cdn) _CONSOLE_DEST="cloudfront";;
cfn) _CONSOLE_DEST="cloudformation";;
ddb) _CONSOLE_DEST="dynamodb";;
eb) _CONSOLE_DEST="elasticbeanstalk";;
secrets) _CONSOLE_DEST="secretsmanager";;
ssm) _CONSOLE_DEST="systems-manager/parameters";;
sso) _CONSOLE_DEST="singlesignon";;
esac
open -n -a "Google Chrome" --args "--new-window" "--profile-directory=${AWS_DEFAULT_PROFILE}" "${_aws_signin_url}${_CONSOLE_DEST}"
else
echo "can't log in to console - could not generate signin URL"
return 3
fi
return
}
function assh {
# ssh to an IP found by 'aws ec2 describe-instances'
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWS_EC2_DI_CMD="aws ec2 describe-instances"
local _USAGE="usage: \
assh [-h] [-e|-u] [-p PROFILE] [-r REGION] HOSTNAME [COMMAND]
-h - help (show this message)
-e - ssh as 'ec2-user'
-u - ssh as 'ubuntu'
-p PROFILE - AWS profile to use (aka --profile option)
-r REGION - AWS region to use (aka --region option) [default: $_DEFAULT_REGION]
HOSTNAME - hostname pattern to search for and get the private IP of
COMMAND - optional command to run on the host and return"
local _host=""
local _max_items="--max-items 40"
local _region="$_DEFAULT_REGION"
[ "$1" == "-h" ] && { echo "$_USAGE"; return; }
[ "$1" == "-e" ] && { local _user="ec2-user@"; shift 1; }
[ "$1" == "-u" ] && { local _user="ubuntu@"; shift 1; }
[ "$1" == "-p" ] && { local _profile="--profile=$2"; shift 2; }
[ "$1" == "-r" ] && { local _region="$2"; shift 2; }
local _tag_name=$1
shift
local _command=$*
[ -z "$_tag_name" ] && { echo "$_USAGE"; return; }
local _filters="--filters Name=tag:Name,Values=*$_tag_name* Name=instance-state-name,Values=running"
local _query="--query \"Reservations[].Instances[].[Tags[?Key=='Name'].Value|[0],PrivateIpAddress]\""
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWS_EC2_DI_CMD $_profile --region=$_region $_max_items $_filters $_query --output text"
_host=$(eval "$_AWS_EC2_DI_CMD $_profile --region=$_region $_max_items $_filters $_query --output text")
if [[ "$(wc -w <<< "$_host")" -gt 2 ]]; then
echo "there are more than one host that matches that name tag,"
echo "please be more specific:"
echo
awk '{print "\t"$1}' <<< "$_host"
elif [[ "$(wc -w <<< "$_host")" -lt 2 ]]; then
echo "no matching hosts found with that name tag,"
echo "please try again"
else
_tag_name=$(awk '{print $1}' <<< "$_host")
_host=$(awk '{print $2}' <<< "$_host")
# echo -e " ${CYN}< $_tag_name > ( $_host ) [ $_command ]${NRM}"
if [[ -n "$_command" ]]; then
echo -e " ${BLU}<${GRN}$_tag_name${BLU}> (${YLW}$_host${BLU}) [${CYN}$_command${BLU}]${NRM}"
else
echo -e " ${BLU}<${GRN}$_tag_name${BLU}> (${YLW}$_host${BLU})${NRM}"
fi
ssh "$_user$_host" "$_command"
fi
}
function awsar {
# list all AWS regions available to me
[ -n "$AWS_DEBUG" ] && echo "debug: aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName"
aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName
}
function awsassrole {
# assume an AWS role
local _USAGE="usage: awsassrole IAM_ROLE_ARN|IAM_ROLE_NAME"
local _DURATION=3600 # seconds
local _iam_arg=$1
local _iam_role_arn
local _iam_role_arn_default="arn:aws:iam::${AWS_ACCOUNT_ID}:role/$_iam_arg"
if [[ ! "$_iam_arg" =~ ^arn:aws:iam::.*:role/.* ]]; then
echo "this does not resemble an AWS IAM ARN: '$_iam_arg'"
echo "using this one: '$_iam_role_arn_default'"
_iam_role_arn=$_iam_role_arn_default
else
_iam_role_arn=$_iam_arg
fi
local _iam_role_name=${_iam_role_arn#*/}
local _iam_role_aws_acct_id=$(cut -d':' -f5 <<< "$_iam_role_arn")
local _iam_role_aws_acct_env=$(awsacct "$_iam_role_aws_acct_id")
local _session
if [[ -n "$_iam_role_arn" ]]; then
[ -n "$AWS_DEBUG" ] && echo "debug: aws sts assume-role --region us-east-1 --role-arn $_iam_role_arn --role-session-name $_iam_role_name --duration-seconds $_DURATION"
if _session=$(aws sts assume-role --region us-east-1 --role-arn "$_iam_role_arn" --role-session-name "$_iam_role_name" --duration-seconds "$_DURATION"); then
_unset_aws_environment > /dev/null
export AWS_ACCESS_KEY_ID=$(jq -r .Credentials.AccessKeyId <<< "$_session")
export AWS_SECRET_ACCESS_KEY=$(jq -r .Credentials.SecretAccessKey <<< "$_session")
export AWS_SESSION_TOKEN=$(jq -r .Credentials.SessionToken <<< "$_session")
export AWS_DEFAULT_PROFILE="role:$_iam_role_name"
_set_aws_prompt_colors "$_iam_role_aws_acct_env"
unset AWS_DEFAULT_PROFILE
echo "Assumed role: $_iam_role_name [$_iam_role_aws_acct_env]"
else
echo "Could not assume role: $_iam_role_name"
return 1
fi
else
echo "error: did not provide an AWS IAM ARN"
echo "$_USAGE"
fi
}
function awsdr {
# AWS Set Default Region
local _area
local _country
local _region=$1
if [[ -n "$_region" ]]; then
if [[ "$(wc -c <<< "$_region")" -eq 4 ]]; then
case ${_region:0:1} in
a) _country="ap";;
c) _country="ca";;
e) _country="eu";;
f) _country="af";;
m) _country="me";;
s) _country="sa";;
u) _country="us";;
*) echo "unknown country (${_region:0:1}): exiting..."; return;;
esac
case ${_region:1:1} in
c) _area="central";;
e) _area="east";;
n) _area="north";;
s) _area="south";;
S) _area="southeast";;
w) _area="west";;
*) echo "unknown area (${_region:1:1}): exiting..."; return;;
esac
export AWS_DEFAULT_REGION="${_country}-${_area}-${_region:2:1}"
else
if [[ "$_region" == "unset" ]]; then
unset AWS_DEFAULT_REGION
else
export AWS_DEFAULT_REGION="$_region"
fi
fi
fi
echo "AWS_DEFAULT_REGION = ${AWS_DEFAULT_REGION:-N/A}"
}
function awssnsep {
# AWS SNS list platform application endpoints
local _USAGE="usage: awssnsep APPLICATION [REGION] # can use 'all'"
local _app=$1
[ -z "$_app" ] && { echo "$_USAGE"; return; }
local _region=$2
[ -n "$_region" ] && _region="--region $_region"
[ -n "$AWS_DEBUG" ] && echo "debug: aws sns list-platform-applications $_region"
if [[ "$_app" == "all" ]]; then
echo "all platform applications found:"
aws sns list-platform-applications "$_region" | \grep PlatformApplicationArn | awk '{print $2}' | tr -d '"'
return
fi
local _app_arn
_app_arn=$(aws sns list-platform-applications "$_region" | \grep "arn:.*$_app" | awk '{print $2}' | tr -d '"')
[ -z "$_app_arn" ] && { echo "none found"; return; }
local _noa
_noa=$(echo "$_app_arn" | wc -l)
if [[ "$_noa" -gt 1 ]]; then
echo "found more than one app, please be more specific:"
echo "$_app_arn" | \grep "$_app"
return
fi
[ -n "$AWS_DEBUG" ] && echo "debug: aws sns list-endpoints-by-platform-application $_region --platform-application-arn $_app_arn"
local _app_eps
_app_eps=$(aws sns list-endpoints-by-platform-application "$_region" --platform-application-arn "$_app_arn")
local _enabled
_enabled=$(echo "$_app_eps" | jq .Endpoints[].Attributes.Enabled | tr -d '"')
local _token
_token=$(echo "$_app_eps" | jq .Endpoints[].Attributes.Token | tr -d '"')
echo "$_app | $_app_arn | $_enabled | $_token"
}
function awsasgcp {
# usage:
# awsasgcp
# -r|--resume or -s|--suspend
# [--region REGION] [--dry-run] [AutoScalingGroupName|RegEx]
# suspend/resume ALL AWS AutoScaling processes
# optional: AutoScalingGroupName or RegEx
# only for a specified autoscaling group name or those matching a reg-ex
# defaults to "running" (i.e. run the command)
# must use "--dry-run" option to NOT perform
local _AWS_CMD
local _JQ_CMD
local _USAGE
local _dryrun=running
local _pc_cmd
local _region
_USAGE="usage: awsasgcp -r|--resume or -s|--suspend [--region REGION] [--dry-run] [AutoScalingGroupName|RegEx]"
_AWS_CMD=$(command -v aws 2> /dev/null) || { echo "'aws' needed to run this function"; exit 3; }
_JQ_CMD=$(command -v jq 2> /dev/null) || { echo "'jq' needed to run this function"; exit 3; }
while true; do
case "$1" in
-r|--resume) _pc_cmd=resume-processes ; shift;;
-s|--suspend) _pc_cmd=suspend-processes; shift;;
--dry-run) _dryrun=dry-run ; shift;;
--region) _region="--region $2" ; shift 2;;
*) break;;
esac
done
[ -z "$_pc_cmd" ] && { echo "$_USAGE"; return; }
local _asgn_pattern=$*
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWS_CMD $_region autoscaling describe-auto-scaling-groups | $_JQ_CMD -r .AutoScalingGroups[].AutoScalingGroupName | \grep \"$_asgn_pattern\""
asg_names=$($_AWS_CMD "$_region" autoscaling describe-auto-scaling-groups | $_JQ_CMD -r .AutoScalingGroups[].AutoScalingGroupName | \grep "$_asgn_pattern")
if [[ -n "$asg_names" ]]; then
for asg_name in $asg_names; do
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWS_CMD $_region autoscaling $_pc_cmd --auto-scaling-group-name $asg_name"
echo "$_dryrun: $(basename "$_AWS_CMD") $_region autoscaling $_pc_cmd --auto-scaling-group-name $asg_name"
if [[ $_dryrun == "running" ]]; then
$_AWS_CMD "$_region" autoscaling $_pc_cmd --auto-scaling-group-name "$asg_name"
fi
done
else
echo "no matching AWS Auto Scaling Group names found"
fi
}
function awsci {
# reboot, start, stop or terminate an instance
local _AWS_CMD
_AWS_CMD=$(command -v aws) # doesn't work if "aws" is an alias
[ -z "$_AWS_CMD" ] && { echo "error: aws command not found"; return 2; }
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _USAGE="usage: awsci [-r REGION] reboot|start|stop|terminate INSTANCE_ID|INSTANCE_NAME # default region: $_DEFAULT_REGION"
local _region=$1
[ "$_region" == "-r" ] && { _region=$2; shift 2; } || _region=$_DEFAULT_REGION
local _CONTROL_CMD=$1
local _INSTANCE_PATTERN=$2
[ -z "$_CONTROL_CMD" ] && { echo "error: did not specify 'reboot', 'start', 'stop' or 'terminate'"; echo "$_USAGE"; return; }
[ -z "$_INSTANCE_PATTERN" ] && { echo "error: did not specify an instance name or ID"; echo "$_USAGE"; return; }
local _instance_id
local _instance_name
_instance_name=$($_AWS_CMD ec2 describe-instances --region "$_region" --instance-ids "$_INSTANCE_PATTERN" --query "Reservations[].Instances[].[Tags[?Key=='Name'].Value]" --output text 2> /dev/null)
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
_instance_id=$_INSTANCE_PATTERN
else
_instance_id=$($_AWS_CMD ec2 describe-instances --region "$_region" --filters "Name=tag:Name,Values=*${_INSTANCE_PATTERN}*" --output json | jq -r .Reservations[].Instances[].InstanceId 2> /dev/null)
_instance_name=$($_AWS_CMD ec2 describe-instances --region "$_region" --instance-ids "$_instance_id" --query "Reservations[].Instances[].[Tags[?Key=='Name'].Value]" --output text 2> /dev/null)
fi
[ -z "$_instance_name" ] && { echo "note: did not find an instance with ID: $_instance_id"; return; }
[ -z "$_instance_id" ] && { echo "note: did not find instance named: $_instance_name"; return; }
local _no_of_ids
_no_of_ids=$(echo "$_instance_id" | wc -l)
[ "$_no_of_ids" -gt 1 ] && { echo "note: found more than one instance - please be more specific"; return; }
local _instance_state
_instance_state=$($_AWS_CMD ec2 describe-instances --region "$_region" --instance-ids "$_instance_id" --query "Reservations[].Instances[].State.Name" --output text)
local _aws_ec2_cmd
case $_CONTROL_CMD in
reboot)
[ "$_instance_state" != "running" ] && { echo "$_instance_name ($_instance_id) is NOT running"; return; }
_aws_ec2_cmd=reboot-instances ;;
start)
[ "$_instance_state" == "running" ] && { echo "$_instance_name ($_instance_id) is already running"; return; }
_aws_ec2_cmd=start-instances ;;
stop)
[ "$_instance_state" == "stopped" ] && { echo "$_instance_name ($_instance_id) is already stopped"; return; }
_aws_ec2_cmd=stop-instances ;;
terminate)
_aws_ec2_cmd=terminate-instances ;;
*)
echo "unknown option: exiting..."; echo "$_USAGE"; return;;
esac
local _ans
echo "Instance: $_instance_name ($_instance_id) is $_instance_state"
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWS_CMD ec2 $_aws_ec2_cmd --region $_region --instance-ids $_instance_id"
read -rp "Are you sure that you want to '${_CONTROL_CMD}' it [yes/no]? " _ans
if [[ "$_ans" == "yes" ]] || [[ "$_ans" == "YES" ]]; then
$_AWS_CMD ec2 $_aws_ec2_cmd --region "$_region" --instance-ids "$_instance_id"
else
echo "Did not enter 'yes'; NOT going to ${_CONTROL_CMD} the instance"
fi
}
function awsda {
# some 'aws ec2 describe-addresses' hacks
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWS_EC2_DA_CMD="aws ec2 describe-addresses"
local _USAGE="usage: \
awsda [OPTIONS]
-n NAME - filter results by this Address Name
-t KEY=VAL - filter results by this tag (key=val)
-m MAX - maximum number of items to display
-r REGION - Region to query (default: $_DEFAULT_REGION, 'all' for all)
+alid - show Allocation ID
+asid - show Association ID
+niid - show Network Interface ID
+t KEY - show tag (KEY)
-h - help (show this message)
default display:
Addy name | EIP | Instance | Priv IP"
local _default_queries="Tags[?Key=='Name'].Value|[0],PublicIp,InstanceId,PrivateIpAddress"
local _filters=""
local _max_items=""
local _pem_file="" # PEM file used to decrypt the passwords
local _more_qs=""
local _queries="Tags[?Key=='Name'].Value|[0],PublicIp,InstanceId,PrivateIpAddress"
local _query="Addresses[]"
local _region="$_DEFAULT_REGION"
while [[ $# -gt 0 ]]; do
case $1 in
-n) _filters="Name=tag:Name,Values=*$2* $_filters" ; shift 2;;
-t) _filters="\"Name=tag:${2%%=*},Values=*${2##*=}*\" $_filters" ; shift 2;;
-m) _max_items="--max-items $2" ; shift 2;;
-r) _region=$2 ; shift 2;;
+alid) _more_qs="$_more_qs${_more_qs:+,}AllocationId" ; shift;;
+asid) _more_qs="$_more_qs${_more_qs:+,}AssociationId" ; shift;;
+niid) _more_qs="$_more_qs${_more_qs:+,}NetworkInterfaceId" ; shift;;
+t) _more_qs="$_more_qs${_more_qs:+,}Tags[?Key=='$2'].Value|[0]" ; shift 2;;
-h|*) echo "$_USAGE"; return;;
esac
done
[ -n "$_filters" ] && _filters="--filters ${_filters% }"
[ -n "$_more_qs" ] && _query="$_query.[$_queries,${_more_qs%,}]" || _query="$_query.[$_default_queries]"
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWS_EC2_DA_CMD --region=$_region $_max_items $_filters --query \"$_query\" --output table"
if [[ "$_region" == "all" ]]; then
local _ALL_REGIONS
_ALL_REGIONS=$(aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName)
for _region in $_ALL_REGIONS; do
eval "$_AWS_EC2_DA_CMD --region=$_region $_max_items $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeAddresses' | sort | sed 's/^| //;s/ |$/|'"$_region"'/' | sed -E 's/ +\| +/\|/g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
done
else
eval "$_AWS_EC2_DA_CMD --region=$_region $_max_items $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeAddresses' | sort | sed 's/^| //;s/ |$//' | sed -E 's/ +\| +/\|/g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
}
function awsdami {
# some 'aws ec2 describe-images' hacks
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWSEC2DAMI_CMD="aws ec2 describe-images"
local _USAGE="usage: \
awsdami [OPTIONS]
-a ARCH - Architecture (e.g. i386, x86_64)
-ht HYPE_TYPE - Hypervisor Type (e.g. ovm, xen)
-i ID - Image ID (RegEx)
-it IMG_TYPE - Image Type (e.g. machine, kernel, ramdisk)
-nt NAME TAG - Image's Name Tag (RegEx)
-n NAME - Image Name when created (RegEx)
-o OWNERS - Owners (e.g. amazon, aws-marketplace, AWS ID. default: self)
-p PROJECT - Project
-r REGION - Region to query (default: $_DEFAULT_REGION, 'all' for all)
-s STATE - State
-v VIRT_TYPE - Virtualization Type (e.g. paravirtual, hvm)
-vs VOL_SIZE - Volume Size (in GiB)
-vt VOL_TYPE - Volume Type (e.g. gp2, io1, st1, sc1, standard)
+a - show Architecture
+cd - show Creation Date
+ht - show Hypervisor Type
+i - show Image ID
+it - show Image Type
+o - show Owner ID
+ps - show Public Status
+rn - show Root Device Name
+rt - show Root Device Type
+s - show State
+v - show Virtualization Type
+vs - show Volume Size
+vt - show Volume Type
-h - help (show this message)
default display:
Name Tag | Image Name | Image ID | State | Region"
#local _owners="self"
local _owners=""
local _region="$_DEFAULT_REGION"
local _filters=""
local _queries="Tags[?Key=='Name'].Value|[0],Name"
local _default_queries="Tags[?Key=='Name'].Value|[0],Name,ImageId,State"
local _more_qs=""
local _query="Images[]"
while [[ $# -gt 0 ]]; do
case $1 in
-a) _filters="Name=architecture,Values=*$2* $_filters" ; shift 2;;
-ht) _filters="Name=hypervisor,Values=*$2* $_filters" ; shift 2;;
-i) _filters="Name=image-id,Values=*$2* $_filters" ; shift 2;;
-it) _filters="Name=image-type,Values=*$2* $_filters" ; shift 2;;
-nt) _filters="Name=tag:Name,Values=*$2* $_filters" ; shift 2;;
-n) _filters="Name=name,Values=*$2* $_filters" ; shift 2;;
-o) _owners="--owners $2" ; shift 2;;
-p) _filters="Name=tag:Project,Values=*$2* $_filters" ; shift 2;;
-s) _filters="Name=state,Values=*$2* $_filters" ; shift 2;;
-v) _filters="Name=virtualization-type,Values=*$2* $_filters" ; shift 2;;
-vs) _filters="Name=block-device-mapping.volume-size,Values=*$2* $_filters" ; shift 2;;
-vt) _filters="Name=block-device-mapping.volume-type,Values=*$2* $_filters" ; shift 2;;
-r) _region=$2 ; shift 2;;
+a) _more_qs="$_more_qs${_more_qs:+,}Architecture" ; shift;;
+cd) _more_qs="$_more_qs${_more_qs:+,}CreationDate" ; shift;;
+ht) _more_qs="$_more_qs${_more_qs:+,}Hypervisor" ; shift;;
+i) _more_qs="$_more_qs${_more_qs:+,}ImageId" ; shift;;
+it) _more_qs="$_more_qs${_more_qs:+,}ImageType" ; shift;;
+o) _more_qs="$_more_qs${_more_qs:+,}OwnerId" ; shift;;
+ps) _more_qs="$_more_qs${_more_qs:+,}Public" ; shift;;
+rn) _more_qs="$_more_qs${_more_qs:+,}RootDeviceName" ; shift;;
+rt) _more_qs="$_more_qs${_more_qs:+,}RootDeviceType" ; shift;;
+s) _more_qs="$_more_qs${_more_qs:+,}State" ; shift;;
+v) _more_qs="$_more_qs${_more_qs:+,}VirtualizationType" ; shift;;
+vs) _more_qs="$_more_qs${_more_qs:+,}BlockDeviceMappings[0].Ebs.VolumeSize" ; shift;;
+vt) _more_qs="$_more_qs${_more_qs:+,}BlockDeviceMappings[0].Ebs.VolumeType" ; shift;;
-h|*) echo "$_USAGE"; return;;
esac
done
[ -n "$_filters" ] && _filters="--filters ${_filters% }"
[ -n "$_more_qs" ] && _query="$_query.[$_queries,${_more_qs%,}]" || _query="$_query.[$_default_queries]"
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWSEC2DAMI_CMD --region=$_region $_owners $_filters --query \"$_query\" --output table"
if [[ "$_region" == "all" ]]; then
local _ALL_REGIONS
_ALL_REGIONS=$(aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName)
for _region in $_ALL_REGIONS; do
eval "$_AWSEC2DAMI_CMD --region=$_region $_owners $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeImages' | sort | sed 's/^| //;s/ \+|$//;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
done
else
eval "$_AWSEC2DAMI_CMD --region=$_region $_owners $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeImages' | sort | sed 's/^| //;s/ \+|$//;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
}
function awsdasg {
# some 'aws autoscaling describe-auto-scaling-groups' hacks
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWSASDASG_CMD="aws autoscaling describe-auto-scaling-groups"
local _USAGE="usage: \
awsdasg [OPTIONS]
-n NAME - filter results by this Auto Scaling Group Name
-m MAX - maximum number of items to display
-p PROFILE - AWS profile (--profile option) to use
-r REGION - region to query (default: $_DEFAULT_REGION, 'all' for all)
+at - show All Tags (keys only)
+dc - show Desired Capacity
+ht - show Health Check Type
+ii - show Instance Id(s)
+ih - show Instance Health Status
+lb - show Load Balancers
+lc - show Launch Configuration Name
+ls - show Life Cycle State
+ni - show Number of Instances
+ns - show Min Size
+xs - show Max Size
+sp - show Suspended Processes
+t KEY - show tag (KEY)
-h - help (show this message)
default display:
ASG name | Launch Config Name | Instances | Desired | Min | Max | Region"
local _max_items=""
local _region="$_DEFAULT_REGION"
local _reg_exp=""
local _queries="AutoScalingGroupName"
local _default_queries="AutoScalingGroupName,LaunchConfigurationName,length(Instances),DesiredCapacity,MinSize,MaxSize"
local _more_qs=""
local _query="AutoScalingGroups[]"
while [[ $# -gt 0 ]]; do
case $1 in
-n) _reg_exp="$2" ; shift 2;;
-m) _max_items="--max-items $2" ; shift 2;;
-p) local _profile="--profile=$2" ; shift 2;;
-r) _region=$2 ; shift 2;;
+at) _more_qs="$_more_qs${_more_qs:+,}Tags[].Key|join(',',@)"; shift;;
+dc) _more_qs="$_more_qs${_more_qs:+,}DesiredCapacity" ; shift;;
+ht) _more_qs="$_more_qs${_more_qs:+,}HealthCheckType" ; shift;;
+ii) _more_qs="$_more_qs${_more_qs:+,}Instances[].InstanceId|join(', ',@)" ; shift;;
+ih) _more_qs="$_more_qs${_more_qs:+,}Instances[].HealthStatus|join(', ',@)" ; shift;;
+lb) _more_qs="$_more_qs${_more_qs:+,}LoadBalancerNames[]|join(', ',@)" ; shift;;
+lc) _more_qs="$_more_qs${_more_qs:+,}LaunchConfigurationName" ; shift;;
+ls) _more_qs="$_more_qs${_more_qs:+,}Instances[].LifecycleState|join(', ',@)" ; shift;;
+ni) _more_qs="$_more_qs${_more_qs:+,}length(Instances)" ; shift;;
+ns) _more_qs="$_more_qs${_more_qs:+,}MinSize" ; shift;;
+xs) _more_qs="$_more_qs${_more_qs:+,}MaxSize" ; shift;;
+sp) _more_qs="$_more_qs${_more_qs:+,}SuspendedProcesses[].ProcessName|join(', ',@)"; shift;;
+t) _more_qs="$_more_qs${_more_qs:+,}Tags[?Key=='$2'].Value|[0]" ; shift 2;;
-h|*) echo "$_USAGE"; return;;
esac
done
[ -n "$_more_qs" ] && _query="$_query.[$_queries,${_more_qs%,}]" || _query="$_query.[$_default_queries]"
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWSASDASG_CMD $_profile --region=$_region $_max_items --query \"$_query\" --output table"
if [[ "$_region" == "all" ]]; then
local _ALL_REGIONS
_ALL_REGIONS=$(aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName)
for _region in $_ALL_REGIONS; do
if [[ -z "$_reg_exp" ]]; then
eval "$_AWSASDASG_CMD $_profile --region=$_region $_max_items --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeAutoScalingGroups' | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
else
eval "$_AWSASDASG_CMD $_profile --region=$_region $_max_items --query \"$_query\" --output table" | \grep "$_reg_exp" | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
done
else
if [[ -z "$_reg_exp" ]]; then
eval "$_AWSASDASG_CMD $_profile --region=$_region $_max_items --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeAutoScalingGroups' | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
else
eval "$_AWSASDASG_CMD $_profile --region=$_region $_max_items --query \"$_query\" --output table" | \grep "$_reg_exp" | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
fi
}
function awsdi {
# some 'aws ec2 describe-instances' hacks
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWS_EC2_DI_CMD="aws ec2 describe-instances"
local _USAGE="usage: \
awsdi [OPTIONS]
-n NAME - filter results by this Instance Name
-s STATE - filter results by this State (e.g. running, terminated, etc.)
-t KEY=VAL - filter results by this tag (key=val)
-m MAX - maximum number of items to display
-r REGION - Region to query (default: $_DEFAULT_REGION, 'all' for all)
-p PROFILE - AWS profile (--profile option) to use
+a - show AMI (ImageId)
+an - show ASG Name
+at - show All Tags (keys only)
+az - show Availability Zone
+ip - show Private IP
+IP - show Instance Profile (ARN)
+it - show Instance Type
+k - show Key Pair name
+lt - show Launch Time
+np - show Network Interface(s) Private IPs
+p - show Platform
+pi - show Public IP
+pt - show Placment Tenancy
+pw PEM_FILE - show [Windows] Admin Passwords [PEM_FILE to decrypt]
+s - show State (e.g. running, stopped...)
+si - show Security Group Id(s)
+sn - show Security Group Name(s)
+t KEY - show tag (KEY)
+v - show VPC ID
-h - help (show this message)
default display:
Inst name | Private IP | Instance ID | State"
local _default_queries="Tags[?Key=='Name'].Value|[0],InstanceId,PrivateIpAddress,PublicIpAddress,State.Name"
local _filters=""
local _max_items=""
local _show_pws="false" # show passwords
local _pem_file="" # PEM file used to decrypt the passwords
local _more_qs=""
local _queries="Tags[?Key=='Name'].Value|[0],InstanceId"
local _query="Reservations[].Instances[]"
local _region="$_DEFAULT_REGION"
while [[ $# -gt 0 ]]; do
case $1 in
-n) _filters="Name=tag:Name,Values=*$2* $_filters" ; shift 2;;
-s) _filters="Name=instance-state-name,Values=*$2* $_filters" ; shift 2;;
-t) _filters="\"Name=tag:${2%%=*},Values=*${2##*=}*\" $_filters" ; shift 2;;
-m) _max_items="--max-items $2" ; shift 2;;
-p) local _profile="--profile=$2" ; shift 2;;
-r) _region=$2 ; shift 2;;
+a) _more_qs="$_more_qs${_more_qs:+,}ImageId" ; shift;;
+an) _more_qs="$_more_qs${_more_qs:+,}Tags[?Key=='aws:autoscaling:groupName'].Value|[0]"; shift;;
+at) _more_qs="$_more_qs${_more_qs:+,}Tags[].Key|join(',',@)"; shift;;
+az) _more_qs="$_more_qs${_more_qs:+,}Placement.AvailabilityZone" ; shift;;
+ip) _more_qs="$_more_qs${_more_qs:+,}PrivateIpAddress" ; shift;;
+IP) _more_qs="$_more_qs${_more_qs:+,}IamInstanceProfile.Arn" ; shift;;
+it) _more_qs="$_more_qs${_more_qs:+,}InstanceType" ; shift;;
+k) _more_qs="$_more_qs${_more_qs:+,}KeyName" ; shift;;
+lt) _more_qs="$_more_qs${_more_qs:+,}LaunchTime" ; shift;;
+np) _more_qs="$_more_qs${_more_qs:+,}NetworkInterfaces[].PrivateIpAddresses[].PrivateIpAddress|join(', ',@)" ; shift;;
+p) _more_qs="$_more_qs${_more_qs:+,}Platform" ; shift;;
+pi) _more_qs="$_more_qs${_more_qs:+,}PublicIpAddress" ; shift;;
+pt) _more_qs="$_more_qs${_more_qs:+,}Placement.Tenancy" ; shift;;
+pw) _show_pws="true"; [ $# -lt 2 ]&&{ echo "missing PEM_FILE";return; }||_pem_file=$2 ; shift 2;;
+s) _more_qs="$_more_qs${_more_qs:+,}State.Name" ; shift;;
+si) _more_qs="$_more_qs${_more_qs:+,}SecurityGroups[].GroupId|join(', ',@)" ; shift;;
+sn) _more_qs="$_more_qs${_more_qs:+,}SecurityGroups[].GroupName|join(', ',@)" ; shift;;
+t) _more_qs="$_more_qs${_more_qs:+,}Tags[?Key=='$2'].Value|[0]" ; shift 2;;
+v) _more_qs="$_more_qs${_more_qs:+,}VpcId" ; shift;;
-h|*) echo "$_USAGE"; return;;
esac
done
[ -n "$_filters" ] && _filters="--filters ${_filters% }"
[ -n "$_more_qs" ] && _query="$_query.[$_queries,${_more_qs%,}]" || _query="$_query.[$_default_queries]"
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWS_EC2_DI_CMD $_profile --region=$_region $_max_items $_filters --query \"$_query\" --output table"
[ -n "$AWS_DEBUG" ] && echo "debug: aws ec2 get-password-data --instance-id $_instance_id --priv-launch-key $_pem_file | jq -r .PasswordData"
if [[ "$_region" == "all" ]]; then
local _ALL_REGIONS
_ALL_REGIONS=$(aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName)
if [[ "$_show_pws" == "true" ]]; then
[ ! -f "$_pem_file" ] && { echo "private key file not found: '$_pem_file'"; return; }
local _tmp_file
_tmp_file=$(mktemp /tmp/awsdi_pws.XXXX)
for _region in $_ALL_REGIONS; do
eval "$_AWS_EC2_DI_CMD $_profile --region=$_region $_max_items $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeInstances' | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ *| */|/g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g' >> "$_tmp_file"
done
local _instance_id
local _awsdi_line
local _pw
# shellcheck disable=SC2013
for _instance_id in $(awk '{print $3}' "$_tmp_file"); do
_awsdi_line=$(\grep "$_instance_id" "$_tmp_file")
_pw=$(aws ec2 get-password-data --instance-id "$_instance_id" --priv-launch-key "$_pem_file" | jq -r .PasswordData)
if [[ -n "$_pw" ]]; then
echo "$_awsdi_line | $_pw"
else
echo "$_awsdi_line | none"
fi
done
rm -f "$_tmp_file"
else
for _region in $_ALL_REGIONS; do
eval "$_AWS_EC2_DI_CMD $_profile --region=$_region $_max_items $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeInstances' | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ *| */|/g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
done
fi
else
if [[ "$_show_pws" == "true" ]]; then
local _awsdi_line
local _instance_id
local _pw
local _tmp_file
[ ! -f "$_pem_file" ] && { echo "private key file not found: '$_pem_file'"; return; }
_tmp_file=$(mktemp /tmp/awsdi_pws.XXXX)
eval "$_AWS_EC2_DI_CMD $_profile --region=$_region $_max_items $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeInstances' | sort | sed 's/^| //;s/ |$//;s/ *| */|/g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g' >> "$_tmp_file"
# shellcheck disable=SC2013
for _instance_id in $(awk '{print $3}' "$_tmp_file"); do
_awsdi_line=$(\grep "$_instance_id" "$_tmp_file")
_pw=$(aws ec2 get-password-data --instance-id "$_instance_id" --priv-launch-key "$_pem_file" | jq -r .PasswordData)
if [[ -n "$_pw" ]]; then
echo "$_awsdi_line | $_pw"
else
echo "$_awsdi_line | none"
fi
done
rm -f "$_tmp_file"
else
eval "$_AWS_EC2_DI_CMD $_profile --region=$_region $_max_items $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeInstances' | sort | sed 's/^| //;s/ |$//;s/ *| */|/g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
fi
}
function awsdis {
# some 'aws ec2 describe-instance-status' hacks
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWS_EC2_DIS_CMD="aws ec2 describe-instance-status"
local _USAGE="usage: \
awsdis [OPTIONS]
-c CODE - filter by Event Code ({instance,system}-{reboot,retirement,stop,maintenance})
-s STATE - filter by Instance State (pending, running, shutting-down, terminated, stopped)
-r REGION - Region to query (default: $_DEFAULT_REGION, 'all' for all)
+az - show Availability Zone
+c - show Events Codes
+d - show Events Descriptions
+s - show Instance State
+t - show Events Dates and Times
-h - help (show this message)
default display:
Instance ID | State | Event Code | Event Description"
local _region="$_DEFAULT_REGION"
local _filters=""
local _queries="InstanceId"
#local _default_queries="InstanceId,InstanceState.Name,Events[].Code|join(', ',@),Events[].Description|join(', ',@)"
local _default_queries="InstanceId,InstanceState.Name,Events[0].Code,Events[0].Description"
local _more_qs=""
local _query="InstanceStatuses[]"
while [[ $# -gt 0 ]]; do
case $1 in
-c) _filters="Name=event.code,Values=*$2* $_filters" ; shift 2;;
-s) _filters="Name=instance-state-name,Values=*$2* $_filters" ; shift 2;;
-r) _region=$2 ; shift 2;;
+az) _more_qs="$_more_qs${_more_qs:+,}AVailabilityZone" ; shift;;
#+c) _more_qs="$_more_qs${_more_qs:+,}Events[].Code|join(', ',@)" ; shift;;
+c) _more_qs="$_more_qs${_more_qs:+,}Events[0].Code" ; shift;;
#+d) _more_qs="$_more_qs${_more_qs:+,}Events[].Description|join(', ',@)" ; shift;;
+d) _more_qs="$_more_qs${_more_qs:+,}Events[0].Description" ; shift;;
+s) _more_qs="$_more_qs${_more_qs:+,}InstanceState.Name" ; shift;;
#+t) _more_qs="$_more_qs${_more_qs:+,}Events[].NotBefore|join(', ',@),Events[].NotAfter|join(', ',@)" ; shift;;
+t) _more_qs="$_more_qs${_more_qs:+,}Events[0].NotBefore,Events[0].NotAfter" ; shift;;
#+fp) _more_qs="$_more_qs${_more_qs:+,}IpPermissions[].FromPort|join(', ',to_array(to_string(@)))"; shift;;
-h|*) echo "$_USAGE"; return;;
esac
done
[ -n "$_filters" ] && _filters="--filters ${_filters% }"
[ -n "$_more_qs" ] && _query="$_query.[$_queries,${_more_qs%,}]" || _query="$_query.[$_default_queries]"
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWS_EC2_DIS_CMD --region=$_region $_filters --query \"$_query\" --output table"
if [[ "$_region" == "all" ]]; then
local _ALL_REGIONS
_ALL_REGIONS=$(aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName)
for _region in $_ALL_REGIONS; do
eval "$_AWS_EC2_DIS_CMD --region=$_region $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeInstanceStatus' | sort | sed 's/^| //;s/ \+|$//;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
done
else
eval "$_AWS_EC2_DIS_CMD --region=$_region $_filters --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeInstanceStatus' | sort | sed 's/^| //;s/ \+|$//;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
}
function awsdlb {
# some 'aws elb describe-load-balancer' hacks
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWSELBDLB_CMD="aws elb describe-load-balancers"
local _USAGE="usage: \
awsdlb [OPTIONS]
-n NAME - filter results by this Launch Config Name
-m MAX - the maximum number of items to display
-r REGION - Region to query (default: $_DEFAULT_REGION, 'all' for all)
+az - show Availability Zones
+d - show DNS Name
+hc - show Health Check info (HTH, Int, T, TO, UTH)
+i - show Instances
+li - show Listeners (LB Port/Proto, Inst Port/Proto)
+s - show Scheme
+sg - show Security Groups
+sn - show Subnets
-h - help (show this message)
default display:
Load Balancer name"
local _max_items=""
local _region="$_DEFAULT_REGION"
local _reg_exp=""
local _queries="LoadBalancerName"
local _default_queries="LoadBalancerName"
local _more_qs=""
local _query="LoadBalancerDescriptions[]"
while [[ $# -gt 0 ]]; do
case $1 in
-n) _reg_exp="$2" ; shift 2;;
-m) _max_items="--max-items $2"; shift 2;;
-r) _region=$2 ; shift 2;;
+az) _more_qs="$_more_qs${_more_qs:+,}AvailabilityZones[]|join(', '@)" ; shift;;
+d) _more_qs="$_more_qs${_more_qs:+,}DNSName" ; shift;;
+hc) _more_qs="$_more_qs${_more_qs:+,}HealthCheck.HealthyThreshold,HealthCheck.Interval,HealthCheck.Target,HealthCheck.Timeout,HealthCheck.UnhealthyThreshold"; shift;;
+i) _more_qs="$_more_qs${_more_qs:+,}Instances[].InstanceId|join(', '@)"; shift;;
+li) _more_qs="$_more_qs${_more_qs:+,}ListenerDescriptions[0].Listener.LoadBalancerPort,ListenerDescriptions[0].Listener.Protocol,ListenerDescriptions[0].Listener.InstancePort,ListenerDescriptions[0].Listener.InstanceProtocol"; shift;;
+s) _more_qs="$_more_qs${_more_qs:+,}Scheme" ; shift;;
+sg) _more_qs="$_more_qs${_more_qs:+,}SecurityGroups|join(', ',@)" ; shift;;
+sn) _more_qs="$_more_qs${_more_qs:+,}Subnets[]|join(', '@)" ; shift;;
-h|*) echo "$_USAGE"; return;;
esac
done
[ -n "$_more_qs" ] && _query="$_query.[$_queries,${_more_qs%,}]" || _query="$_query.[$_default_queries]"
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWSELBDLB_CMD --region=$_region $_max_items --query \"$_query\" --output table"
if [[ "$_region" == "all" ]]; then
local _ALL_REGIONS
_ALL_REGIONS=$(aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName)
for _region in $_ALL_REGIONS; do
if [[ -z "$_reg_exp" ]]; then
eval "$_AWSELBDLB_CMD --region=$_region $_max_items --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeLoadBalancers' | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
else
eval "$_AWSELBDLB_CMD --region=$_region $_max_items --query \"$_query\" --output table" | \grep "$_reg_exp" | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
done
else
if [[ -z "$_reg_exp" ]]; then
eval "$_AWSELBDLB_CMD --region=$_region $_max_items --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeLoadBalancers' | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
else
eval "$_AWSELBDLB_CMD --region=$_region $_max_items --query \"$_query\" --output table" | \grep "$_reg_exp" | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
fi
}
function awsdlb2 {
# some 'aws elbv2 describe-load-balancer' hacks
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWSELBV2DLB_CMD="aws elbv2 describe-load-balancers"
local _USAGE="usage: \
awsdlb [OPTIONS]
-n NAME - filter results by this Launch Config Name
-m MAX - the maximum number of items to display
-r REGION - Region to query (default: $_DEFAULT_REGION, 'all' for all)
+az - show Availability Zones
+d - show DNS Name
+hc - show Health Check info (HTH, Int, T, TO, UTH)
+i - show Instances
+li - show Listeners (LB Port/Proto, Inst Port/Proto)
+s - show Scheme
+sg - show Security Groups
+sn - show Subnets
+t - show Type
-h - help (show this message)
default display:
Load Balancer name"
local _max_items=""
local _region="$_DEFAULT_REGION"
local _reg_exp=""
local _queries="LoadBalancerName"
local _default_queries="LoadBalancerName"
local _more_qs=""
local _query="LoadBalancers[]"
while [[ $# -gt 0 ]]; do
case $1 in
-n) _reg_exp="$2" ; shift 2;;
-m) _max_items="--max-items $2"; shift 2;;
-r) _region=$2 ; shift 2;;
+az) _more_qs="$_more_qs${_more_qs:+,}AvailabilityZones[]|join(', '@)" ; shift;;
+d) _more_qs="$_more_qs${_more_qs:+,}DNSName" ; shift;;
+hc) _more_qs="$_more_qs${_more_qs:+,}HealthCheck.HealthyThreshold,HealthCheck.Interval,HealthCheck.Target,HealthCheck.Timeout,HealthCheck.UnhealthyThreshold"; shift;;
+i) _more_qs="$_more_qs${_more_qs:+,}Instances[].InstanceId|join(', '@)"; shift;;
+li) _more_qs="$_more_qs${_more_qs:+,}ListenerDescriptions[0].Listener.LoadBalancerPort,ListenerDescriptions[0].Listener.Protocol,ListenerDescriptions[0].Listener.InstancePort,ListenerDescriptions[0].Listener.InstanceProtocol"; shift;;
+s) _more_qs="$_more_qs${_more_qs:+,}Scheme" ; shift;;
+sg) _more_qs="$_more_qs${_more_qs:+,}SecurityGroups|join(', ',@)" ; shift;;
+sn) _more_qs="$_more_qs${_more_qs:+,}Subnets[]|join(', '@)" ; shift;;
+t) _more_qs="$_more_qs${_more_qs:+,}Type" ; shift;;
-h|*) echo "$_USAGE"; return;;
esac
done
[ -n "$_more_qs" ] && _query="$_query.[$_queries,${_more_qs%,}]" || _query="$_query.[$_default_queries]"
[ -n "$AWS_DEBUG" ] && echo "debug: $_AWSELBV2DLB_CMD --region=$_region $_max_items --query \"$_query\" --output table"
if [[ "$_region" == "all" ]]; then
local _ALL_REGIONS
_ALL_REGIONS=$(aws ec2 describe-regions --region us-east-1 | jq -r .Regions[].RegionName)
for _region in $_ALL_REGIONS; do
if [[ -z "$_reg_exp" ]]; then
eval "$_AWSELBV2DLB_CMD --region=$_region $_max_items --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeLoadBalancers' | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
else
eval "$_AWSELBV2DLB_CMD --region=$_region $_max_items --query \"$_query\" --output table" | \grep "$_reg_exp" | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
done
else
if [[ -z "$_reg_exp" ]]; then
eval "$_AWSELBV2DLB_CMD --region=$_region $_max_items --query \"$_query\" --output table" | \grep -Ev '^[-+]|DescribeLoadBalancers' | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
else
eval "$_AWSELBV2DLB_CMD --region=$_region $_max_items --query \"$_query\" --output table" | \grep "$_reg_exp" | sort | sed 's/^| //;s/ |$/|'"$_region"'/;s/ //g' | column -s'|' -t | sed 's/\( \)\([a-zA-Z0-9]\)/ | \2/g'
fi
fi
}
function awsdlc {
# some 'aws autoscaling describe-launch-configurations' hacks
local _DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-west-2}"
local _AWSASDLC_CMD="aws autoscaling describe-launch-configurations"
local _USAGE="usage: \
awsdlc [OPTIONS]
-n NAME - filter results by this Launch Config Name
-m MAX - the maximum number of items to display
-r REGION - Region to query (default: $_DEFAULT_REGION, 'all' for all)
+i - show Image ID
+ip - show IAM Instance Profile
+it - show Instance Type
+kn - show Key Name
+pt - show Placement Tenancy
+sg - show Security Groups
-h - help (show this message)
default display:
Launch Config name | AMI ID | Instance Type | Region"
local _max_items=""
local _region="$_DEFAULT_REGION"
local _reg_exp=""
local _queries="LaunchConfigurationName"
local _default_queries="LaunchConfigurationName,ImageId,InstanceType"
local _more_qs=""
local _query="LaunchConfigurations[]"
while [[ $# -gt 0 ]]; do
case $1 in
-n) _reg_exp="$2" ; shift 2;;