Skip to content

Instantly share code, notes, and snippets.

@geekbass
Last active April 8, 2024 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save geekbass/19e5712d1886b46adbd8b356d41ef197 to your computer and use it in GitHub Desktop.
Save geekbass/19e5712d1886b46adbd8b356d41ef197 to your computer and use it in GitHub Desktop.
Cloud Init Rescue

Rescue ec2 via Cloud-Init

If something takes over your instance and you lose ssh, here is a way that you can recover it via Cloud-init (As long as you are using Cloud-Init). Use boot_cmd to recover or undo anychanges that caused this.

Find the instance ID and stop it.

aws ec2 stop-instances --instance-ids i-instanceID --force

Create your file. Use boot_cmd as this will execute on boot every time. See https://cloudinit.readthedocs.io/en/latest/topics/examples.html?highlight=systemd#run-commands-on-first-boot for the details.

Base64 encode it.

base64 file.txt >file64.txt

Upload it to the instance when its finally stopped.

aws ec2 modify-instance-attribute --instance-id i-instanceID --attribute userData -value file://./file64.txt
aws ec2 describe-instance-attribute --instance-id i-instanceID --attribute userData --output text --query "UserData.Value" | base64 --decode
#cloud-config

bootcmd:
 - [ systemctl, disable, docker ]
 - [ systemctl, stop, docker ] 

Start the instance and validate it worked and you have unf&cked your machine.

aws ec2 start-instances --instance-ids i-instanceID
@aayvazyan
Copy link

Nice! Thank you for putting this together!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment