Skip to content

Commit 997cc23

Browse files
committed
Adding new serverless pattern using SAM
Eventbridge to EBS Snapshot Creation
1 parent 584ecdd commit 997cc23

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.
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+
## Testing
49+
50+
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.
51+
52+
## Cleanup
53+
54+
```bash
55+
sam delete
56+
```
57+
58+
**Note:** This will not delete existing snapshots. Clean up snapshots manually if needed.
59+
60+
---
61+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
62+
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: 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)