Skip to content
This repository has been archived by the owner on May 18, 2020. It is now read-only.

Added the TAGS_TO_COPY configuration parameter. #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion ebs_bckup/ebs_bckup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def lambda_handler(event, context):
regionsList = regionsStrg.split(',')
EC2_INSTANCE_TAG = config.get('main', 'EC2_INSTANCE_TAG')
retention_days = config.getint('main', 'RETENTION_DAYS')
tags_to_copy_str = config.get('main', 'TAGS_TO_COPY')
tags_to_copy = set(tags_to_copy_str.split(','))

for r in regionsList:
aws_region = r
print("Checking Region %s" % aws_region)
Expand All @@ -39,9 +42,12 @@ def lambda_handler(event, context):
vol_id = dev['Ebs']['VolumeId']
instance_id=instance['InstanceId']
instance_name = ''
instance_tags = []
for tags in instance['Tags']:
if tags["Key"] == 'Name':
instance_name = tags["Value"]
elif tags["Key"] in tags_to_copy:
instance_tags += [tags]
print("Found EBS Volume %s on Instance %s, creating Snapshot" % (vol_id, instance['InstanceId']))
snap = ec.create_snapshot(
Description="Snapshot of Instance %s (%s) %s" % (instance_id, instance_name, dev['DeviceName']),
Expand All @@ -52,7 +58,7 @@ def lambda_handler(event, context):
delete_fmt = delete_date.strftime('%Y-%m-%d')
ec.create_tags(
Resources=[snap['SnapshotId']],
Tags=[
Tags = instance_tags + [
{'Key': 'DeleteOn', 'Value': delete_fmt},
{'Key': 'Name', 'Value': snapshot},
{'Key': 'InstanceId', 'Value': instance_id},
Expand Down
1 change: 1 addition & 0 deletions files/vars.ini.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[main]
EC2_INSTANCE_TAG = ${EC2_INSTANCE_TAG}
RETENTION_DAYS = ${RETENTION_DAYS}
TAGS_TO_COPY = ${TAGS_TO_COPY}
[regions]
regionList = ${REGIONS}
21 changes: 12 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data "template_file" "vars" {
EC2_INSTANCE_TAG = "${var.EC2_INSTANCE_TAG}"
RETENTION_DAYS = "${var.RETENTION_DAYS}"
REGIONS = "${join(",", var.regions)}"
TAGS_TO_COPY = "${join(",", var.TAGS_TO_COPY)}"
}
}

Expand All @@ -34,16 +35,16 @@ resource "null_resource" "buildlambdazip" {
triggers { key = "${uuid()}" }
provisioner "local-exec" {
command = <<EOF
mkdir -p ${path.module}/lambda && mkdir -p ${path.module}/tmp
cp ${path.module}/ebs_bckup/ebs_bckup.py ${path.module}/tmp/ebs_bckup.py
echo "${data.template_file.vars.rendered}" > ${path.module}/tmp/vars.ini
mkdir -p ${path.root}/lambda && mkdir -p ${path.root}/tmp
cp ${path.module}/ebs_bckup/ebs_bckup.py ${path.root}/tmp/ebs_bckup.py
echo "${data.template_file.vars.rendered}" > ${path.root}/tmp/vars.ini
EOF
}
}
data "archive_file" "lambda_zip" {
type = "zip"
source_dir = "${path.module}/tmp"
output_path = "${path.module}/lambda/${var.stack_prefix}-${var.unique_name}.zip"
source_dir = "${path.root}/tmp"
output_path = "${path.root}/lambda/${var.stack_prefix}-${var.unique_name}.zip"
depends_on = ["null_resource.buildlambdazip"]
}

Expand All @@ -52,14 +53,16 @@ data "archive_file" "lambda_zip" {

resource "aws_lambda_function" "ebs_bckup_lambda" {
function_name = "${var.stack_prefix}_lambda_${var.unique_name}"
filename = "${path.module}/lambda/${var.stack_prefix}-${var.unique_name}.zip"
filename = "${path.root}/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"
handler = "ebs_bckup.lambda_handler"
timeout = "60"
publish = true
depends_on = ["null_resource.buildlambdazip"]

tags = "${var.tags}"
}

# Run the function with CloudWatch Event cronlike scheduler
Expand All @@ -74,9 +77,9 @@ resource "aws_cloudwatch_event_rule" "ebs_bckup_timer" {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

resource "aws_cloudwatch_event_target" "run_ebs_bckup_lambda" {
rule = "${aws_cloudwatch_event_rule.ebs_bckup_timer.name}"
target_id = "${aws_lambda_function.ebs_bckup_lambda.id}"
arn = "${aws_lambda_function.ebs_bckup_lambda.arn}"
rule = "${aws_cloudwatch_event_rule.ebs_bckup_timer.name}"
target_id = "${aws_lambda_function.ebs_bckup_lambda.id}"
arn = "${aws_lambda_function.ebs_bckup_lambda.arn}"
}

# Allow lambda to be called from cloudwatch
Expand Down
10 changes: 10 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ variable "RETENTION_DAYS" {
default = 5
description = "Numbers of Days that the EBS Snapshots will be stored (INT)"
}
variable "TAGS_TO_COPY" {
default = []
description = "Tags to copy from the EBS Volume to the snapshot"
}
variable "unique_name" {
default = "v1"
description = "Enter Unique Name to identify the Terraform Stack (lowercase)"
Expand All @@ -25,3 +29,9 @@ variable "cron_expression" {
variable "regions" {
type = "list"
}

variable "tags" {
type = "map"
description = "Optional Tags"
default = {}
}