Skip to content

Commit 441b8c4

Browse files
author
Rafael Felix Correa
committed
Support for scheduled lambda runs through Cloudwatch Events rule
1 parent f371cbf commit 441b8c4

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,15 @@ function name unique per region, for example by setting
9393

9494
| Name | Description |
9595
|------|-------------|
96+
<<<<<<< HEAD
9697
| function\_arn | The ARN of the Lambda function |
9798
| function\_name | The name of the Lambda function |
9899
| role\_arn | The ARN of the IAM role created for the Lambda function |
99100
| role\_name | The name of the IAM role created for the Lambda function |
101+
=======
102+
| function_arn | The ARN of the Lambda function |
103+
| function_name | The name of the Lambda function |
104+
| role_arn | The ARN of the IAM role created for the Lambda function |
105+
| role_name | The name of the IAM role created for the Lambda function |
106+
| cloudwatch_rule_arn | The ARN of the Cloudwatch rule |
107+
>>>>>>> Support for scheduled lambda runs through Cloudwatch Events rule

cloudwatch.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "aws_cloudwatch_event_rule" "rule" {
2+
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}"
3+
name = "${var.cloudwatch_rule_config["name"]}"
4+
description = "${var.cloudwatch_rule_config["description"]}"
5+
schedule_expression = "${var.cloudwatch_rule_config["schedule_expression"]}"
6+
}
7+
8+
resource "aws_cloudwatch_event_target" "target" {
9+
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}"
10+
target_id = "${element(concat(aws_lambda_function.lambda.*.function_name, aws_lambda_function.lambda_s3.*.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)}"
11+
rule = "${aws_cloudwatch_event_rule.rule.name}"
12+
arn = "${element(concat(aws_lambda_function.lambda.*.arn, aws_lambda_function.lambda_s3.*.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)}"
13+
}

lambda.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,12 @@ resource "aws_lambda_function" "lambda_with_dl_and_vpc" {
147147
depends_on = ["null_resource.archive"]
148148
environment = ["${slice( list(var.environment), 0, length(var.environment) == 0 ? 0 : 1 )}"]
149149
}
150+
151+
resource "aws_lambda_permission" "cloudwatch_trigger" {
152+
count = "${var.attach_cloudwatch_rule_config ? 1 : 0}"
153+
statement_id = "AllowExecutionFromCloudWatch"
154+
action = "lambda:InvokeFunction"
155+
function_name = "${element(concat(aws_lambda_function.lambda.*.function_name, aws_lambda_function.lambda_s3.*.arn, 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)}"
156+
principal = "events.amazonaws.com"
157+
source_arn = "${aws_cloudwatch_event_rule.rule.arn}"
158+
}

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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
description = "Test scheduled lambda run"
32+
schedule_expression = "cron(0 20 * * ? *)"
33+
}
34+
}
35+
36+
output "cloudwatchrule_arn" {
37+
value = "${module.lambda.cloudwatch_rule_arn}"
38+
}

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ variable "attach_vpc_config" {
7373
default = false
7474
}
7575

76+
variable "cloudwatch_rule_config" {
77+
description = "Cloudwatch Rule for the Lambda function"
78+
type = "map"
79+
default = {}
80+
}
81+
82+
variable "attach_cloudwatch_rule_config" {
83+
description = "Set this to true if using the cloudwatch_rule_config variable"
84+
type = "string"
85+
default = false
86+
}
87+
7688
variable "source_from_s3" {
7789
description = "Set this to true if fetching the Lambda source code from S3."
7890
type = "string"

0 commit comments

Comments
 (0)