Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.

Commit 3b90458

Browse files
committed
support for cloudwatch event pattern as trigger
unified existing support of scheduled events with new pattern based event trigger in new submodule **this needs to be released as new major v5 version** fixes #53
1 parent bf3571c commit 3b90458

File tree

11 files changed

+104
-49
lines changed

11 files changed

+104
-49
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Terraform module to create AWS [Lambda](https://www.terraform.io/docs/providers/
66

77
The following [event sources](https://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html) are supported (see [examples](#examples)):
88

9-
- [cloudwatch-scheduled-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-cloudwatch-scheduled-event): configures a [CloudWatch Event Rule](https://www.terraform.io/docs/providers/aws/r/cloudwatch_event_rule.html) to trigger the Lambda on a regular, scheduled basis
9+
- [cloudwatch-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-cloudwatch-event): configures a [CloudWatch Event Rule](https://www.terraform.io/docs/providers/aws/r/cloudwatch_event_rule.html) to trigger the Lambda by CloudWatch [event pattern](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatchEventsandEventPatterns.html) or on a regular, scheduled basis
1010
- [dynamodb](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-dynamodb-event): configures an [Event Source Mapping](https://www.terraform.io/docs/providers/aws/r/lambda_event_source_mapping.html) to trigger the Lambda by DynamoDb events
1111
- [kinesis](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-kinesis-event): configures an [Event Source Mapping](https://www.terraform.io/docs/providers/aws/r/lambda_event_source_mapping.html) to trigger the Lambda by Kinesis events
1212
- [s3](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-s3-event): configures permission to trigger the Lambda by S3
@@ -46,7 +46,7 @@ module "lambda" {
4646
4747
// configurable event trigger, see examples
4848
event = {
49-
type = "cloudwatch-scheduled-event"
49+
type = "cloudwatch-event"
5050
schedule_expression = "rate(1 minute)"
5151
}
5252
@@ -74,7 +74,7 @@ module "lambda" {
7474

7575
### Examples
7676

77-
- [example-with-cloudwatch-scheduled-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-cloudwatch-scheduled-event)
77+
- [example-with-cloudwatch-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-cloudwatch-event)
7878
- [example-with-dynamodb-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-dynamodb-event)
7979
- [example-with-kinesis-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-kinesis-event)
8080
- [example-with-s3-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-s3-event)

examples/example-with-cloudwatch-scheduled-event/README.md renamed to examples/example-with-cloudwatch-event/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Example with CloudWatch scheduled event
1+
# Example with CloudWatch events
22

3-
Creates an AWS Lambda function triggered by a CloudWatch (scheduled) [event](https://docs.aws.amazon.com/lambda/latest/dg/with-scheduled-events.html).
3+
Creates AWS Lambda functions triggered by a CloudWatch [events](https://docs.aws.amazon.com/lambda/latest/dg/with-scheduled-events.html).
44

55
## requirements
66

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
module "lambda-scheduled" {
6+
source = "../../"
7+
description = "Example AWS Lambda using go with cloudwatch scheduled event trigger"
8+
filename = "${path.module}/test_function.zip"
9+
function_name = "tf-example-go-basic"
10+
handler = "example-lambda-func"
11+
runtime = "go1.x"
12+
13+
event = {
14+
type = "cloudwatch-event"
15+
schedule_expression = "rate(1 minute)"
16+
}
17+
}
18+
19+
module "lambda-pattern" {
20+
source = "../../"
21+
description = "Example AWS Lambda using go with cloudwatch event pattern trigger"
22+
filename = "${path.module}/test_function.zip"
23+
function_name = "tf-example-go-basic"
24+
handler = "example-lambda-func"
25+
runtime = "go1.x"
26+
27+
event = {
28+
type = "cloudwatch-event"
29+
event_pattern = <<PATTERN
30+
{
31+
"detail-type": [
32+
"AWS Console Sign In via CloudTrail"
33+
]
34+
}
35+
PATTERN
36+
}
37+
}

examples/example-with-cloudwatch-scheduled-event/main.tf

Lines changed: 0 additions & 27 deletions
This file was deleted.

main.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ module "lambda" {
1414
vpc_config = var.vpc_config
1515
}
1616

17-
module "event-cloudwatch-scheduled-event" {
18-
source = "./modules/event/cloudwatch-scheduled-event"
19-
enable = lookup(var.event, "type", "") == "cloudwatch-scheduled-event" ? true : false
17+
module "event-cloudwatch" {
18+
source = "./modules/event/cloudwatch-event"
19+
enable = lookup(var.event, "type", "") == "cloudwatch-event" ? true : false
2020

2121
lambda_function_arn = module.lambda.arn
22+
description = lookup(var.event, "description", "")
23+
event_pattern = lookup(var.event, "event_pattern", "")
24+
is_enabled = lookup(var.event, "is_enabled", true)
25+
name = lookup(var.event, "name", null)
26+
name_prefix = lookup(var.event, "name_prefix", null)
2227
schedule_expression = lookup(var.event, "schedule_expression", "")
2328
}
2429

modules/event/cloudwatch-scheduled-event/main.tf renamed to modules/event/cloudwatch-event/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ resource "aws_lambda_permission" "cloudwatch" {
99

1010
resource "aws_cloudwatch_event_rule" "lambda" {
1111
count = var.enable ? 1 : 0
12+
description = var.description
13+
event_pattern = var.event_pattern
14+
is_enabled = var.is_enabled
15+
name = var.name
16+
name_prefix = var.name_prefix
1217
schedule_expression = var.schedule_expression
1318
}
1419

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# REQUIRED PARAMETERS
3+
# You must provide a value for each of these parameters.
4+
# ---------------------------------------------------------------------------------------------------------------------
5+
6+
variable "lambda_function_arn" {
7+
description = "The Amazon Resource Name (ARN) identifying the Lambda Function trigger by CloudWatch"
8+
}
9+
10+
# ---------------------------------------------------------------------------------------------------------------------
11+
# OPTIONAL PARAMETERS
12+
# These parameters have reasonable defaults.
13+
# ---------------------------------------------------------------------------------------------------------------------
14+
15+
variable "description" {
16+
default = ""
17+
description = "The description of the rule. "
18+
}
19+
20+
variable "enable" {
21+
default = false
22+
description = "Conditionally enables this module (and all it's ressources)."
23+
}
24+
25+
variable "event_pattern" {
26+
default = ""
27+
description = "(Required, if schedule_expression isn't specified) Event pattern described a JSON object."
28+
}
29+
30+
variable "is_enabled" {
31+
default = true
32+
description = "Whether the rule should be enabled (defaults to true)."
33+
}
34+
35+
variable "name" {
36+
default = ""
37+
description = "The rule's name. By default generated by Terraform."
38+
}
39+
40+
variable "name_prefix" {
41+
default = ""
42+
description = "The rule's name. Conflicts with name."
43+
}
44+
45+
variable "schedule_expression" {
46+
default = ""
47+
description = "(Required, if event_pattern isn't specified) Scheduling expression for triggering the Lambda Function using CloudWatch events. For example, cron(0 20 * * ? *) or rate(5 minutes)."
48+
}
49+

0 commit comments

Comments
 (0)