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

Commit 420737e

Browse files
author
Chris Machler
authored
Merge pull request #5 from cmachler/dev
Using Lambda Environment Variable to Define Regions
2 parents 33bb1a3 + 6bf9b4c commit 420737e

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,27 @@ Next create an IAM role also called "ebs-backup-worker" selete "AWS Lambda" as t
5757
}
5858
```
5959

60-
## Edit the scripts to add appropriate regions for your environment.
60+
## Add the regions you want run the scripts against as a Python Base64 encoded string Lambda environment variable "aws_regions".
6161

62-
Edit the `regions` list.
62+
Since Lambda does not allow commas in the environment variable values, we cannot enter in a list for our regions we want to run the script against. To work around this we will Base64 encode the list/string, and then decode the string in our script and then "split" the string into a list again.
63+
64+
Below is an example of using Python to Base64 encode our string:
65+
66+
```
67+
~$ python
68+
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
69+
[GCC 5.4.0 20160609] on linux2
70+
Type "help", "copyright", "credits" or "license" for more information.
71+
>>> import base64
72+
>>> encoded = base64.b64encode(b'us-west-2,us-east-2')
73+
>>> encoded
74+
'dXMtd2VzdC0yLHVzLWVhc3QtMg=='
75+
>>> data = base64.b64decode(encoded)
76+
>>> data
77+
'us-west-2,us-east-2'
78+
>>>
79+
```
80+
**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==).**
6381

6482
## Create the Lambda Functions
6583

lambda-ebs-backup-cleanup.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import boto3
22
import re
33
import datetime
4+
import base64
5+
import os
46

5-
regions = ['us-west-2','us-east-2']
7+
base64_region = os.environ['aws_regions']
68

79
iam = boto3.client('iam')
810

@@ -13,6 +15,11 @@
1315
"""
1416

1517
def lambda_handler(event, context):
18+
decoded_regions = base64.b64decode(base64_region)
19+
regions = decoded_regions.split(',')
20+
21+
print "Cleaning up snapshots in regions: %s" % regions
22+
1623
for region in regions:
1724
ec = boto3.client('ec2', region_name=region)
1825
account_ids = list()

lambda-ebs-backup.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import boto3
22
import collections
33
import datetime
4+
import base64
5+
import os
46

5-
regions = ['us-west-2','us-east-2']
7+
base64_region = os.environ['aws_regions']
68

79
def lambda_handler(event, context):
10+
decoded_regions = base64.b64decode(base64_region)
11+
regions = decoded_regions.split(',')
12+
13+
print "Backing up instances in regions: %s" % regions
14+
815
for region in regions:
916
ec = boto3.client('ec2', region_name=region)
1017
reservations = ec.describe_instances(

0 commit comments

Comments
 (0)