Skip to content

Commit 4a9d0b0

Browse files
authored
feat: Added support for custom lambda function (#172)
1 parent a8dae23 commit 4a9d0b0

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Doing serverless with Terraform? Check out [serverless.tf framework](https://ser
1212
- Create new SNS topic or use existing one
1313
- Support plaintext and encrypted version of Slack webhook URL
1414
- Most of Slack message options are customizable
15+
- Custom Lambda function
1516
- Various event types are supported, even generic messages:
1617
- AWS CloudWatch Alarms
1718
- AWS CloudWatch LogMetrics Alarms
@@ -114,6 +115,7 @@ See the [functions](https://github.com/terraform-aws-modules/terraform-aws-notif
114115
| <a name="input_lambda_function_vpc_security_group_ids"></a> [lambda\_function\_vpc\_security\_group\_ids](#input\_lambda\_function\_vpc\_security\_group\_ids) | List of security group ids when Lambda Function should run in the VPC. | `list(string)` | `null` | no |
115116
| <a name="input_lambda_function_vpc_subnet_ids"></a> [lambda\_function\_vpc\_subnet\_ids](#input\_lambda\_function\_vpc\_subnet\_ids) | List of subnet ids when Lambda Function should run in the VPC. Usually private or intra subnets. | `list(string)` | `null` | no |
116117
| <a name="input_lambda_role"></a> [lambda\_role](#input\_lambda\_role) | IAM role attached to the Lambda Function. If this is set then a role will not be created for you. | `string` | `""` | no |
118+
| <a name="input_lambda_source_path"></a> [lambda\_source\_path](#input\_lambda\_source\_path) | The source path of the custom Lambda function | `string` | `null` | no |
117119
| <a name="input_log_events"></a> [log\_events](#input\_log\_events) | Boolean flag to enabled/disable logging of incoming events | `bool` | `false` | no |
118120
| <a name="input_recreate_missing_package"></a> [recreate\_missing\_package](#input\_recreate\_missing\_package) | Whether to recreate missing Lambda package if it is missing locally or not | `bool` | `true` | no |
119121
| <a name="input_reserved_concurrent_executions"></a> [reserved\_concurrent\_executions](#input\_reserved\_concurrent\_executions) | The amount of reserved concurrent executions for this lambda function. A value of 0 disables lambda from being triggered and -1 removes any concurrency limitations | `number` | `-1` | no |

examples/notify-slack-simple/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ Note that this example may create resources which can cost money (AWS Elastic IP
3838

3939
| Name | Source | Version |
4040
|------|--------|---------|
41+
| <a name="module_custom_lambda"></a> [custom\_lambda](#module\_custom\_lambda) | ../../ | n/a |
4142
| <a name="module_notify_slack"></a> [notify\_slack](#module\_notify\_slack) | ../../ | n/a |
4243

4344
## Resources
4445

4546
| Name | Type |
4647
|------|------|
48+
| [aws_sns_topic.custom_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
4749
| [aws_sns_topic.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
4850
| [local_file.integration_testing](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
4951

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
locals {
2+
custom = {
3+
name = "ex-${replace(basename(path.cwd), "_", "-")}-custom"
4+
tags = merge({ "Type" = "custom" }, local.tags)
5+
}
6+
}
7+
8+
################################################################################
9+
# Supporting Resources
10+
################################################################################
11+
12+
resource "aws_sns_topic" "custom_lambda" {
13+
name = local.custom.name
14+
tags = local.custom.tags
15+
}
16+
17+
################################################################################
18+
# Slack Notify Module
19+
################################################################################
20+
21+
module "custom_lambda" {
22+
source = "../../"
23+
24+
lambda_function_name = "custom_lambda"
25+
lambda_source_path = "../../functions/mylambda.py"
26+
27+
iam_role_name_prefix = "custom"
28+
29+
sns_topic_name = aws_sns_topic.custom_lambda.name
30+
31+
slack_webhook_url = "https://hooks.slack.com/services/AAA/BBB/CCC"
32+
slack_channel = "aws-notification"
33+
slack_username = "reporter"
34+
35+
tags = local.custom.tags
36+
}

functions/mylambda.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python3.6
2+
# CUSTOM LAMBDA FUNCTION
3+
4+
import urllib3
5+
import json
6+
import os
7+
http = urllib3.PoolManager()
8+
9+
10+
def lambda_handler(event, context):
11+
url = os.environ["SLACK_WEBHOOK_URL"]
12+
msg = {
13+
"channel": "#channel-name",
14+
"username": "Prometheus",
15+
"text": event['Records'][0]['Sns']['Message'],
16+
"icon_emoji": ""
17+
}
18+
19+
encoded_msg = json.dumps(msg).encode('utf-8')
20+
resp = http.request('POST', url, body=encoded_msg)
21+
print({
22+
"message": event['Records'][0]['Sns']['Message'],
23+
"status_code": resp.status,
24+
"response": resp.data
25+
})

main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ locals {
2222
actions = ["kms:Decrypt"]
2323
resources = [var.kms_key_arn]
2424
}
25+
26+
lambda_handler = try(split(".", basename(var.lambda_source_path))[0], "notify_slack")
2527
}
2628

2729
data "aws_iam_policy_document" "lambda" {
@@ -77,8 +79,8 @@ module "lambda" {
7779
function_name = var.lambda_function_name
7880
description = var.lambda_description
7981

80-
handler = "notify_slack.lambda_handler"
81-
source_path = "${path.module}/functions/notify_slack.py"
82+
handler = "${local.lambda_handler}.lambda_handler"
83+
source_path = var.lambda_source_path != null ? "${path.root}/${var.lambda_source_path}" : "${path.module}/functions/notify_slack.py"
8284
recreate_missing_package = var.recreate_missing_package
8385
runtime = "python3.8"
8486
timeout = 30

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ variable "lambda_description" {
2828
default = null
2929
}
3030

31+
variable "lambda_source_path" {
32+
description = "The source path of the custom Lambda function"
33+
type = string
34+
default = null
35+
}
36+
3137
variable "sns_topic_name" {
3238
description = "The name of the SNS topic to create"
3339
type = string

0 commit comments

Comments
 (0)