Skip to content

Commit c5d65ee

Browse files
authored
chore: Added examples to show CloudWatch Event Rule as triggers (terraform-aws-modules#126)
1 parent 3c89d13 commit c5d65ee

File tree

7 files changed

+249
-0
lines changed

7 files changed

+249
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ Q4: What does this error mean - `"We currently do not support adding policies fo
564564
* [With EFS](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/with-efs) - Create Lambda Function with Elastic File System attached (Terraform 0.13+ is recommended).
565565
* [Multiple regions](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/multiple-regions) - Create the same Lambda Function in multiple regions with non-conflicting IAM roles and policies.
566566
* [Event Source Mapping](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/event-source-mapping) - Create Lambda Function with event source mapping configuration (SQS, DynamoDB, and Kinesis).
567+
* [Triggers](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/triggers) - Create Lambda Function with some triggers (eg, Cloudwatch Events, EventBridge).
567568

568569

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

examples/triggers/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
builds/*

examples/triggers/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Triggers of AWS Lambda examples
2+
3+
Configuration in this directory creates AWS Lambda Function with some triggers (eg. CloudWatch Events).
4+
5+
6+
## Usage
7+
8+
To run this example you need to execute:
9+
10+
```bash
11+
$ terraform init
12+
$ terraform plan
13+
$ terraform apply
14+
```
15+
16+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
17+
18+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
19+
## Requirements
20+
21+
| Name | Version |
22+
|------|---------|
23+
| terraform | >= 0.12.26 |
24+
| aws | >= 2.67 |
25+
| random | >= 2 |
26+
27+
## Providers
28+
29+
| Name | Version |
30+
|------|---------|
31+
| aws | >= 2.67 |
32+
| random | >= 2 |
33+
34+
## Modules
35+
36+
| Name | Source | Version |
37+
|------|--------|---------|
38+
| lambda_function | ../../ | |
39+
40+
## Resources
41+
42+
| Name |
43+
|------|
44+
| [aws_cloudwatch_event_rule](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule) |
45+
| [aws_cloudwatch_event_target](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target) |
46+
| [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) |
47+
48+
## Inputs
49+
50+
No input.
51+
52+
## Outputs
53+
54+
| Name | Description |
55+
|------|-------------|
56+
| lambda\_cloudwatch\_log\_group\_arn | The ARN of the Cloudwatch Log Group |
57+
| lambda\_role\_arn | The ARN of the IAM role created for the Lambda Function |
58+
| lambda\_role\_name | The name of the IAM role created for the Lambda Function |
59+
| local\_filename | The filename of zip archive deployed (if deployment was from local) |
60+
| s3\_object | The map with S3 object data of zip archive deployed (if deployment was from S3) |
61+
| this\_lambda\_function\_arn | The ARN of the Lambda Function |
62+
| this\_lambda\_function\_invoke\_arn | The Invoke ARN of the Lambda Function |
63+
| this\_lambda\_function\_kms\_key\_arn | The ARN for the KMS encryption key of Lambda Function |
64+
| this\_lambda\_function\_last\_modified | The date Lambda Function resource was last modified |
65+
| this\_lambda\_function\_name | The name of the Lambda Function |
66+
| this\_lambda\_function\_qualified\_arn | The ARN identifying your Lambda Function Version |
67+
| this\_lambda\_function\_source\_code\_hash | Base64-encoded representation of raw SHA-256 sum of the zip file |
68+
| this\_lambda\_function\_source\_code\_size | The size in bytes of the function .zip file |
69+
| this\_lambda\_function\_version | Latest published version of Lambda Function |
70+
| this\_lambda\_layer\_arn | The ARN of the Lambda Layer with version |
71+
| this\_lambda\_layer\_created\_date | The date Lambda Layer resource was created |
72+
| this\_lambda\_layer\_layer\_arn | The ARN of the Lambda Layer without version |
73+
| this\_lambda\_layer\_source\_code\_size | The size in bytes of the Lambda Layer .zip file |
74+
| this\_lambda\_layer\_version | The Lambda Layer version |
75+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/triggers/main.tf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
provider "aws" {
2+
region = "eu-west-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+
##########################################
13+
# Lambda Function (with various triggers)
14+
##########################################
15+
16+
module "lambda_function" {
17+
source = "../../"
18+
19+
function_name = "${random_pet.this.id}-lambda-triggers"
20+
description = "My awesome lambda function"
21+
handler = "index.lambda_handler"
22+
runtime = "python3.8"
23+
publish = true
24+
25+
create_package = false
26+
local_existing_package = "${path.module}/../fixtures/python3.8-zip/existing_package.zip"
27+
28+
allowed_triggers = {
29+
ScanAmiRule = {
30+
principal = "events.amazonaws.com"
31+
source_arn = aws_cloudwatch_event_rule.scan_ami.arn
32+
}
33+
}
34+
}
35+
36+
##################
37+
# Extra resources
38+
##################
39+
40+
resource "random_pet" "this" {
41+
length = 2
42+
}
43+
44+
##################################
45+
# Cloudwatch Events (EventBridge)
46+
##################################
47+
resource "aws_cloudwatch_event_rule" "scan_ami" {
48+
name = "EC2CreateImageEvent"
49+
description = "EC2 Create Image Event..."
50+
event_pattern = <<EOF
51+
{
52+
"source": ["aws.ec2"],
53+
"detail-type": ["AWS API Call via CloudTrail"],
54+
"detail": {
55+
"eventSource": ["ec2.amazonaws.com"],
56+
"eventName": ["CreateImage"]
57+
}
58+
}
59+
EOF
60+
}
61+
62+
resource "aws_cloudwatch_event_target" "scan_ami_lambda_function" {
63+
rule = aws_cloudwatch_event_rule.scan_ami.name
64+
arn = module.lambda_function.this_lambda_function_arn
65+
}

examples/triggers/outputs.tf

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Lambda Function
2+
output "this_lambda_function_arn" {
3+
description = "The ARN of the Lambda Function"
4+
value = module.lambda_function.this_lambda_function_arn
5+
}
6+
7+
output "this_lambda_function_invoke_arn" {
8+
description = "The Invoke ARN of the Lambda Function"
9+
value = module.lambda_function.this_lambda_function_invoke_arn
10+
}
11+
12+
output "this_lambda_function_name" {
13+
description = "The name of the Lambda Function"
14+
value = module.lambda_function.this_lambda_function_name
15+
}
16+
17+
output "this_lambda_function_qualified_arn" {
18+
description = "The ARN identifying your Lambda Function Version"
19+
value = module.lambda_function.this_lambda_function_qualified_arn
20+
}
21+
22+
output "this_lambda_function_version" {
23+
description = "Latest published version of Lambda Function"
24+
value = module.lambda_function.this_lambda_function_version
25+
}
26+
27+
output "this_lambda_function_last_modified" {
28+
description = "The date Lambda Function resource was last modified"
29+
value = module.lambda_function.this_lambda_function_last_modified
30+
}
31+
32+
output "this_lambda_function_kms_key_arn" {
33+
description = "The ARN for the KMS encryption key of Lambda Function"
34+
value = module.lambda_function.this_lambda_function_kms_key_arn
35+
}
36+
37+
output "this_lambda_function_source_code_hash" {
38+
description = "Base64-encoded representation of raw SHA-256 sum of the zip file"
39+
value = module.lambda_function.this_lambda_function_source_code_hash
40+
}
41+
42+
output "this_lambda_function_source_code_size" {
43+
description = "The size in bytes of the function .zip file"
44+
value = module.lambda_function.this_lambda_function_source_code_size
45+
}
46+
47+
# Lambda Layer
48+
output "this_lambda_layer_arn" {
49+
description = "The ARN of the Lambda Layer with version"
50+
value = module.lambda_function.this_lambda_layer_arn
51+
}
52+
53+
output "this_lambda_layer_layer_arn" {
54+
description = "The ARN of the Lambda Layer without version"
55+
value = module.lambda_function.this_lambda_layer_layer_arn
56+
}
57+
58+
output "this_lambda_layer_created_date" {
59+
description = "The date Lambda Layer resource was created"
60+
value = module.lambda_function.this_lambda_layer_created_date
61+
}
62+
63+
output "this_lambda_layer_source_code_size" {
64+
description = "The size in bytes of the Lambda Layer .zip file"
65+
value = module.lambda_function.this_lambda_layer_source_code_size
66+
}
67+
68+
output "this_lambda_layer_version" {
69+
description = "The Lambda Layer version"
70+
value = module.lambda_function.this_lambda_layer_version
71+
}
72+
73+
# IAM Role
74+
output "lambda_role_arn" {
75+
description = "The ARN of the IAM role created for the Lambda Function"
76+
value = module.lambda_function.lambda_role_arn
77+
}
78+
79+
output "lambda_role_name" {
80+
description = "The name of the IAM role created for the Lambda Function"
81+
value = module.lambda_function.lambda_role_name
82+
}
83+
84+
# CloudWatch Log Group
85+
output "lambda_cloudwatch_log_group_arn" {
86+
description = "The ARN of the Cloudwatch Log Group"
87+
value = module.lambda_function.lambda_cloudwatch_log_group_arn
88+
}
89+
90+
# Deployment package
91+
output "local_filename" {
92+
description = "The filename of zip archive deployed (if deployment was from local)"
93+
value = module.lambda_function.local_filename
94+
}
95+
96+
output "s3_object" {
97+
description = "The map with S3 object data of zip archive deployed (if deployment was from S3)"
98+
value = module.lambda_function.s3_object
99+
}

examples/triggers/variables.tf

Whitespace-only changes.

examples/triggers/versions.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_version = ">= 0.12.26"
3+
4+
required_providers {
5+
aws = ">= 2.67"
6+
random = ">= 2"
7+
}
8+
}

0 commit comments

Comments
 (0)