Recover ssh access to amazon EC2 instance after ufw firewall activation by accident

There are two solutions to resolve this problem:

  • Solution 1 
  1. Stop your instance
  2. Go to User Data (Action > Instance Settings > View/Change User Data)
  3. Add following line then start instance again
 #cloud-config bootcmd:
- cloud-init-per always fix_broken_ufw_1 sh -xc "/usr/sbin/service ufw stop >> /var/tmp/svc_$INSTANCE_ID 2>&1 || true"
- cloud-init-per always fix_broken_ufw_2 sh -xc "/usr/sbin/ufw disable>> /var/tmp/ufw_$INSTANCE_ID 2>&1 || true"

  • Solution 2

    1. Stop your instance
    2. Attach your EBS volume to another instance. if you don't have one, create a micro instance.
    3. Mount yor EBS volume somewhere ie. /opt/recover
    4. List item
    5. Edit {your-ebs-mount}/etc/ufw/ufw.conf and change enabled=yes to enabled=no
    6. Umount the EBS
    7. Detach from the temp instance
    8. Reattach to the original instance. (make sure to attach as root)
    9. Restart the instance