Enable Termination Protection on AWS instances the dirty way

meme

At work we use chef for provisioning instances. ( I hate chef ). but it is what it is.
our instances did not have termination protection enabled and we have many large nodes running specialized databases and I didn’t want anyone to terminate an instance by mistake using the AWS console.

Sometimes people are clicking around and they terminate a node on the console or even using the api.

So in order to change termination protection using the aws cli you need to get the instance ID of the instance to apply the modify-attribute and enable termination protection. I used Ansible to setup host groups for the instances. so I ran this quick command line one liner to enable me to first connect to the instance and use curl to get the instance id from the local ec2 meta-data and then enable termination protection on the instance.

for ec2_instance in `ansible -i inventory/$1.yml $2 -m shell -a "curl -s  http://169.254.169.254/latest/meta-data/instance-id" | grep -v rc | grep -v WARN` ; \
do aws ec2 modify-instance-attribute --instance-id $ec2_instance  --attribute disableApiTermination --value true ; done

in here $1 and $2 are the inventory either prod or staging and $2 is the host-group I want to get the instance id’s

I know I could do this with AWS custodian or other tools but this was very quick.

Escape Telnet session when connected to a port

facepalm

This is one thing that’s driven me crazy in the past. So say I want to use telnet to debug a connection and see if a port is listening. Sometimes you connect to the port

telnet cassandra-node-01.blabla.com 9042 and then you get connected but when you type exit the telnet session does not exit you are still connected and you need to get back to your terminal session just enter
this on the keyboard

Ctrl ]

then you will be dropped to the telnet prompt where you can enter quit and you will get back to the terminal

Escape character is '^]'.
^[^]
telnet> quit
Connection closed.
(cloudme) linuxuser

Now you are back to your terminal.