Skip to content

Commit 8b80bc3

Browse files
committed
Add instance-stop-protection functions
On May 24,2022 AWS announced the ability to protect instances from unintentional stop actions[0]. This PR adds that ability to bash-my-aws. [0]https://aws.amazon.com/about-aws/whats-new/2022/05/amazon-ec2-enables-protect-instances-unintentional-stop-actions/
1 parent 2c38b6c commit 8b80bc3

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

aliases

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ alias instance-stack='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stack'
8484
alias instance-start='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-start'
8585
alias instance-state='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-state'
8686
alias instance-stop='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stop'
87+
alias instance-stop-protection='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stop-protection'
88+
alias instance-stop-protection-disable='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stop-protection-disable'
89+
alias instance-stop-protection-enable='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-stop-protection-enable'
8790
alias instance-tags='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-tags'
8891
alias instance-terminate='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-terminate'
8992
alias instance-termination-protection='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma instance-termination-protection'

bash_completion.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ complete -F _bma_instances_completion instance-stack
180180
complete -F _bma_instances_completion instance-start
181181
complete -F _bma_instances_completion instance-state
182182
complete -F _bma_instances_completion instance-stop
183+
complete -F _bma_instances_completion instance-stop-protection
184+
complete -F _bma_instances_completion instance-stop-protection-disable
185+
complete -F _bma_instances_completion instance-stop-protection-enable
183186
complete -F _bma_instances_completion instance-tags
184187
complete -F _bma_instances_completion instance-terminate
185188
complete -F _bma_instances_completion instance-termination-protection

docs/command-reference.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,33 @@ Stop EC2 Instance(s)
652652
i-5d74753e210bfe04d PreviousState=running CurrentState=stopping
653653

654654

655+
### instance-stop-protection
656+
657+
List current state of stop Protection for EC2 Instance(s)
658+
659+
USAGE: instance-stop-protection instance-id [instance-id]
660+
661+
$ instances | instance-stop-protection
662+
i-4e15ece1de1a3f869 DisableApiStop=true
663+
i-89cefa9403373d7a5 DisableApiStop=false
664+
i-806d8f1592e2a2efd DisableApiStop=false
665+
i-61e86ac6be1e2c193 DisableApiStop=false
666+
667+
668+
### instance-stop-protection-disable
669+
670+
Disable EC2 Instance stop protection
671+
672+
USAGE: instance-stop-protection-disable instance-id [instance-id]
673+
674+
675+
### instance-stop-protection-enable
676+
677+
Enable EC2 Instance stop protection
678+
679+
USAGE: instance-stop-protection-enable instance-id [instance-id]
680+
681+
655682
### instance-tags
656683

657684
List tags applied EC2 Instance(s)

lib/instance-functions

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,91 @@ instance-stop() {
423423
}
424424

425425

426+
instance-stop-protection() {
427+
428+
# List current state of Stop Protection for EC2 Instance(s)
429+
#
430+
# USAGE: instance-stop-protection instance-id [instance-id]
431+
#
432+
# $ instances | instance-termination-protection
433+
# i-4e15ece1de1a3f869 DisableApiStop=true
434+
# i-89cefa9403373d7a5 DisableApiStop=false
435+
# i-806d8f1592e2a2efd DisableApiStop=false
436+
# i-61e86ac6be1e2c193 DisableApiStop=false
437+
438+
local instance_ids=$(skim-stdin "$@")
439+
[[ -z $instance_ids ]] && __bma_usage "instance-id [instance-id]" && return 1
440+
441+
for instance_id in $instance_ids; do
442+
aws ec2 describe-instance-attribute \
443+
--attribute disableApiStop \
444+
--instance-id "$instance_id" \
445+
--output text \
446+
--query "[
447+
InstanceId,
448+
join('=', [
449+
'DisableApiStop',
450+
to_string(DisableApiStop.Value)
451+
])
452+
]"
453+
done
454+
}
455+
456+
457+
instance-stop-protection-disable() {
458+
459+
# Disable EC2 Instance stop protection
460+
#
461+
# USAGE: instance-stop-protection-disable instance-id [instance-id]
462+
463+
local instance_ids=$(skim-stdin "$@")
464+
[[ -z $instance_ids ]] && __bma_usage "instance-id [instance-id]" && return 1
465+
466+
echo "You are about to disable stop protection on the following instances:"
467+
echo "$instance_ids" | tr ' ' "\n" | instances
468+
[ -t 0 ] || exec </dev/tty # reattach keyboard to STDIN
469+
local regex_yes="^[Yy]$"
470+
read -p "Are you sure you want to continue? " -n 1 -r
471+
echo
472+
if [[ $REPLY =~ $regex_yes ]]
473+
then
474+
for instance_id in $instance_ids; do
475+
aws ec2 modify-instance-attribute \
476+
--attribute disableApiStop \
477+
--value false \
478+
--instance-id "$instance_id"
479+
done
480+
fi
481+
}
482+
483+
484+
instance-stop-protection-enable() {
485+
486+
# Enable EC2 Instance stop protection
487+
#
488+
# USAGE: instance-stop-protection-enable instance-id [instance-id]
489+
490+
local instance_ids=$(skim-stdin "$@")
491+
[[ -z $instance_ids ]] && __bma_usage "instance-id [instance-id]" && return 1
492+
493+
echo "You are about to enable stop protection on the following instances:"
494+
echo "$instance_ids" | tr ' ' "\n" | instances
495+
[ -t 0 ] || exec </dev/tty # reattach keyboard to STDIN
496+
local regex_yes="^[Yy]$"
497+
read -p "Are you sure you want to continue? " -n 1 -r
498+
echo
499+
if [[ $REPLY =~ $regex_yes ]]
500+
then
501+
for instance_id in $instance_ids; do
502+
aws ec2 modify-instance-attribute \
503+
--attribute disableApiStop \
504+
--value true \
505+
--instance-id "$instance_id"
506+
done
507+
fi
508+
}
509+
510+
426511
instance-tags() {
427512

428513
# List tags applied EC2 Instance(s)

0 commit comments

Comments
 (0)