Ends in
00
days
00
hrs
00
mins
00
secs
ENROLL NOW

Fire Emoji 70% OFF on our Black Friday Mega Sale with $1.99 eBooks and 100+ Free Courses

Daily AMI Export: Enhancing Backup and Recovery

Home » Others » Daily AMI Export: Enhancing Backup and Recovery

Daily AMI Export: Enhancing Backup and Recovery

Daily automation of Amazon Machine Image (AMI) export is vital for keeping IT systems strong and dependable. This process ensures that the latest version of your system’s setup is always saved. If something goes wrong, like a system crash, having these up-to-date backups means you can quickly get things back up and running. Additionally, it makes it easier to grow or move your systems because you have readily available copies of machine images. By doing this every day, businesses can reduce the risk of losing data, work more efficiently, and keep everything running smoothly.

Automating daily exports of Amazon Machine Images (AMIs) offers significant benefits for IT operations. Firstly, it keeps up-to-date backups which reduces downtime during system failures. Secondly, it simplifies infrastructure scaling and migration by providing easily accessible system configurations. Additionally, it helps ensure that you are following the rules about managing data properly and lets IT teams focus on important projects by automatically handling routine tasks. With this, it helps save money by getting rid of unnecessary storage costs. Overall, daily AMI automation helps organizations make their IT systems more reliable, scalable, compliant, efficient, and cost-effective.

Implementation

Creating an EBS snapshot policy

  • Before proceeding with Step 1, ensure that the EC2 instance to be used for creating snapshots has its tags already configured.

Daily AMI Export Enhancing Backup and Recovery Image 1

 

Step 1: Go to LifeCycle Manager → Create new Lifecycle Policy → Choose EBS snapshot policy

Daily AMI Export Enhancing Backup and Recovery Image 2

 

Step 2: Specify Settings:
– Target resource types: INSTANCE
Target resource tags:
– key: your-key-id
– value: your-value

Daily AMI Export Enhancing Backup and Recovery Image 3

Step 3: For the IAM role, choose the default policy.

Daily AMI Export Enhancing Backup and Recovery Image 4

Click Next.

 

Step 4: Define a snapshot schedule for the policy.

Follow the configuration below:

Daily AMI Export Enhancing Backup and Recovery Image 5

 

Cross-Region copy through Amazon DLM

Step 5: You can also automate the copying of a snapshot to another AWS Region after it has been created. Copies can be scheduled for up to three AWS Regions from a single policy, and retention periods are set for each AWS Region separately.

Follow the configuration below:

Tutorials dojo strip

Daily AMI Export Enhancing Backup and Recovery Image 6

Step 6: Select Enable cross-account sharing for this schedule check box to share the snapshots created by this schedule with other AWS accounts.

  • Your Backup Account ID: ” “

Daily AMI Export Enhancing Backup and Recovery Image 7

 

Step 7: Review and Create Policy.

 

Configuration on the second account

Step 8: Go to the Backup Account and Create a Lifecycle Policy → Choose Cross-account copy event policy

Daily AMI Export Enhancing Backup and Recovery Image 8

Step 9: Enter a brief Description of the policy.

  • Policy description: Your Policy description

Daily AMI Export Enhancing Backup and Recovery Image 9

 

Step 10: Next to Copy snapshots shared by, enter the AWS account ID of the source account that you want to copy snapshots from.

Daily AMI Export Enhancing Backup and Recovery Image 10

 

  • Select Add after entering each account.

 

Step 11: You must add a Snapshot description filter to copy snapshots. Use a regex that will only capture snapshots created by the existing DLM policy.

  • Created for policy: policy_ID.*

 

Daily AMI Export Enhancing Backup and Recovery Image 11

Step 12: For the IAM role, select the default role.

Daily AMI Export Enhancing Backup and Recovery Image 12

Step 13: Under the Copy action, you can select up to three Regions as the destinations for your snapshots.

Note: We will copy snapshots to AWS Ohio (us-east-2o) Region, where we will retain it for 1 day.

Daily AMI Export Enhancing Backup and Recovery Image 13

Step 14: Scroll to the bottom of the page, verify that the Policy status after creation is enabled (if you want the policy to be effective immediately). Then select Create Policy.

Daily AMI Export Enhancing Backup and Recovery Image14

 

Create a scheduled Lambda function in the 2nd account that deletes EBS snapshots older than 30 days

Step 1: Go to Lambda Function

Step 2: Create Function.

  • Function name: delete-EBS-snapshots-older-than-30-days
  • Runtime: Python 3.9

Step 3: Copy the code below:

 import boto3
from datetime import datetime, timedelta, timezone

def lambda_handler(event, context):
# Specify the AWS region where you want to delete snapshots
region = 'us-east-2'

# Calculate the date 30 days ago in UTC timezone
thirty_days_ago = datetime.now(timezone.utc) - timedelta(days=30)

ec2 = boto3.client('ec2', region_name=region)

# Describe EBS snapshots in the specified region
response = ec2.describe_snapshots(OwnerIds=['self'])

deleted_snapshots = [] # Initialize a list to store deleted snapshot IDs

for snapshot in response['Snapshots']:
snapshot_id = snapshot['SnapshotId']
snapshot_start_time = snapshot['StartTime']

# Check if the snapshot is older than 30 days
if snapshot_start_time < thirty_days_ago:
# Delete the snapshot
ec2.delete_snapshot(SnapshotId=snapshot_id)
deleted_snapshots.append(snapshot_id) # Add deleted snapshot ID to the list

# Print or log the deleted snapshot IDs
if deleted_snapshots:
deleted_snapshots_str = ', '.join(deleted_snapshots)
print(f"Deleted snapshots: {deleted_snapshots_str}")
else:
print("No snapshots were deleted.")

Step 4: Go to the Configurations tab → Permissions → Select the Role name.

Daily AMI Export Enhancing Backup and Recovery Image 15

Step 5: Create Inline Policy

Daily AMI Export Enhancing Backup and Recovery Image 16

 

Step 6: Add the policy below → Name the policy as ec2-DeleteSnapshot

{
    "Version": "2012-10-17",
    "Statement": [
       {
           "Effect": "Allow",
           "Action": "ec2:DeleteSnapshot",
           "Resource": "*"
       }
    ]
}

Step 7: Attach Policy → AmazonEC2ReadOnlyAccess

Daily AMI Export Enhancing Backup and Recovery Image 17

Step 8: Go back to the Lambda Function’s Configurations tab → Triggers → Add Trigger

Daily AMI Export Enhancing Backup and Recovery Image 18

Step 9: Add a trigger that will run the Lambda Function every month.

Daily AMI Export Enhancing Backup and Recovery Image 19

Daily AMI Export Enhancing Backup and Recovery Image 20

Conclusion

Automating the daily export of Amazon Machine Images (AMIs) is essential for a strong IT infrastructure. It keeps your backups up-to-date, making system recovery and scaling easier. This involves setting up policies for EBS snapshots, copying snapshots to different regions, and automatically deleting old ones. These steps ensure your backups are reliable, secure, and accessible. Moreover, automation also helps meet data regulations and allows IT teams to focus on more important tasks while saving on storage costs. Overall, automating daily AMI exports improves disaster recovery, scalability, and IT efficiency.

70% OFF on our Black Friday Mega Sale with $1.99 eBooks and 100+ Free Courses

Tutorials Dojo portal

Learn AWS with our PlayCloud Hands-On Labs

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

FREE AWS, Azure, GCP Practice Test Samplers

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

Follow Us On Linkedin

Recent Posts

Written by: Joshua Emmanuel Santiago

Joshua, a college student at Mapúa University pursuing BS IT course, serves as an intern at Tutorials Dojo.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?