A simple AWS lambda project to help automate creation and deletion of AMIs. The scripts are written in JavaScript and will run on the the serverless AWS Lambda platform.
Creating an AMI automatically snapshots all the associated EBS volumes for that instance. This makes instance recovery much more reliable and faster. A daily or weekly backup schedule is recommended for instances and to make sure you have a backup if ever needed. The system also removes the automatically created AMIs and any associated snapshots as per the settings.
Update 31 March 2017 - Added feature to prevent reboot while creating AMI. Add a tag BackupNoReboot with value true if want to avoid rebooting that instance.
Update December 2018 - Replaced screenshots to reflect AWS Console changes.
-
Go to the AWS Lambda Console and click Create function.
-
Give the custom role a name and paste the contents of roles.json into the edit box.
-
Now the function has been created, and you'll be presented with the lambda configuration screen.
-
Scroll down to the code editor, and remove the stub code you see in the editor.
-
Paste the contents of createAMI.js into the edit box.
-
Scroll down further and modify the Basic Settings.
Provide a description and set the timeout to 5 minutes.
-
Scroll back to the top and click Save.
Add a trigger by selecting CloudWatch Event.
-
Configure the Rule:
- Provide a rule name and description.
- Set the event pattern or schedule.
- The cron expression showed below will run at 2AM every day.
- Finish by clicking Add at the bottom.
-
Create a second function named deleteAMI.
Follow the same basic steps as above, but this time:
- Use deleteAMI.js
- Use the existing lambda Role.
- Use the existing CloudWatch Rule.
Set the tags on the instances you want backed up.
- Backup: yes
- BackupRetentionDays: a positive integer
- BackupNoReboot: true (this tag is optional - reboot will happen unless the tag is present and value is true)
For multiple instances it is easiest to use the Tag Editor.
- This is another open source project bought to you by Webdigi - Web development, London.
- Please submit your pull requests or suggestions to improve this script.
- You can read about updates to this project on our blog - AWS Instance Automated AMI Creation & Deletion System