Skip to content

Commit

Permalink
feat: Added support for custom lambda function (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobi3ch committed Jun 14, 2022
1 parent a8dae23 commit 4a9d0b0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Doing serverless with Terraform? Check out [serverless.tf framework](https://ser
- Create new SNS topic or use existing one
- Support plaintext and encrypted version of Slack webhook URL
- Most of Slack message options are customizable
- Custom Lambda function
- Various event types are supported, even generic messages:
- AWS CloudWatch Alarms
- AWS CloudWatch LogMetrics Alarms
Expand Down Expand Up @@ -114,6 +115,7 @@ See the [functions](https://github.com/terraform-aws-modules/terraform-aws-notif
| <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 |
| <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 |
| <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 |
| <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 |
| <a name="input_log_events"></a> [log\_events](#input\_log\_events) | Boolean flag to enabled/disable logging of incoming events | `bool` | `false` | no |
| <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 |
| <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 |
Expand Down
2 changes: 2 additions & 0 deletions examples/notify-slack-simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ Note that this example may create resources which can cost money (AWS Elastic IP

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

## Resources

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

Expand Down
36 changes: 36 additions & 0 deletions examples/notify-slack-simple/custom-lambda.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
locals {
custom = {
name = "ex-${replace(basename(path.cwd), "_", "-")}-custom"
tags = merge({ "Type" = "custom" }, local.tags)
}
}

################################################################################
# Supporting Resources
################################################################################

resource "aws_sns_topic" "custom_lambda" {
name = local.custom.name
tags = local.custom.tags
}

################################################################################
# Slack Notify Module
################################################################################

module "custom_lambda" {
source = "../../"

lambda_function_name = "custom_lambda"
lambda_source_path = "../../functions/mylambda.py"

iam_role_name_prefix = "custom"

sns_topic_name = aws_sns_topic.custom_lambda.name

slack_webhook_url = "https://hooks.slack.com/services/AAA/BBB/CCC"
slack_channel = "aws-notification"
slack_username = "reporter"

tags = local.custom.tags
}
25 changes: 25 additions & 0 deletions functions/mylambda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/python3.6
# CUSTOM LAMBDA FUNCTION

import urllib3
import json
import os
http = urllib3.PoolManager()


def lambda_handler(event, context):
url = os.environ["SLACK_WEBHOOK_URL"]
msg = {
"channel": "#channel-name",
"username": "Prometheus",
"text": event['Records'][0]['Sns']['Message'],
"icon_emoji": ""
}

encoded_msg = json.dumps(msg).encode('utf-8')
resp = http.request('POST', url, body=encoded_msg)
print({
"message": event['Records'][0]['Sns']['Message'],
"status_code": resp.status,
"response": resp.data
})
6 changes: 4 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ locals {
actions = ["kms:Decrypt"]
resources = [var.kms_key_arn]
}

lambda_handler = try(split(".", basename(var.lambda_source_path))[0], "notify_slack")
}

data "aws_iam_policy_document" "lambda" {
Expand Down Expand Up @@ -77,8 +79,8 @@ module "lambda" {
function_name = var.lambda_function_name
description = var.lambda_description

handler = "notify_slack.lambda_handler"
source_path = "${path.module}/functions/notify_slack.py"
handler = "${local.lambda_handler}.lambda_handler"
source_path = var.lambda_source_path != null ? "${path.root}/${var.lambda_source_path}" : "${path.module}/functions/notify_slack.py"
recreate_missing_package = var.recreate_missing_package
runtime = "python3.8"
timeout = 30
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ variable "lambda_description" {
default = null
}

variable "lambda_source_path" {
description = "The source path of the custom Lambda function"
type = string
default = null
}

variable "sns_topic_name" {
description = "The name of the SNS topic to create"
type = string
Expand Down

0 comments on commit 4a9d0b0

Please sign in to comment.