Skip to content

Commit

Permalink
add lambda cron schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
marciogoda committed Jan 30, 2024
1 parent fa3fe17 commit 43f594e
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This module will deploy a Lambda function. It supports both Zip and Image deploy
- `reserved_concurrent_executions` (number) - The amount of reserved concurrent executions for this lambda function.
- `tags` (map) - A mapping of tags to assign to this lambda function.
- `datadog_log_subscription_arn` - (string) - Log subscription arn for shipping logs to datadog.
- `lambda_cron_schedule` - (string) - Cron expression to execute the lambda. ex rate(5 minute) or cron(0 5 * * ? *)

### Zip deployment variables
- `runtime` - (string) - **REQUIRED** - The runtime environment for the Lambda function you are uploading.
Expand Down
22 changes: 22 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,25 @@ resource "aws_cloudwatch_log_subscription_filter" "kinesis_log_stream" {
filter_pattern = var.log_subscription_filter
depends_on = [aws_lambda_function.lambda_function]
}

resource "aws_cloudwatch_event_rule" "cron_schedule" {
count = var.lambda_cron_schedule != "" ? 1 : 0
name = replace("${var.function_name}-cron_schedule", "/(.{0,64}).*/", "$1")
description = "Schedule for ${var.function_name}"
schedule_expression = var.lambda_cron_schedule
}

resource "aws_cloudwatch_event_target" "event_target" {
count = var.lambda_cron_schedule != "" ? 1 : 0
rule = aws_cloudwatch_event_rule.cron_schedule[0].name
arn = aws_lambda_function.lambda_function.arn
}

resource "aws_lambda_permission" "allow_cloudwatch" {
count = var.lambda_cron_schedule != "" ? 1 : 0
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_function.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.cron_schedule[0].arn
}
2 changes: 0 additions & 2 deletions test/files/create_lambda.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
"arn": true,
"create_date": true,
"id": true,
"inline_policy": true,
"managed_policy_arns": true,
"name": true,
"unique_id": true
}
Expand Down
2 changes: 0 additions & 2 deletions test/files/create_lambda_container.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
"arn": true,
"create_date": true,
"id": true,
"inline_policy": true,
"managed_policy_arns": true,
"name": true,
"unique_id": true
}
Expand Down
2 changes: 0 additions & 2 deletions test/files/create_lambda_in_vpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
"arn": true,
"create_date": true,
"id": true,
"inline_policy": true,
"managed_policy_arns": true,
"name": true,
"unique_id": true
}
Expand Down
2 changes: 0 additions & 2 deletions test/files/create_lambda_with_layers.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
"arn": true,
"create_date": true,
"id": true,
"inline_policy": true,
"managed_policy_arns": true,
"name": true,
"unique_id": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
"arn": true,
"create_date": true,
"id": true,
"inline_policy": true,
"managed_policy_arns": true,
"name": true,
"unique_id": true
}
Expand Down
2 changes: 0 additions & 2 deletions test/files/create_lambda_with_tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
"arn": true,
"create_date": true,
"id": true,
"inline_policy": true,
"managed_policy_arns": true,
"name": true,
"unique_id": true
}
Expand Down
1 change: 1 addition & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==6.2.5
deepdiff==5.5.0
4 changes: 3 additions & 1 deletion test/test_lambda.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import unittest
import deepdiff

from subprocess import check_call, check_output

Expand Down Expand Up @@ -36,7 +37,7 @@ def assert_resource_changes(self, testname, resource_changes):
with open(f'test/files/{testname}.json', 'r') as f:
data = json.load(f)

assert data.get('resource_changes') == resource_changes
assert deepdiff.diff.DeepDiff(data.get('resource_changes'), resource_changes) == {}

def test_all_resources_to_be_created(self):
# Given When
Expand All @@ -49,6 +50,7 @@ def test_all_resources_to_be_created(self):
])

resource_changes = self.get_resource_changes()
print(resource_changes)

# Then
assert len(resource_changes) == 4
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,9 @@ variable "layers" {
description = "ARNs of the layers to attach to the lambda function in order"
default = []
}

variable "lambda_cron_schedule" {
type = string
description = "value of cron expression for scheduled lambda"
default = ""
}

0 comments on commit 43f594e

Please sign in to comment.