Skip to content

Commit e7d6d39

Browse files
authored
Merge pull request #2791 from anirudhg309/anirudhg309-eventbridge-ebs-snapshot
New serverless pattern - Eventbridge-EBS-Snapshot
2 parents dbafab0 + 3e366ad commit e7d6d39

File tree

5 files changed

+302
-0
lines changed

5 files changed

+302
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
![EventBridge EBS Snapshot Architecture](./generated-diagrams/eventbridge-ebs-snapshot-diagram.png)
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
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"title": "Automated EBS Snapshot Creation on EC2 Shutdown using EventBridge",
3+
"description": "This pattern deploys an EventBridge rule that automatically creates EBS snapshots when an EC2 instance shuts down using direct EBS target integration.",
4+
"language": "CloudFormation",
5+
"level": "100",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern creates an automated EBS snapshot system using EventBridge event-driven rules with direct EBS target integration.",
11+
"The EventBridge rule monitors EC2 instance state changes and triggers when an instance enters shutting-down or stopping state.",
12+
"The solution requires Volume ID and Instance ID parameters and uses pure infrastructure-as-code approach without Lambda functions."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/sam-eventbridge-ebs-snapshot",
18+
"templateURL": "serverless-patterns/sam-eventbridge-ebs-snapshot",
19+
"projectFolder": "sam-eventbridge-ebs-snapshot",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "Amazon EventBridge Event Rules",
27+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rules.html"
28+
},
29+
{
30+
"text": "Amazon EBS Snapshots",
31+
"link": "https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSSnapshots.html"
32+
},
33+
{
34+
"text": "EventBridge Built-in Targets",
35+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html"
36+
}
37+
]
38+
},
39+
"deploy": {
40+
"text": [
41+
"sam build",
42+
"sam deploy --guided"
43+
]
44+
},
45+
"testing": {
46+
"text": [
47+
"Stop or terminate the monitored EC2 instance after deployment.",
48+
"Check EC2 console snapshots section - snapshot will be created when instance shuts down."
49+
]
50+
},
51+
"cleanup": {
52+
"text": [
53+
"sam delete",
54+
"Note: Existing snapshots must be deleted manually if needed."
55+
]
56+
},
57+
"authors": [
58+
{
59+
"name": "Anirudh Gupta",
60+
"image": "https://drive.google.com/file/d/1aQKx3aY2ID25FpsDI1HS_wSxgIMxOq9J/view?usp=sharing",
61+
"bio": "Technical Account Manager at AWS",
62+
"linkedin": "https://www.linkedin.com/in/anirudh-gupta-13a0b111a/"
63+
}
64+
]
65+
}
62.5 KB
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"title": "Automated EBS Snapshot Creation on EC2 Shutdown using EventBridge",
3+
"description": "This pattern deploys an EventBridge rule that automatically creates EBS snapshots when an EC2 instance shuts down using direct EBS target integration.",
4+
"language": "YAML",
5+
"level": "100",
6+
"framework": "AWS SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern creates an automated EBS snapshot system using EventBridge event-driven rules with direct EBS target integration.",
11+
"The EventBridge rule monitors EC2 instance state changes and triggers when an instance enters shutting-down or stopping state.",
12+
"The solution requires Volume ID and Instance ID parameters and uses pure infrastructure-as-code approach without Lambda functions."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/sam-eventbridge-ebs-snapshot",
18+
"templateURL": "serverless-patterns/sam-eventbridge-ebs-snapshot",
19+
"projectFolder": "sam-eventbridge-ebs-snapshot",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "Amazon EventBridge Event Rules",
27+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rules.html"
28+
},
29+
{
30+
"text": "Amazon EBS Snapshots",
31+
"link": "https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSSnapshots.html"
32+
},
33+
{
34+
"text": "EventBridge Built-in Targets",
35+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html"
36+
}
37+
]
38+
},
39+
"deploy": {
40+
"text": [
41+
"sam build",
42+
"sam deploy --guided"
43+
]
44+
},
45+
"testing": {
46+
"text": [
47+
"Stop or terminate the monitored EC2 instance after deployment.",
48+
"Check EC2 console snapshots section - snapshot will be created when instance shuts down."
49+
]
50+
},
51+
"cleanup": {
52+
"text": [
53+
"sam delete",
54+
"Note: Existing snapshots must be deleted manually if needed."
55+
]
56+
},
57+
"authors": [
58+
{
59+
"name": "Anirudh Gupta",
60+
"image": "https://drive.google.com/file/d/1aQKx3aY2ID25FpsDI1HS_wSxgIMxOq9J/view?usp=sharing",
61+
"bio": "Technical Account Manager at AWS",
62+
"linkedin": "anirudh-gupta-13a0b111a"
63+
}
64+
],
65+
"patternArch": {
66+
"icon1": {
67+
"x": 20,
68+
"y": 50,
69+
"service": "ec2",
70+
"label": "Amazon EC2"
71+
},
72+
"icon2": {
73+
"x": 80,
74+
"y": 50,
75+
"service": "eventbridge",
76+
"label": "Amazon EventBridge"
77+
},
78+
"line1": {
79+
"from": "icon1",
80+
"to": "icon2",
81+
"label": "state change"
82+
}
83+
}
84+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Description: Automated EBS Snapshot Creation on EC2 Shutdown using EventBridge
3+
4+
Parameters:
5+
VolumeId:
6+
Type: String
7+
Description: EBS Volume ID to snapshot (e.g., vol-0abcd123456ef7890)
8+
AllowedPattern: ^vol-[0-9a-f]{8,17}$
9+
ConstraintDescription: Must be a valid EBS Volume ID
10+
11+
InstanceId:
12+
Type: String
13+
Description: EC2 Instance ID to monitor (e.g., i-0abcd123456ef7890)
14+
AllowedPattern: ^i-[0-9a-f]{8,17}$
15+
ConstraintDescription: Must be a valid EC2 Instance ID
16+
17+
Resources:
18+
SnapshotRole:
19+
Type: AWS::IAM::Role
20+
Properties:
21+
AssumeRolePolicyDocument:
22+
Version: '2012-10-17'
23+
Statement:
24+
- Effect: Allow
25+
Principal:
26+
Service: events.amazonaws.com
27+
Action: sts:AssumeRole
28+
Policies:
29+
- PolicyName: CreateSnapshotPolicy
30+
PolicyDocument:
31+
Version: '2012-10-17'
32+
Statement:
33+
- Effect: Allow
34+
Action: ec2:CreateSnapshot
35+
Resource: '*'
36+
37+
EC2ShutdownSnapshotRule:
38+
Type: AWS::Events::Rule
39+
Properties:
40+
Description: Create EBS snapshot when EC2 instance shuts down
41+
EventPattern:
42+
source:
43+
- aws.ec2
44+
detail-type:
45+
- EC2 Instance State-change Notification
46+
detail:
47+
state:
48+
- shutting-down
49+
- stopping
50+
instance-id:
51+
- !Ref InstanceId
52+
State: ENABLED
53+
Targets:
54+
- Arn: !Sub 'arn:aws:events:${AWS::Region}:${AWS::AccountId}:target/create-snapshot'
55+
Id: EBSSnapshotTarget
56+
RoleArn: !GetAtt SnapshotRole.Arn
57+
Input: !Sub '"${VolumeId}"'
58+
59+
60+
Outputs:
61+
EventRuleArn:
62+
Description: EventBridge Rule ARN
63+
Value: !GetAtt EC2ShutdownSnapshotRule.Arn
64+
65+
VolumeId:
66+
Description: EBS Volume being snapshotted
67+
Value: !Ref VolumeId
68+
69+
InstanceId:
70+
Description: EC2 Instance being monitored
71+
Value: !Ref InstanceId

0 commit comments

Comments
 (0)