Skip to content

Commit

Permalink
Merge pull request #2 from evergage/W-9219149-upgrade-terraform-to-v13
Browse files Browse the repository at this point in the history
Upgraded code to be terraform v13+ compatible
  • Loading branch information
astral303 authored May 6, 2021
2 parents 6f76235 + e62d585 commit 8bac723
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 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.3"
source = "git::https://github.com/evergage/ebs_bckup.git?ref=v1.4"
EC2_INSTANCE_TAG_NAME = "environment"
EC2_INSTANCE_TAG_VALUE = "prod"
RETENTION_DAYS = 10
Expand Down
57 changes: 37 additions & 20 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
terraform {
required_providers {
archive = {
source = "hashicorp/archive"
version = "2.2.0"
}
template = {
source = "hashicorp/template"
version = "2.2.0"
}
aws = {
source = "hashicorp/aws"
version = "3.38.0"
}
}
}

# Create the lambda role (using lambdarole.json file)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

resource "aws_iam_role" "ebs_bckup-role-lambdarole" {
name = "${var.stack_prefix}-role-lambdarole-${var.unique_name}"
assume_role_policy = "${file("${path.module}/files/lambdarole.json")}"
assume_role_policy = file("${path.module}/files/lambdarole.json")
}

# Apply the Policy Document we just created
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

resource "aws_iam_role_policy" "ebs_bckup-role-lambdapolicy" {
name = "${var.stack_prefix}-role-lambdapolicy-${var.unique_name}"
role = "${aws_iam_role.ebs_bckup-role-lambdarole.id}"
policy = "${file("${path.module}/files/lambdapolicy.json")}"
role = aws_iam_role.ebs_bckup-role-lambdarole.id
policy = file("${path.module}/files/lambdapolicy.json")
}

# Output the ARN of the lambda role
Expand All @@ -21,13 +38,13 @@ resource "aws_iam_role_policy" "ebs_bckup-role-lambdapolicy" {
# Render vars.ini for Lambda function

data "template_file" "vars" {
template = "${file("${path.module}/files/vars.ini.template")}"
vars {
EC2_INSTANCE_TAG_NAME = "${var.EC2_INSTANCE_TAG_NAME}"
EC2_INSTANCE_TAG_VALUE = "${var.EC2_INSTANCE_TAG_VALUE}"
RETENTION_DAYS = "${var.RETENTION_DAYS}"
VOLUME_TAG_NAMES_TO_RETAIN = "${join(",", var.VOLUME_TAG_NAMES_TO_RETAIN)}"
REGIONS = "${join(",", var.regions)}"
template = file("${path.module}/files/vars.ini.template")
vars = {
EC2_INSTANCE_TAG_NAME = var.EC2_INSTANCE_TAG_NAME
EC2_INSTANCE_TAG_VALUE = var.EC2_INSTANCE_TAG_VALUE
RETENTION_DAYS = var.RETENTION_DAYS
VOLUME_TAG_NAMES_TO_RETAIN = join(",", var.VOLUME_TAG_NAMES_TO_RETAIN)
REGIONS = join(",", var.regions)
}
}

Expand All @@ -36,11 +53,11 @@ data "archive_file" "lambda_zip" {
output_path = "${path.module}/lambda-${var.stack_prefix}-${var.unique_name}.zip"
source {
filename = "ebs_bckup.py"
content = "${file("${path.module}/ebs_bckup/ebs_bckup.py")}"
content = file("${path.module}/ebs_bckup/ebs_bckup.py")
}
source {
filename = "vars.ini"
content = "${data.template_file.vars.rendered}"
content = data.template_file.vars.rendered
}
}

Expand All @@ -50,11 +67,11 @@ 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"
source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
role = "${aws_iam_role.ebs_bckup-role-lambdarole.arn}"
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
role = aws_iam_role.ebs_bckup-role-lambdarole.arn
runtime = "python3.8"
handler = "ebs_bckup.lambda_handler"
timeout = "${var.timeout}"
timeout = var.timeout
publish = true
memory_size = 1024

Expand All @@ -72,9 +89,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 All @@ -83,7 +100,7 @@ resource "aws_cloudwatch_event_target" "run_ebs_bckup_lambda" {
resource "aws_lambda_permission" "allow_cloudwatch_to_call" {
statement_id = "${var.stack_prefix}_AllowExecutionFromCloudWatch_${var.unique_name}"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.ebs_bckup_lambda.function_name}"
function_name = aws_lambda_function.ebs_bckup_lambda.function_name
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.ebs_bckup_timer.arn}"
source_arn = aws_cloudwatch_event_rule.ebs_bckup_timer.arn
}
4 changes: 2 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
output "aws_iam_role_arn" {
value = "${aws_iam_role.ebs_bckup-role-lambdarole.arn}"
value = aws_iam_role.ebs_bckup-role-lambdarole.arn
}


output "lambda_function_name" {
value = "${aws_lambda_function.ebs_bckup_lambda.function_name}"
value = aws_lambda_function.ebs_bckup_lambda.function_name
}
6 changes: 3 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ variable "RETENTION_DAYS" {

variable "VOLUME_TAG_NAMES_TO_RETAIN" {
default = []
type = "list"
type = list
description = "List of volume tag names, which will be copied to the snapshot tags from the volume"
}

Expand All @@ -36,11 +36,11 @@ variable "cron_expression" {
}

variable "regions" {
type = "list"
type = list
description = "List of regions in which this Lambda function may run. At least one region is required."
}

variable "timeout" {
default = "600"
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 8bac723

Please sign in to comment.