This time, I will introduce one of the notification methods that can be used when operating using AWS services that I often touch.
We will actually implement it according to the above flow.
__ Step 1 __
This time, the string "test" is output to CloudWatch Logs.
__ Step 2 __
We will use the CloudWatch Logs subscription filter (Lambda) for string detection.
__ Step 3 __
Set the Lambda function for notification, log format and filter pattern
__ Step 4 __
The environment variable "Webhook URL" is set to the slack Webhook URL you want to notify.
notifi.py
import json
import os
import urllib.request
import slackweb
def lambda_handler(event, context):
decoded_data = zlib.decompress(
base64.b64decode(event['awslogs']['data']),
16+zlib.MAX_WBITS
)
json_data = json.loads(decoded_data)
print(json_data['logEvents'])
for i in json_data['logEvents']:
test = i['message'] #Log contents
slack = slackweb.Slack(url=os.environ['WebhookURL'])
slack.notify(text=test)
I confirmed that it detects the character string set earlier and notifies slack.
There are multiple notification methods other than the above, but it is easier to manage if you summarize them as concisely as possible, so I would like to make it easier and easier to simplify.
Recommended Posts