Skip to content

Commit 5968898

Browse files
committed
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.
1 parent bfc59a9 commit 5968898

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ module "lambda" {
5454
subnet_ids = ["${aws_subnet.test.id}"]
5555
security_group_ids = ["${aws_security_group.test.id}"]
5656
}
57+
58+
// Trigger from a Cloudwatch Events rule.
59+
attach_cloudwatch_rule_config = true
60+
cloudwatch_rule_config {
61+
name = "scheduled-run"
62+
enabled = true // set this to false if you want to have the trigger declared but disabled
63+
description = "Run my lambda every day at 8pm"
64+
schedule_expression = "cron(0 20 * * ? *)"
65+
}
5766
}
5867
```
5968

@@ -68,11 +77,13 @@ function name unique per region, for example by setting
6877

6978
| Name | Description | Type | Default | Required |
7079
|------|-------------|:----:|:-----:|:-----:|
80+
| attach\_cloudwatch\_rule\_config | Set this to true if using the cloudwatch_rule_config variable | string | `false` | no |
7181
| attach\_dead\_letter\_config | Set this to true if using the dead_letter_config variable | string | `"false"` | no |
7282
| attach\_policy | Set this to true if using the policy variable | string | `"false"` | no |
7383
| attach\_vpc\_config | Set this to true if using the vpc_config variable | string | `"false"` | no |
7484
| build\_command | The command that creates the Lambda package zip file | string | `"python build.py '$filename' '$runtime' '$source'"` | no |
7585
| build\_paths | The files or directories used by the build command, to trigger new Lambda package builds whenever build scripts change | list | `<list>` | no |
86+
| cloudwatch\_rule\_config | Cloudwatch Rule for the Lambda function | map | `<map>` | no |
7687
| dead\_letter\_config | Dead letter configuration for the Lambda function | map | `<map>` | no |
7788
| description | Description of what your Lambda function does | string | `"Managed by Terraform"` | no |
7889
| enable\_cloudwatch\_logs | Set this to false to disable logging your Lambda output to CloudWatch Logs | string | `"true"` | no |
@@ -92,6 +103,7 @@ function name unique per region, for example by setting
92103

93104
| Name | Description |
94105
|------|-------------|
106+
| cloudwatch\_rule\_arn | The ARN of the Cloudwatch rule |
95107
| function\_arn | The ARN of the Lambda function |
96108
| function\_name | The name of the Lambda function |
97109
| role\_arn | The ARN of the IAM role created for the Lambda function |

cloudwatch.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "aws_lambda_permission" "cloudwatch_trigger" {
2+
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}"
3+
statement_id = "AllowExecutionFromCloudWatch"
4+
action = "${lookup(var.cloudwatch_rule_config, "enabled", true) ? "lambda:InvokeFunction" : "lambda:DisableInvokeFunction"}"
5+
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)}"
6+
principal = "events.amazonaws.com"
7+
source_arn = "${aws_cloudwatch_event_rule.rule.arn}"
8+
}
9+
resource "aws_cloudwatch_event_rule" "rule" {
10+
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}"
11+
name = "${var.cloudwatch_rule_config["name"]}"
12+
description = "${var.cloudwatch_rule_config["description"]}"
13+
schedule_expression = "${var.cloudwatch_rule_config["schedule_expression"]}"
14+
}
15+
16+
resource "aws_cloudwatch_event_target" "target" {
17+
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}"
18+
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)}"
19+
rule = "${aws_cloudwatch_event_rule.rule.name}"
20+
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)}"
21+
}

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ output "role_name" {
1717
description = "The name of the IAM role created for the Lambda function"
1818
value = "${aws_iam_role.lambda.name}"
1919
}
20+
21+
output "cloudwatch_rule_arn" {
22+
description = "The ARN of the Cloudwatch rule"
23+
value = "${element(concat(aws_cloudwatch_event_rule.rule.*.arn), 0)}"
24+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def lambda_handler(event, context):
2+
return 'test passed'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
terraform {
2+
backend "local" {
3+
path = "terraform.tfstate"
4+
}
5+
}
6+
7+
provider "aws" {
8+
region = "eu-west-1"
9+
}
10+
11+
resource "random_id" "name" {
12+
byte_length = 6
13+
prefix = "terraform-aws-lambda-scheduled-"
14+
}
15+
16+
module "lambda" {
17+
source = "../../"
18+
19+
function_name = "${random_id.name.hex}"
20+
description = "Test cloudwatch rule trigger in terraform-aws-lambda"
21+
handler = "lambda.lambda_handler"
22+
runtime = "python3.6"
23+
timeout = 30
24+
25+
source_path = "${path.module}/lambda.py"
26+
27+
attach_cloudwatch_rule_config = true
28+
29+
cloudwatch_rule_config {
30+
name = "scheduled-run"
31+
# enabled = false
32+
description = "Test scheduled lambda run"
33+
schedule_expression = "cron(0 20 * * ? *)"
34+
}
35+
}
36+
37+
output "cloudwatchrule_arn" {
38+
value = "${module.lambda.cloudwatch_rule_arn}"
39+
}

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ variable "attach_vpc_config" {
8484
default = false
8585
}
8686

87+
variable "cloudwatch_rule_config" {
88+
description = "Cloudwatch Rule for the Lambda function"
89+
type = "map"
90+
default = {}
91+
}
92+
93+
variable "attach_cloudwatch_rule_config" {
94+
description = "Set this to true if using the cloudwatch_rule_config variable"
95+
type = "string"
96+
default = false
97+
}
98+
8799
variable "tags" {
88100
description = "A mapping of tags"
89101
type = "map"

0 commit comments

Comments
 (0)