I'm talking inside the company, "Ramu but Ramu," and for me, a former C #, is it something of a programming language? I thought, but it wasn't. (There is a lambda expression in C #.)
Internally, I placed the node.js runtime on AWS Lambda and notified slack, but I was told that the version of node is old and I will stop supporting it soon, so I decided to switch to Python. (Because there was a person in the company who knew Python better than node)
In AWS Lambda, common processing is provided as a template, so it is fairly easy to do. I will summarize the procedure.
Roughly follow the steps below.
The final result will be something like this.
Create the main Lambda function this time. You can easily make it by following the instructions. Rest assured that you can easily recreate or delete it.
Select Lambda from the service list.
Start creating your Lambda function.
If you enter slack in the filter, you can create a slack template, but this time we will create it with blanks.
You'll set the trigger later, so leave it empty.
SNS = Simple Notification Service. It is a service that receives some processing or status change and notifies you as a message. You can send messages to emails and Lamda like this one. The service itself has not been translated as of April 06, 2017, but I think that there is no problem because it is simple English.
Create an SNS and attach the subscription.
Create Subscriptions.
Select the Lambda function you just created
Set an alarm from the EC2 screen.
Make various settings
The settings are as follows.
This time I set it as follows.
@code
lambda_handler.py
from __future__ import print_function
import boto3
import json
import logging
import os
from urllib2 import Request, urlopen, URLError, HTTPError
#slack settings
SLACK_CHANNEL = os.environ['SLACK_CHANNEL']
HOOK_URL = os.environ['HOOK_URL']
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
logger.info("Event: " + str(event))
message = json.loads(event['Records'][0]['Sns']['Message'])
logger.info("Message: " + str(message))
alarm_name = message['AlarmName']
description = message['AlarmDescription']
new_state = message['NewStateValue']
reason = message['NewStateReason']
if new_state == 'OK':
emoji = ":+1:"
elif new_state == 'ALARM':
emoji = ":exclamation:"
slack_message = {
'channel': SLACK_CHANNEL,
'text': "*%s %s: %s*\n %s\n %s" % (emoji, new_state, alarm_name, description, reason)
}
req = Request(HOOK_URL, json.dumps(slack_message))
try:
response = urlopen(req)
response.read()
logger.info("Message posted to %s", slack_message['channel'])
except HTTPError as e:
logger.error("Request failed: %d %s", e.code, e.reason)
except URLError as e:
logger.error("Server connection failed: %s", e.reason)
@ Environment variable + Arufa
From the Lambda screen, do the following:
This is miso, but I hope you can refer to this for test data.
A notification has been successfully sent to the sandbox channel. It also contains emoji.
I was able to realize slack notification with a simple program. Set the metrics appropriately and enjoy a comfortable AWS life. (Even if you notify me, it doesn't make sense if you look at it ...)
In addition, it seems that only SNS created in the same region can be set from Lambda, so if you want to cross regions, select Lambda from SNS of each region.
At first, I tried to use Lambda environment variables, but I couldn't use it because I got an error around base64. If you can use this, the Lambda function can be easily reused, so I will deal with it properly at the next opportunity.
Recommended Posts