Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Terraform module to create AWS [Lambda](https://www.terraform.io/docs/providers/

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

- [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
- [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
- [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
- [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
- [s3](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-s3-event): configures permission to trigger the Lambda by S3
Expand Down Expand Up @@ -46,7 +46,7 @@ module "lambda" {

// configurable event trigger, see examples
event = {
type = "cloudwatch-scheduled-event"
type = "cloudwatch-event"
schedule_expression = "rate(1 minute)"
}

Expand Down Expand Up @@ -74,7 +74,7 @@ module "lambda" {

### Examples

- [example-with-cloudwatch-scheduled-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-cloudwatch-scheduled-event)
- [example-with-cloudwatch-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-cloudwatch-event)
- [example-with-dynamodb-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-dynamodb-event)
- [example-with-kinesis-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-kinesis-event)
- [example-with-s3-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-s3-event)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Example with CloudWatch scheduled event
# Example with CloudWatch events

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

## requirements

Expand Down
37 changes: 37 additions & 0 deletions examples/example-with-cloudwatch-event/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
provider "aws" {
region = "eu-west-1"
}

module "lambda-scheduled" {
source = "../../"
description = "Example AWS Lambda using go with cloudwatch scheduled event trigger"
filename = "${path.module}/test_function.zip"
function_name = "tf-example-go-basic"
handler = "example-lambda-func"
runtime = "go1.x"

event = {
type = "cloudwatch-event"
schedule_expression = "rate(1 minute)"
}
}

module "lambda-pattern" {
source = "../../"
description = "Example AWS Lambda using go with cloudwatch event pattern trigger"
filename = "${path.module}/test_function.zip"
function_name = "tf-example-go-basic"
handler = "example-lambda-func"
runtime = "go1.x"

event = {
type = "cloudwatch-event"
event_pattern = <<PATTERN
{
"detail-type": [
"AWS Console Sign In via CloudTrail"
]
}
PATTERN
}
}
27 changes: 0 additions & 27 deletions examples/example-with-cloudwatch-scheduled-event/main.tf

This file was deleted.

11 changes: 8 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ module "lambda" {
vpc_config = var.vpc_config
}

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

lambda_function_arn = module.lambda.arn
description = lookup(var.event, "description", "")
event_pattern = lookup(var.event, "event_pattern", "")
is_enabled = lookup(var.event, "is_enabled", true)
name = lookup(var.event, "name", null)
name_prefix = lookup(var.event, "name_prefix", null)
schedule_expression = lookup(var.event, "schedule_expression", "")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ resource "aws_lambda_permission" "cloudwatch" {

resource "aws_cloudwatch_event_rule" "lambda" {
count = var.enable ? 1 : 0
description = var.description
event_pattern = var.event_pattern
is_enabled = var.is_enabled
name = var.name
name_prefix = var.name_prefix
schedule_expression = var.schedule_expression
}

Expand Down
49 changes: 49 additions & 0 deletions modules/event/cloudwatch-event/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ---------------------------------------------------------------------------------------------------------------------
# REQUIRED PARAMETERS
# You must provide a value for each of these parameters.
# ---------------------------------------------------------------------------------------------------------------------

variable "lambda_function_arn" {
description = "The Amazon Resource Name (ARN) identifying the Lambda Function trigger by CloudWatch"
}

# ---------------------------------------------------------------------------------------------------------------------
# OPTIONAL PARAMETERS
# These parameters have reasonable defaults.
# ---------------------------------------------------------------------------------------------------------------------

variable "description" {
default = ""
description = "The description of the rule. "
}

variable "enable" {
default = false
description = "Conditionally enables this module (and all it's ressources)."
}

variable "event_pattern" {
default = ""
description = "(Required, if schedule_expression isn't specified) Event pattern described a JSON object."
}

variable "is_enabled" {
default = true
description = "Whether the rule should be enabled (defaults to true)."
}

variable "name" {
default = ""
description = "The rule's name. By default generated by Terraform."
}

variable "name_prefix" {
default = ""
description = "The rule's name. Conflicts with name."
}

variable "schedule_expression" {
default = ""
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)."
}

14 changes: 0 additions & 14 deletions modules/event/cloudwatch-scheduled-event/variables.tf

This file was deleted.