This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for scheduled lambda runs through Cloudwatch Events rule
this patch allow provisioning cloudwatch event rules from within the module. for now only scheduled execution is supported.
- Loading branch information
1 parent
b65918e
commit f403e71
Showing
6 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
resource "aws_lambda_permission" "cloudwatch_trigger" { | ||
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}" | ||
statement_id = "AllowExecutionFromCloudWatch" | ||
action = "${lookup(var.cloudwatch_rule_config, "enabled", true) ? "lambda:InvokeFunction" : "lambda:DisableInvokeFunction"}" | ||
function_name = "${element(concat(aws_lambda_function.lambda.*.function_name, aws_lambda_function.lambda_with_dl.*.function_name, aws_lambda_function.lambda_with_vpc.*.function_name, aws_lambda_function.lambda_with_dl_and_vpc.*.function_name), 0)}" | ||
principal = "events.amazonaws.com" | ||
source_arn = "${aws_cloudwatch_event_rule.rule.arn}" | ||
} | ||
resource "aws_cloudwatch_event_rule" "rule" { | ||
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}" | ||
name = "${var.cloudwatch_rule_config["name"]}" | ||
description = "${var.cloudwatch_rule_config["description"]}" | ||
schedule_expression = "${var.cloudwatch_rule_config["schedule_expression"]}" | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "target" { | ||
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}" | ||
target_id = "${element(concat(aws_lambda_function.lambda.*.function_name, aws_lambda_function.lambda_with_dl.*.function_name, aws_lambda_function.lambda_with_vpc.*.function_name, aws_lambda_function.lambda_with_dl_and_vpc.*.function_name), 0)}" | ||
rule = "${aws_cloudwatch_event_rule.rule.name}" | ||
arn = "${element(concat(aws_lambda_function.lambda.*.arn, aws_lambda_function.lambda_with_dl.*.arn, aws_lambda_function.lambda_with_vpc.*.arn, aws_lambda_function.lambda_with_dl_and_vpc.*.arn), 0)}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def lambda_handler(event, context): | ||
return 'test passed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
terraform { | ||
backend "local" { | ||
path = "terraform.tfstate" | ||
} | ||
} | ||
|
||
provider "aws" { | ||
region = "eu-west-1" | ||
} | ||
|
||
resource "random_id" "name" { | ||
byte_length = 6 | ||
prefix = "terraform-aws-lambda-scheduled-" | ||
} | ||
|
||
module "lambda" { | ||
source = "../../" | ||
|
||
function_name = "${random_id.name.hex}" | ||
description = "Test cloudwatch rule trigger in terraform-aws-lambda" | ||
handler = "lambda.lambda_handler" | ||
runtime = "python3.6" | ||
timeout = 30 | ||
|
||
source_path = "${path.module}/lambda.py" | ||
|
||
attach_cloudwatch_rule_config = true | ||
|
||
cloudwatch_rule_config { | ||
name = "scheduled-run" | ||
# enabled = false | ||
description = "Test scheduled lambda run" | ||
schedule_expression = "cron(0 20 * * ? *)" | ||
} | ||
} | ||
|
||
output "cloudwatchrule_arn" { | ||
value = "${module.lambda.cloudwatch_rule_arn}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters