Skip to content

Commit 24e5d75

Browse files
committed
Update to handle the MAC need to log to more than one slack channel based on message contents.
1 parent ef5d94a commit 24e5d75

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
[![](https://img.shields.io/badge/Available-serverless%20app%20repository-blue.svg)](https://serverlessrepo.aws.amazon.com/#/applications/arn:aws:serverlessrepo:us-east-1:289559741701:applications~lambda-to-slack)
22

3-
# lambda-to-slack
3+
# macnoms-lambda-to-slack
44

5-
This serverless app posts messages to Slack.
5+
This serverless app posts messages to Slack. It uses a set of contains strings to determine which slack channel/webhook
6+
should be used for the messages. This is important because only a single subscriber is allowed for each cloudwatch
7+
log; so logging to more than a single slack channel with a single lambda-to-slack (traditional) lambda was not possible.
68

79
## App Architecture
810

@@ -32,6 +34,10 @@ To get a webhook URL for this application:
3234

3335
1. `SlackUrl` (required) - Webhook URL for integration with Slack
3436
1. `LogLevel` (optional) - Log level for Lambda function logging, e.g., ERROR, INFO, DEBUG, etc. Default: INFO
37+
1. `SlackUrl1` (required) - Webhook URL for integration with Slack (if messages match ContainsString1)
38+
1. `ContainsString1` (required) - A string that, if present in the message, dictates that the message should be logged to SlackUrl2
39+
1. `SlackUrl2` (required) - Webhook URL for integration with Slack (if messages match ContainsString1)
40+
1. `ContainsString2` (required) - A string that, if present in the message, dictates that the message should be logged to SlackUrl2
3541

3642
## App Outputs
3743

src/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44

55
LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO')
66
SLACK_URL = os.getenv('SLACK_URL')
7+
SLACK_URL1 = os.getenv('SLACK_URL1')
8+
SLACK_URL2 = os.getenv('SLACK_URL2')
9+
CONTAINS_STRING1 = os.getenv('CONTAINS_STRING1')
10+
CONTAINS_STRING2 = os.getenv('CONTAINS_STRING2')

src/handlers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ def post_to_slack(event, context):
1919
raise InputError(event, "Input needs to be a json array")
2020

2121
for message in event:
22-
slack.post_message(config.SLACK_URL, message)
22+
if config.CONTAINS_STRING1 in message:
23+
slack.post_message(config.SLACK_URL1, message)
24+
elif config.CONTAINS_STRING2 in message:
25+
slack.post_message(config.SLACK_URL2, message)
26+
else:
27+
slack.post_message(config.SLACK_URL, message)

template.yml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,40 @@ Transform: AWS::Serverless-2016-10-31
33

44
Metadata:
55
AWS::ServerlessRepo::Application:
6-
Name: lambda-to-slack
6+
Name: macnoms-lambda-to-slack
77
Description: Posts messages to a Slack channel.
88
Author: Keeton Hodgson
99
SpdxLicenseId: MIT
1010
# paths are relative to .aws-sam/build directory
1111
LicenseUrl: ../../LICENSE
1212
ReadmeUrl: ../../README.md
1313
Labels: [serverless,slack,lambda]
14-
HomePageUrl: https://github.com/keetonian/lambda-to-slack
14+
HomePageUrl: https://github.com/chander/macnoms-lambda-to-slack
1515
# Update the semantic version and run sam publish to publish a new version of your app
16-
SemanticVersion: 0.0.1
16+
SemanticVersion: 0.0.2
1717
# best practice is to use git tags for each release and link to the version tag as your source code URL
18-
SourceCodeUrl: https://github.com/keetonian/lambda-to-slack/tree/0.0.1
18+
SourceCodeUrl: https://github.com/chander/macnoms-lambda-to-slack/tree/0.0.2
1919

2020
Parameters:
2121
LogLevel:
2222
Type: String
2323
Description: Log level for Lambda function logging, e.g., ERROR, INFO, DEBUG, etc
2424
Default: INFO
25+
SlackUrl1:
26+
Description: Webhook URL for integration with Slack (using ContainsString1)
27+
Type: String
28+
ContainsString1:
29+
Description: A String that must appear in the message in order for SlackUrl1 to be used.
30+
SlackUrl2:
31+
Description: Webhook URL for integration with Slack (using ContainsString2)
32+
Type: String
33+
ContainsString2:
34+
Description: A String that must appear in the message in order for SlackUrl2 to be used.
2535
SlackUrl:
26-
Description: Webhook URL for integration with Slack
36+
Description: Webhook URL for integration with Slack (for messages not matched yet)
2737
Type: String
2838

39+
2940
Globals:
3041
Function:
3142
Runtime: python3.7

0 commit comments

Comments
 (0)