Skip to content

Commit

Permalink
Merge pull request #1 from evergage/W-9219149-update-python-and-settings
Browse files Browse the repository at this point in the history
Update python and settings
  • Loading branch information
astral303 authored May 5, 2021
2 parents 97e5d07 + dcaeaf5 commit 6f76235
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ In your Terrafom `main.tf` call the module with the required variables.
```
module "ebs_bckup" {
// It is recommended that you lock "ref" to a specific release version
source = "git::https://github.com/evergage/ebs_bckup.git?ref=v1.2"
source = "git::https://github.com/evergage/ebs_bckup.git?ref=v1.3"
EC2_INSTANCE_TAG_NAME = "environment"
EC2_INSTANCE_TAG_VALUE = "prod"
RETENTION_DAYS = 10
Expand Down
9 changes: 7 additions & 2 deletions ebs_bckup/ebs_bckup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ConfigParser
import datetime

import boto3
import configparser

config = ConfigParser.RawConfigParser()
config = configparser.ConfigParser(interpolation=None)
config.read('./vars.ini')

print('Starting EBS snapshots')
Expand All @@ -22,11 +22,14 @@ def snapshot_region(aws_region):
account = event['account']
ec = boto3.client('ec2', region_name=aws_region)
instances = find_all_eligible_instances(ec)
print("Found " + str(len(instances)) + " eligible instances to snapshot.");

for instance in instances:
snapshot_instance(ec, instance)

print("Backups complete, preparing to delete snapshots.")
purge_old_snapshots(account, ec)
print("Purge of previous backups complete.")

def find_all_eligible_instances(ec):
reservations = ec.describe_instances(
Expand Down Expand Up @@ -125,6 +128,7 @@ def purge_old_snapshots(account, ec):
],
)

print("Found %d snapshots to delete." % (len(all_managed_snapshots['Snapshots'])))
ascending_start_dates_to_delete = find_start_dates_to_delete(all_managed_snapshots)

if len(ascending_start_dates_to_delete) > 0:
Expand All @@ -133,6 +137,7 @@ def purge_old_snapshots(account, ec):
delete_snapshots_older_than(ec, last_start_date_to_delete, all_managed_snapshots)

def find_start_dates_to_delete(all_managed_snapshots):
print("Determining from which dates to delete.");
start_dates_set = set()
for snap in all_managed_snapshots['Snapshots']:
start_dates_set.add(snap['StartTime'].date())
Expand Down
4 changes: 3 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ resource "aws_lambda_function" "ebs_bckup_lambda" {
filename = "${path.module}/lambda-${var.stack_prefix}-${var.unique_name}.zip"
source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
role = "${aws_iam_role.ebs_bckup-role-lambdarole.arn}"
runtime = "python2.7"
runtime = "python3.8"
handler = "ebs_bckup.lambda_handler"
timeout = "${var.timeout}"
publish = true
memory_size = 1024

}

# Run the function with CloudWatch Event cronlike scheduler
Expand Down
2 changes: 1 addition & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ variable "regions" {
}

variable "timeout" {
default = "60"
default = "600"
description = "Number of seconds that the snapshotting Lambda is allowed to run. Increase if you have a large number of instances."
}

0 comments on commit 6f76235

Please sign in to comment.