Schedule automatic Backup in Dynamodb.

Ezequiel Arielli
Jan 29, 2018 · 6 min read

Hello People,

Then of Amazon Reinvent:2017 , They created Dynamodb backup this backup solution is very good but they didn’t create scheduled backup.

This article explain as create a lambda function for autobackup and configure custom retention and alerts.

History:

Boss(Luis) says: it is possible scheduler the backup?
My(Ezequiel): At the moment is not possible.
Boss(Luis): I think is possible create backup job using one lambda function, please review this problem.
My(Ezequiel): Okey perfect! I reading about running awscli commands in lambda but i wasn’t convinced. I keeping investigate and found boto3 support for backups in dynamodb in this url:
http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html
My(Ezequiel): I think this is great but I dont understand as building scripts in python. I’ll try.
MY(Ezequiel): In the last month take two courses in python, I understood a little.
My(Ezequiel): I began work in the script in four hours this script successfull but didn’t work perfect, later my teammate Juan helped me. He was me teach about functions in python and try/except and Juan review my script.
My(Ezequiel): In twenty minutes more later. It worked perfect. I talked Juan you are a genius, thanks.
Boss(Luis) says: Please add e-mail notification and generated push notification depending on the status.
My(Ezequiel): Okey perfect! In fifty minutes this work was complete.

Requeriments:

  • Aws account and dynamodb table for backup.

Starting to Work:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
}

Go to create Lambda function and select role created in last step.

In this part , you have two ways:

one: only use e-mail for errors.
two: use email for inform if backup is succesful or push notification if backup is failed.

Default: The script have a one week at retention, it is customizable in this line.

check = dynamo.list_backups(
TableName=name,
Limit=100,
TimeRangeUpperBound = current_time - timedelta(days=7)
#TimeRangeLowerBound = datetime(2015, 1, 1)
)

code for way one(use email alldays if result is succesful and use push notification if result is failed).

import boto3
import sys
from datetime import datetime, timedelta
import calendar
import json
import gc
from pprint import pprint

Save function and generated event with your TableName.

Saved and Test function.

Check the backup in Dynamodb. It is working ,please review your e-mail.

check e-mail

In this moment i go to create rule for schedule backup .

In targets select your LambdaFunction and insert the table name.

Now i go configure sns and push alert by opsgenie.

copy your arn in the part sns of the script:

It’s worked!

code for way two(only use email if result is failed):

import boto3
import sys
from datetime import datetime, timedelta
import calendar
import json
import gc
from pprint import pprint

# add this line
This line send a test e-mail, later delete. if your backup failed “lambda” will send e-mail for you.

Thanks for reading this article. Hope you find it helpful 😃

Ezequiel Arielli

Written by

DevOps SR | SRE @Miroculus.com San Francisco, CA www.itshellws.org

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade