Skip to content

Commit 452c21e

Browse files
chore: Added example of scheduled events with lambda functions (#28)
1 parent cc66f74 commit 452c21e

File tree

10 files changed

+279
-7
lines changed

10 files changed

+279
-7
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.DS_Store
2-
31
# Local .terraform directories
42
**/.terraform/*
53

@@ -9,7 +7,6 @@
97
# .tfstate files
108
*.tfstate
119
*.tfstate.*
12-
*.tfplan
1310

1411
# Crash log files
1512
crash.log
@@ -30,3 +27,6 @@ override.tf.json
3027
# Ignore CLI configuration files
3128
.terraformrc
3229
terraform.rc
30+
31+
# Zip archive
32+
*.zip

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,33 @@ module "eventbridge_with_permissions" {
173173
}
174174
```
175175

176+
### EventBridge with lambda rule schedule
177+
178+
```hcl
179+
module "eventbridge" {
180+
source = "terraform-aws-modules/eventbridge/aws"
181+
182+
create_bus = false
183+
184+
rules = {
185+
crons = {
186+
description = "Trigger for a Lambda"
187+
schedule_expression = "rate(5 minutes)"
188+
}
189+
}
190+
191+
targets = {
192+
crons = [
193+
{
194+
name = "lambda-loves-cron"
195+
arn = "arn:aws:lambda:ap-southeast-1:135367859851:function:resolved-penguin-lambda"
196+
input = jsonencode({"job": "cron-by-rate"})
197+
}
198+
]
199+
}
200+
}
201+
```
202+
176203
### EventBridge API Destination
177204

178205
```hcl
@@ -284,13 +311,14 @@ module "eventbridge" {
284311

285312
## Examples
286313

287-
* [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.
314+
* [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.
288315
* [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.
289316
* [Using Default Bus](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/default-bus) - Creates resources in the `default` bus.
290317
* [Archive](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-archive) - EventBridge Archives resources in various configurations.
291318
* [Permissions](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-permissions) - Controls permissions to EventBridge.
319+
* [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.
320+
* [Lambda Scheduling Events](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-lambda-scheduling) - Trigger Lambda functions on schedule.
292321
* [API Destination](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-api-destination) - Control access to EventBridge using API destinations.
293-
* [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.
294322

295323

296324
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/complete/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ Note that this example may create resources which cost money. Run `terraform des
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
2323
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
24+
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2 |
2425
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3 |
2526

2627
## Providers
2728

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

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

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

examples/complete/main.tf

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ provider "aws" {
1212
module "eventbridge" {
1313
source = "../../"
1414

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

1720
attach_tracing_policy = true
1821

@@ -46,6 +49,10 @@ module "eventbridge" {
4649
event_pattern = jsonencode({ "source" : ["myapp.emails"] })
4750
enabled = true
4851
}
52+
crons = {
53+
description = "Trigger for a Lambda"
54+
schedule_expression = "rate(5 minutes)"
55+
}
4956
}
5057

5158
targets = {
@@ -104,6 +111,14 @@ module "eventbridge" {
104111
}
105112
}
106113
]
114+
115+
crons = [
116+
{
117+
name = "something-for-cron"
118+
arn = module.lambda.lambda_function_arn
119+
input = jsonencode({ "job" : "crons" })
120+
}
121+
]
107122
}
108123

109124
######################
@@ -278,7 +293,6 @@ resource "aws_ecs_service" "hello_world" {
278293
name = "hello_world-${random_pet.this.id}"
279294
cluster = module.ecs.ecs_cluster_id
280295
task_definition = aws_ecs_task_definition.hello_world.arn
281-
launch_type = "FARGATE"
282296

283297
desired_count = 1
284298

@@ -300,3 +314,66 @@ resource "aws_ecs_task_definition" "hello_world" {
300314
]
301315
EOF
302316
}
317+
318+
#############################################
319+
# Using packaged function from Lambda module
320+
#############################################
321+
322+
module "lambda" {
323+
source = "terraform-aws-modules/lambda/aws"
324+
version = "~> 2.0"
325+
326+
function_name = "${random_pet.this.id}-lambda"
327+
handler = "index.lambda_handler"
328+
runtime = "python3.8"
329+
330+
create_package = false
331+
local_existing_package = local.downloaded
332+
333+
create_current_version_allowed_triggers = false
334+
allowed_triggers = {
335+
ScanAmiRule = {
336+
principal = "events.amazonaws.com"
337+
source_arn = module.eventbridge.eventbridge_rule_arns["crons"]
338+
}
339+
}
340+
}
341+
342+
locals {
343+
package_url = "https://raw.githubusercontent.com/terraform-aws-modules/terraform-aws-lambda/master/examples/fixtures/python3.8-zip/existing_package.zip"
344+
downloaded = "downloaded_package_${md5(local.package_url)}.zip"
345+
}
346+
347+
resource "null_resource" "download_package" {
348+
triggers = {
349+
downloaded = local.downloaded
350+
}
351+
352+
provisioner "local-exec" {
353+
command = "curl -L -o ${local.downloaded} ${local.package_url}"
354+
}
355+
}
356+
357+
#######
358+
## Lambda
359+
#######
360+
#module "lambda" {
361+
# source = "terraform-aws-modules/lambda/aws"
362+
# version = "~> 2.0"
363+
#
364+
# function_name = "dev-cron-job"
365+
# description = "Lambda Serverless Job"
366+
# handler = "index.handler"
367+
# runtime = "nodejs14.x"
368+
# timeout = 900
369+
#
370+
# source_path = "../with-lambda-shceduling/lambda"
371+
#}
372+
#
373+
#resource "aws_lambda_permission" "crons_invoke" {
374+
# statement_id = "AllowExecutionFromCloudWatch"
375+
# action = "lambda:InvokeFunction"
376+
# function_name = module.lambda.lambda_function_name
377+
# principal = "events.amazonaws.com"
378+
# source_arn = module.eventbridge.eventbridge_rule_arns.orders
379+
#}

examples/complete/versions.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ terraform {
44
required_providers {
55
aws = ">= 3.19"
66
random = ">= 3"
7+
null = ">= 2"
78
}
89
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# EventBridge Lambda & Scheduled Events Example
2+
3+
Configuration in this directory creates EventBridge resource configuration including an Lambda service.
4+
5+
## Usage
6+
7+
To run this example you need to execute:
8+
9+
```bash
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Requirements
19+
20+
| Name | Version |
21+
|------|---------|
22+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
24+
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2 |
25+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2 |
26+
27+
## Providers
28+
29+
| Name | Version |
30+
|------|---------|
31+
| <a name="provider_null"></a> [null](#provider\_null) | >= 2 |
32+
| <a name="provider_random"></a> [random](#provider\_random) | >= 2 |
33+
34+
## Modules
35+
36+
| Name | Source | Version |
37+
|------|--------|---------|
38+
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | |
39+
| <a name="module_lambda"></a> [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | ~> 2.0 |
40+
41+
## Resources
42+
43+
| Name | Type |
44+
|------|------|
45+
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
46+
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
47+
48+
## Inputs
49+
50+
No inputs.
51+
52+
## Outputs
53+
54+
| Name | Description |
55+
|------|-------------|
56+
| <a name="output_eventbridge_rule_arns"></a> [eventbridge\_rule\_arns](#output\_eventbridge\_rule\_arns) | The EventBridge Rule ARNs |
57+
| <a name="output_lambda_function_arn"></a> [lambda\_function\_arn](#output\_lambda\_function\_arn) | The ARN of the Lambda Function |
58+
| <a name="output_lambda_function_name"></a> [lambda\_function\_name](#output\_lambda\_function\_name) | The name of the Lambda Function |
59+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
provider "aws" {
2+
region = "ap-southeast-1"
3+
4+
# Make it faster by skipping something
5+
skip_get_ec2_platforms = true
6+
skip_metadata_api_check = true
7+
skip_region_validation = true
8+
skip_credentials_validation = true
9+
skip_requesting_account_id = true
10+
}
11+
12+
module "eventbridge" {
13+
source = "../../"
14+
15+
create_bus = false
16+
17+
rules = {
18+
crons = {
19+
description = "Trigger for a Lambda"
20+
schedule_expression = "rate(5 minutes)"
21+
}
22+
}
23+
24+
targets = {
25+
crons = [
26+
{
27+
name = "lambda-loves-cron"
28+
arn = module.lambda.lambda_function_arn
29+
input = jsonencode({ "job" : "cron-by-rate" })
30+
}
31+
]
32+
}
33+
}
34+
35+
##################
36+
# Extra resources
37+
##################
38+
39+
resource "random_pet" "this" {
40+
length = 2
41+
}
42+
43+
#############################################
44+
# Using packaged function from Lambda module
45+
#############################################
46+
47+
module "lambda" {
48+
source = "terraform-aws-modules/lambda/aws"
49+
version = "~> 2.0"
50+
51+
function_name = "${random_pet.this.id}-lambda"
52+
handler = "index.lambda_handler"
53+
runtime = "python3.8"
54+
55+
create_package = false
56+
local_existing_package = local.downloaded
57+
58+
create_current_version_allowed_triggers = false
59+
allowed_triggers = {
60+
ScanAmiRule = {
61+
principal = "events.amazonaws.com"
62+
source_arn = module.eventbridge.eventbridge_rule_arns["crons"]
63+
}
64+
}
65+
}
66+
67+
locals {
68+
package_url = "https://raw.githubusercontent.com/terraform-aws-modules/terraform-aws-lambda/master/examples/fixtures/python3.8-zip/existing_package.zip"
69+
downloaded = "downloaded_package_${md5(local.package_url)}.zip"
70+
}
71+
72+
resource "null_resource" "download_package" {
73+
triggers = {
74+
downloaded = local.downloaded
75+
}
76+
77+
provisioner "local-exec" {
78+
command = "curl -L -o ${local.downloaded} ${local.package_url}"
79+
}
80+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "eventbridge_rule_arns" {
2+
description = "The EventBridge Rule ARNs"
3+
value = module.eventbridge.eventbridge_rule_arns
4+
}
5+
6+
output "lambda_function_arn" {
7+
description = "The ARN of the Lambda Function"
8+
value = module.lambda.lambda_function_arn
9+
}
10+
11+
output "lambda_function_name" {
12+
description = "The name of the Lambda Function"
13+
value = module.lambda.lambda_function_name
14+
}

examples/with-lambda-scheduling/variables.tf

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 0.13.1"
3+
4+
required_providers {
5+
aws = ">= 3.19"
6+
random = ">= 2"
7+
null = ">= 2"
8+
}
9+
}

0 commit comments

Comments
 (0)