How to schedule automatic (daily) snapshots of AWS EC2 Windows Instance?
Solution 1:
Amazon Web Services recently announced PowerShell command line tools for Windows and it's packaged along with their AWS Tools for .NET SDK.
The AWS Powershell tools make it quite easy to create a snapshot:
New-EC2Snapshot "vol-371acd04" -Description "My Snapshot"
And you can query your snapshots like this:
PS C:\Program Files (x86)\AWS Tools\PowerShell> Get-EC2Snapshot | more
SnapshotId : snap-18be2b28
VolumeId : vol-371acd04
Status : completed
StartTime : 2012-12-28T08:17:00.000Z
Progress : 100%
OwnerId : 383816850479
VolumeSize : 30
Description : My Snapshot
OwnerAlias :
Tag : {}
Make sure you have the AWS Powershell tools installed and just create a scheduled task that uses a powershell script similar to the snippet above to schedule your snapshots and you should be good.
Updated to query for attached EBS volumes:
To query for EBS volumes attached to your instance and then snapshot each of them you could do something like this:
# Find my instance ID from the EC2 metadata
$myInstanceID = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
# Query for volumes that are attached to my Instance Id
$volumes = (Get-EC2Volume).Attachment | where {$_.InstanceId -eq $myInstanceID } | Select VolumeId
# Iterate through these volumes and snapshot each of them
foreach ($volume in $volumes)
{
New-EC2Snapshot $volume.VolumeId -Description "My Snapshot"
}
Solution 2:
2018 Update As of late 2018 there are two additional ways to automate EBS snapshots. I still use the original method of CloudWatch events as it's worked fine for years and I see no point in changing.
Ops Automator (OA)
Ops Automator is a very flexible set of lambda scripts provided by AWS. It's deployed with a CloudWatch template which is on the page above.
It's setup steps are
- The AWS CloudFormation template launches the core framework, which includes a suite of microservices (AWS Lambda functions) that manage triggering events, resource selection, task execution, concurrency control, and completion.
- Task configuration data, which defines the triggering event, how the task should be performed, which resources will be selected by the actions, and where these resources are located, is stored in an Amazon DynamoDB table.
- Solution-generated AWS CloudFormation templates configure tasks based on parameters you define, and the roles necessary to perform actions across accounts.
- The solution tracks all steps in the process, the selected resources, and the results of the actions, including possible errors, in a DynamoDB table.
- The solution also leverages Amazon CloudWatch Logs for logging. Warning and error messages are published to a solution-created Amazon Simple Notification Service (Amazon SNS) topic which sends messages to a subsc