Skip to content

Commit

Permalink
chore: Added example of scheduled events with lambda functions (terra…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslacerdacl authored Nov 17, 2021
1 parent cc66f74 commit 452c21e
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.DS_Store

# Local .terraform directories
**/.terraform/*

Expand All @@ -9,7 +7,6 @@
# .tfstate files
*.tfstate
*.tfstate.*
*.tfplan

# Crash log files
crash.log
Expand All @@ -30,3 +27,6 @@ override.tf.json
# Ignore CLI configuration files
.terraformrc
terraform.rc

# Zip archive
*.zip
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,33 @@ module "eventbridge_with_permissions" {
}
```

### EventBridge with lambda rule schedule

```hcl
module "eventbridge" {
source = "terraform-aws-modules/eventbridge/aws"
create_bus = false
rules = {
crons = {
description = "Trigger for a Lambda"
schedule_expression = "rate(5 minutes)"
}
}
targets = {
crons = [
{
name = "lambda-loves-cron"
arn = "arn:aws:lambda:ap-southeast-1:135367859851:function:resolved-penguin-lambda"
input = jsonencode({"job": "cron-by-rate"})
}
]
}
}
```

### EventBridge API Destination

```hcl
Expand Down Expand Up @@ -284,13 +311,14 @@ module "eventbridge" {

## Examples

* [Complete](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/complete) - Creates EventBridge resources (bus, rules and targets) and connect with SQS queues, Kinesis Stream, Step Function, CloudWatch Logs, and more.
* [Complete](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/complete) - Creates EventBridge resources (bus, rules and targets) and connect with SQS queues, Kinesis Stream, Step Function, CloudWatch Logs, Lambda Functions, and more.
* [HTTP API Gateway](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/api-gateway-event-source) - Creates an integration with HTTP API Gateway as event source.
* [Using Default Bus](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/default-bus) - Creates resources in the `default` bus.
* [Archive](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-archive) - EventBridge Archives resources in various configurations.
* [Permissions](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-permissions) - Controls permissions to EventBridge.
* [ECS Scheduling Events](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-ecs-scheduling) - Use default bus to schedule events on ECS.
* [Lambda Scheduling Events](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-lambda-scheduling) - Trigger Lambda functions on schedule.
* [API Destination](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-api-destination) - Control access to EventBridge using API destinations.
* [ECS Scheduled Events](/~https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-ecs-scheduling) - Use default bus to schedule events on ECS.


<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Expand Down
4 changes: 4 additions & 0 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ Note that this example may create resources which cost money. Run `terraform des
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.19 |
| <a name="provider_null"></a> [null](#provider\_null) | >= 2 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 3 |

## Modules
Expand All @@ -36,6 +38,7 @@ Note that this example may create resources which cost money. Run `terraform des
|------|--------|---------|
| <a name="module_ecs"></a> [ecs](#module\_ecs) | terraform-aws-modules/ecs/aws | ~> 3.0 |
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | |
| <a name="module_lambda"></a> [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | ~> 2.0 |
| <a name="module_step_function"></a> [step\_function](#module\_step\_function) | terraform-aws-modules/step-functions/aws | ~> 2.0 |

## Resources
Expand All @@ -50,6 +53,7 @@ Note that this example may create resources which cost money. Run `terraform des
| [aws_sqs_queue.fifo](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
| [aws_sqs_queue.queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
| [aws_sqs_queue_policy.queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_policy) | resource |
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
| [aws_iam_policy_document.queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

Expand Down
81 changes: 79 additions & 2 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ provider "aws" {
module "eventbridge" {
source = "../../"

bus_name = "${random_pet.this.id}-bus"
create_bus = false

# Some targets are only working with the default bus, so we don't have to create a new one like this:
# bus_name = "${random_pet.this.id}-bus"

attach_tracing_policy = true

Expand Down Expand Up @@ -46,6 +49,10 @@ module "eventbridge" {
event_pattern = jsonencode({ "source" : ["myapp.emails"] })
enabled = true
}
crons = {
description = "Trigger for a Lambda"
schedule_expression = "rate(5 minutes)"
}
}

targets = {
Expand Down Expand Up @@ -104,6 +111,14 @@ module "eventbridge" {
}
}
]

crons = [
{
name = "something-for-cron"
arn = module.lambda.lambda_function_arn
input = jsonencode({ "job" : "crons" })
}
]
}

######################
Expand Down Expand Up @@ -278,7 +293,6 @@ resource "aws_ecs_service" "hello_world" {
name = "hello_world-${random_pet.this.id}"
cluster = module.ecs.ecs_cluster_id
task_definition = aws_ecs_task_definition.hello_world.arn
launch_type = "FARGATE"

desired_count = 1

Expand All @@ -300,3 +314,66 @@ resource "aws_ecs_task_definition" "hello_world" {
]
EOF
}

#############################################
# Using packaged function from Lambda module
#############################################

module "lambda" {
source = "terraform-aws-modules/lambda/aws"
version = "~> 2.0"

function_name = "${random_pet.this.id}-lambda"
handler = "index.lambda_handler"
runtime = "python3.8"

create_package = false
local_existing_package = local.downloaded

create_current_version_allowed_triggers = false
allowed_triggers = {
ScanAmiRule = {
principal = "events.amazonaws.com"
source_arn = module.eventbridge.eventbridge_rule_arns["crons"]
}
}
}

locals {
package_url = "https://raw.githubusercontent.com/terraform-aws-modules/terraform-aws-lambda/master/examples/fixtures/python3.8-zip/existing_package.zip"
downloaded = "downloaded_package_${md5(local.package_url)}.zip"
}

resource "null_resource" "download_package" {
triggers = {
downloaded = local.downloaded
}

provisioner "local-exec" {
command = "curl -L -o ${local.downloaded} ${local.package_url}"
}
}

#######
## Lambda
#######
#module "lambda" {
# source = "terraform-aws-modules/lambda/aws"
# version = "~> 2.0"
#
# function_name = "dev-cron-job"
# description = "Lambda Serverless Job"
# handler = "index.handler"
# runtime = "nodejs14.x"
# timeout = 900
#
# source_path = "../with-lambda-shceduling/lambda"
#}
#
#resource "aws_lambda_permission" "crons_invoke" {
# statement_id = "AllowExecutionFromCloudWatch"
# action = "lambda:InvokeFunction"
# function_name = module.lambda.lambda_function_name
# principal = "events.amazonaws.com"
# source_arn = module.eventbridge.eventbridge_rule_arns.orders
#}
1 change: 1 addition & 0 deletions examples/complete/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ terraform {
required_providers {
aws = ">= 3.19"
random = ">= 3"
null = ">= 2"
}
}
59 changes: 59 additions & 0 deletions examples/with-lambda-scheduling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# EventBridge Lambda & Scheduled Events Example

Configuration in this directory creates EventBridge resource configuration including an Lambda service.

## Usage

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_null"></a> [null](#provider\_null) | >= 2 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 2 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | |
| <a name="module_lambda"></a> [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | ~> 2.0 |

## Resources

| Name | Type |
|------|------|
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |

## Inputs

No inputs.

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_eventbridge_rule_arns"></a> [eventbridge\_rule\_arns](#output\_eventbridge\_rule\_arns) | The EventBridge Rule ARNs |
| <a name="output_lambda_function_arn"></a> [lambda\_function\_arn](#output\_lambda\_function\_arn) | The ARN of the Lambda Function |
| <a name="output_lambda_function_name"></a> [lambda\_function\_name](#output\_lambda\_function\_name) | The name of the Lambda Function |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
80 changes: 80 additions & 0 deletions examples/with-lambda-scheduling/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
skip_requesting_account_id = true
}

module "eventbridge" {
source = "../../"

create_bus = false

rules = {
crons = {
description = "Trigger for a Lambda"
schedule_expression = "rate(5 minutes)"
}
}

targets = {
crons = [
{
name = "lambda-loves-cron"
arn = module.lambda.lambda_function_arn
input = jsonencode({ "job" : "cron-by-rate" })
}
]
}
}

##################
# Extra resources
##################

resource "random_pet" "this" {
length = 2
}

#############################################
# Using packaged function from Lambda module
#############################################

module "lambda" {
source = "terraform-aws-modules/lambda/aws"
version = "~> 2.0"

function_name = "${random_pet.this.id}-lambda"
handler = "index.lambda_handler"
runtime = "python3.8"

create_package = false
local_existing_package = local.downloaded

create_current_version_allowed_triggers = false
allowed_triggers = {
ScanAmiRule = {
principal = "events.amazonaws.com"
source_arn = module.eventbridge.eventbridge_rule_arns["crons"]
}
}
}

locals {
package_url = "https://raw.githubusercontent.com/terraform-aws-modules/terraform-aws-lambda/master/examples/fixtures/python3.8-zip/existing_package.zip"
downloaded = "downloaded_package_${md5(local.package_url)}.zip"
}

resource "null_resource" "download_package" {
triggers = {
downloaded = local.downloaded
}

provisioner "local-exec" {
command = "curl -L -o ${local.downloaded} ${local.package_url}"
}
}
14 changes: 14 additions & 0 deletions examples/with-lambda-scheduling/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "eventbridge_rule_arns" {
description = "The EventBridge Rule ARNs"
value = module.eventbridge.eventbridge_rule_arns
}

output "lambda_function_arn" {
description = "The ARN of the Lambda Function"
value = module.lambda.lambda_function_arn
}

output "lambda_function_name" {
description = "The name of the Lambda Function"
value = module.lambda.lambda_function_name
}
Empty file.
9 changes: 9 additions & 0 deletions examples/with-lambda-scheduling/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 0.13.1"

required_providers {
aws = ">= 3.19"
random = ">= 2"
null = ">= 2"
}
}

0 comments on commit 452c21e

Please sign in to comment.