Skip to content

Commit 0efd0c7

Browse files
authored
Merge pull request #328 from rnhurt/instance-stop-protection
Add instance-stop-protection functions
2 parents ccf4439 + e48c4d4 commit 0efd0c7

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

aliases

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ alias instance-stack='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stack'
114114
alias instance-start='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-start'
115115
alias instance-state='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-state'
116116
alias instance-stop='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stop'
117+
alias instance-stop-protection='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stop-protection'
118+
alias instance-stop-protection-disable='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stop-protection-disable'
119+
alias instance-stop-protection-enable='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stop-protection-enable'
117120
alias instance-subnet='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-subnet'
118121
alias instance-tag='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-tag'
119122
alias instance-tag-create='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-tag-create'

bash_completion.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ complete -F _bma_instances_completion instance-stack
185185
complete -F _bma_instances_completion instance-start
186186
complete -F _bma_instances_completion instance-state
187187
complete -F _bma_instances_completion instance-stop
188+
complete -F _bma_instances_completion instance-stop-protection
189+
complete -F _bma_instances_completion instance-stop-protection-disable
190+
complete -F _bma_instances_completion instance-stop-protection-enable
188191
complete -F _bma_instances_completion instance-subnet
189192
complete -F _bma_instances_completion instance-tag
190193
complete -F _bma_instances_completion instance-tag-create

docs/command-reference.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,20 +663,43 @@ Stop EC2 Instance(s)
663663
i-5d74753e210bfe04d PreviousState=running CurrentState=stopping
664664

665665

666+
### instance-stop-protection
667+
668+
List current state of stop Protection for EC2 Instance(s)
669+
670+
USAGE: instance-stop-protection instance-id [instance-id]
671+
672+
$ instances | instance-stop-protection
673+
i-4e15ece1de1a3f869 DisableApiStop=true
674+
i-89cefa9403373d7a5 DisableApiStop=false
675+
i-806d8f1592e2a2efd DisableApiStop=false
676+
i-61e86ac6be1e2c193 DisableApiStop=false
677+
678+
### instance-stop-protection-disable
679+
680+
Disable EC2 Instance stop protection
681+
682+
USAGE: instance-stop-protection-disable instance-id [instance-id]
683+
684+
685+
### instance-stop-protection-enable
686+
687+
Enable EC2 Instance stop protection
688+
689+
USAGE: instance-stop-protection-enable instance-id [instance-id]
690+
666691
### instance-subnet
667692

668693
List subnet for EC2 Instance(s)
669694

670695
USAGE: instance-subnets instance-id [instance-id]
671696

672-
673697
### instance-tags
674698

675699
List tags applied EC2 Instance(s)
676700

677701
USAGE: instance-tags instance-id [instance-id]
678702

679-
680703
### instance-tag
681704

682705
List named tag on EC2 Instance(s)

lib/instance-functions

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,91 @@ instance-subnet() {
471471
column -s$'\t' -t
472472
}
473473

474+
instance-stop-protection() {
475+
476+
# List current state of Stop Protection for EC2 Instance(s)
477+
#
478+
# USAGE: instance-stop-protection instance-id [instance-id]
479+
#
480+
# $ instances | instance-termination-protection
481+
# i-4e15ece1de1a3f869 DisableApiStop=true
482+
# i-89cefa9403373d7a5 DisableApiStop=false
483+
# i-806d8f1592e2a2efd DisableApiStop=false
484+
# i-61e86ac6be1e2c193 DisableApiStop=false
485+
486+
local instance_ids=$(skim-stdin "$@")
487+
[[ -z $instance_ids ]] && __bma_usage "instance-id [instance-id]" && return 1
488+
489+
for instance_id in $instance_ids; do
490+
aws ec2 describe-instance-attribute \
491+
--attribute disableApiStop \
492+
--instance-id "$instance_id" \
493+
--output text \
494+
--query "[
495+
InstanceId,
496+
join('=', [
497+
'DisableApiStop',
498+
to_string(DisableApiStop.Value)
499+
])
500+
]"
501+
done
502+
}
503+
504+
505+
instance-stop-protection-disable() {
506+
507+
# Disable EC2 Instance stop protection
508+
#
509+
# USAGE: instance-stop-protection-disable instance-id [instance-id]
510+
511+
local instance_ids=$(skim-stdin "$@")
512+
[[ -z $instance_ids ]] && __bma_usage "instance-id [instance-id]" && return 1
513+
514+
echo "You are about to disable stop protection on the following instances:"
515+
echo "$instance_ids" | tr ' ' "\n" | instances
516+
[ -t 0 ] || exec </dev/tty # reattach keyboard to STDIN
517+
local regex_yes="^[Yy]$"
518+
read -p "Are you sure you want to continue? " -n 1 -r
519+
echo
520+
if [[ $REPLY =~ $regex_yes ]]
521+
then
522+
for instance_id in $instance_ids; do
523+
aws ec2 modify-instance-attribute \
524+
--attribute disableApiStop \
525+
--value false \
526+
--instance-id "$instance_id"
527+
done
528+
fi
529+
}
530+
531+
532+
instance-stop-protection-enable() {
533+
534+
# Enable EC2 Instance stop protection
535+
#
536+
# USAGE: instance-stop-protection-enable instance-id [instance-id]
537+
538+
local instance_ids=$(skim-stdin "$@")
539+
[[ -z $instance_ids ]] && __bma_usage "instance-id [instance-id]" && return 1
540+
541+
echo "You are about to enable stop protection on the following instances:"
542+
echo "$instance_ids" | tr ' ' "\n" | instances
543+
[ -t 0 ] || exec </dev/tty # reattach keyboard to STDIN
544+
local regex_yes="^[Yy]$"
545+
read -p "Are you sure you want to continue? " -n 1 -r
546+
echo
547+
if [[ $REPLY =~ $regex_yes ]]
548+
then
549+
for instance_id in $instance_ids; do
550+
aws ec2 modify-instance-attribute \
551+
--attribute disableApiStop \
552+
--value true \
553+
--instance-id "$instance_id"
554+
done
555+
fi
556+
}
557+
558+
474559
instance-tags() {
475560

476561
# List tags applied EC2 Instance(s)

0 commit comments

Comments
 (0)