|
| 1 | +# Automated EBS Snapshot Creation on EC2 Shutdown using EventBridge |
| 2 | + |
| 3 | +This pattern demonstrates how to automatically create EBS snapshots when an EC2 instance shuts down using EventBridge with direct EBS snapshot target integration. It helps protect data on stateful applications or self-managed databases by capturing the volume state during instance shutdown events, reducing data loss risk and increasing recovery flexibility. While this implementation targets a single EBS volume, the pattern can be extended for multiple volumes by either deploying multiple stacks or modifying the template to use a Lambda function that can snapshot all attached volumes. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +EC2 Instance State Change → EventBridge Rule → EBS CreateSnapshot (Direct Target) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 14 | +* [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) installed |
| 15 | +* EBS Volume ID that you want to snapshot |
| 16 | +* EC2 Instance ID to monitor for shutdown events |
| 17 | + |
| 18 | +## Deployment |
| 19 | + |
| 20 | +1. Clone and navigate to the pattern directory: sam-eventbridge-ebs-snapshot |
| 21 | +2. Build the application: |
| 22 | + ```bash |
| 23 | + sam build |
| 24 | + ``` |
| 25 | +3. Deploy with guided prompts: |
| 26 | + ```bash |
| 27 | + sam deploy --guided |
| 28 | + ``` |
| 29 | +4. Follow the deployment prompts: |
| 30 | + - Stack Name: Enter `ebs-snapshot-automation` (default is `sam-app`) |
| 31 | + - AWS Region: Enter `us-east-1` (or your preferred region) |
| 32 | + - Parameter VolumeId: Enter your EBS Volume ID (e.g., `vol-0abcd123456ef7890`) |
| 33 | + - Parameter InstanceId: Enter your EC2 Instance ID (e.g., `i-0abcd123456ef7890`) |
| 34 | + - Confirm changes before deploy: Enter `Y` |
| 35 | + - Allow SAM CLI IAM role creation: Enter `Y` |
| 36 | + - Disable rollback: Enter `N` |
| 37 | + - Save arguments to configuration file: Enter `Y` |
| 38 | + |
| 39 | +## How it Works |
| 40 | + |
| 41 | +- EventBridge rule monitors EC2 instance state changes |
| 42 | +- Rule triggers when the specified instance enters "shutting-down" or "stopping" state |
| 43 | +- Rule directly targets EBS snapshot creation service using built-in target |
| 44 | +- No Lambda functions or custom code required |
| 45 | +- Snapshots are created automatically for the specified volume ID |
| 46 | +- Pure infrastructure-as-code approach |
| 47 | + |
| 48 | +## Scope and Limitations |
| 49 | + |
| 50 | +- This pattern targets a single EBS volume specified via the VolumeId parameter |
| 51 | +- It does not automatically detect or snapshot all volumes attached to an instance |
| 52 | +- Works with EBS volumes of any size (snapshot creation is asynchronous and incremental) |
| 53 | +- Only triggers on graceful EC2 state changes (stopping, shutting-down, terminated) |
| 54 | +- Will not capture snapshots during abrupt failures where no state change event is emitted |
| 55 | + |
| 56 | +### Multi-Volume Extension |
| 57 | + |
| 58 | +To handle multiple volumes, you can: |
| 59 | +1. Deploy multiple stacks of this pattern, each targeting a different volume |
| 60 | +2. Modify the template to use a Lambda function that queries the EC2 API to discover and snapshot all volumes attached to the instance |
| 61 | + |
| 62 | +## Use Cases |
| 63 | + |
| 64 | +- Protecting stateful applications or self-managed databases running on EC2 |
| 65 | +- Preserving data during EC2 instance stop/terminate events (patching, maintenance) |
| 66 | +- Safeguarding against accidental shutdowns during testing |
| 67 | + |
| 68 | +## Testing |
| 69 | + |
| 70 | +Stop or terminate the monitored EC2 instance, then check your EC2 console snapshots section. A snapshot will be created automatically when the instance shuts down. |
| 71 | + |
| 72 | +## Cleanup |
| 73 | + |
| 74 | +```bash |
| 75 | +sam delete |
| 76 | +``` |
| 77 | + |
| 78 | +**Note:** This will not delete existing snapshots. Clean up snapshots manually if needed. |
| 79 | + |
| 80 | +--- |
| 81 | +Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 82 | +SPDX-License-Identifier: MIT-0 |
0 commit comments