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
Nice! Thank you for putting this together!