Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 230b8b6

Browse files
author
Chris Machler
committed
Merged dev into master
2 parents 420737e + 3ed6b48 commit 230b8b6

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ First create an IAM policy called "ebs-backup-worker" with the following policy
3535
"Resource": [
3636
"*"
3737
]
38+
},
39+
{
40+
"Effect": "Allow",
41+
"Action": [
42+
"sns:Publish"
43+
],
44+
"Resource": "*"
3845
}
3946
]
4047
}
@@ -79,6 +86,11 @@ Type "help", "copyright", "credits" or "license" for more information.
7986
```
8087
**We will copy the encoded value and add it as the Lambda environment variable "aws_regions". When copying the encoded value please omit the single quotes in the output (ie. dXMtd2VzdC0yLHVzLWVhc3QtMg==).**
8188

89+
## Add the SNS Topics ARN you want publish as a Lambda environmenet variable "aws_sns_arn"
90+
91+
This is optional environment variable if you want publish any topic, so you might receive email notification
92+
once backing up was executed.
93+
8294
## Create the Lambda Functions
8395

8496
Create two functions in Lambda using the Python 2.7 runtime, one for the backup script and one for the cleanup script. I recommend just using the 128 MB memory setting, and adjust the timeout to 10 seconds (longer in a larger environment). Set the event source to be "CloudWatch Events - Schedule" and set the Schedule expression to be a cron expression of your liking i.e. "cron(0 6 * * ? *)" if you want the job to be kicked off at 06:00 UTC, set the cleanup job to run a few minutes later.

lambda-ebs-backup-cleanup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def lambda_handler(event, context):
4444
]
4545
snapshot_response = ec.describe_snapshots(OwnerIds=account_ids, Filters=filters)
4646

47+
print "Found %d snapshots that need deleting in region %s on %s" % (
48+
len(snapshot_response['Snapshots']),
49+
region,
50+
delete_on)
4751

4852
for snap in snapshot_response['Snapshots']:
4953
print "Deleting snapshot %s" % snap['SnapshotId']

lambda-ebs-backup.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,28 @@
33
import datetime
44
import base64
55
import os
6+
import json
67

78
base64_region = os.environ['aws_regions']
9+
aws_sns_arn = os.getenv('aws_sns_arn', None)
10+
11+
def send_to_sns(subject, message):
12+
if aws_sns_arn is None:
13+
return
14+
15+
print "Sending notification to: %s" % aws_sns_arn
16+
17+
client = boto3.client('sns')
18+
19+
response = client.publish(
20+
TargetArn=aws_sns_arn,
21+
Message=message,
22+
Subject=subject)
23+
24+
if 'MessageId' in response:
25+
print "Notification sent with message id: %s" % response['MessageId']
26+
else:
27+
print "Sending notification failed with response: %s" % str(response)
828

929
def lambda_handler(event, context):
1030
decoded_regions = base64.b64decode(base64_region)
@@ -28,7 +48,7 @@ def lambda_handler(event, context):
2848
for r in reservations
2949
], [])
3050

31-
print "Found %d instances that need backing up" % len(instances)
51+
print "Found %d instances that need backing up in region %s" % (len(instances), region)
3252

3353
to_tag_retention = collections.defaultdict(list)
3454
to_tag_mount_point = collections.defaultdict(list)
@@ -82,3 +102,6 @@ def lambda_handler(event, context):
82102
{'Key': 'DeleteOn', 'Value': delete_fmt},
83103
]
84104
)
105+
106+
message = "{} instances have been backed up in region {}".format(len(instances), region)
107+
send_to_sns('EBS Backups', message)

0 commit comments

Comments
 (0)