Merge pull request #142 feature-NEWDWH2021-659 into develop

This commit is contained in:
朝倉 明日香 2023-01-27 11:16:38 +09:00
commit 5011e5b4bb
8 changed files with 78 additions and 97 deletions

View File

@ -1,37 +0,0 @@
"use-strict";
const request = require("request-promise");
exports.handler = (event, context, callback) => {
const attaches = event.Records.map(evt => {
return {
"fallback":`Notification from mbj-newdwh2021 AWS: ${evt.EventSubscriptionArn}`,
"pretext":`Notification from mbj-newdwh2021 AWS: ${evt.EventSubscriptionArn}`,
"color":"#0000D0",
"fields":[
{
"title": `${evt.Sns.Subject}`,
"value": `${evt.Sns.Timestamp}: ${evt.Sns.Message} (MessageId: ${evt.Sns.MessageId})`,
"short": false
}
]
};
});
request({
method: "POST",
url: process.env.webhookurl,
body: {
attachments: attaches
},
json: true
}).then((body => {
callback(null, `Request succeed. ${body}`);
})).catch((err) => {
callback("failed to request to slack.", {
result: "ERROR",
event: event,
cause: `request failed. ${err}.`
});
})
};

View File

@ -1,15 +0,0 @@
{
"name": "mbj-newdwh2021-staging-noticetoslack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"request": "^2.88.2",
"request-promise": "^4.2.6"
}
}

View File

@ -1,30 +0,0 @@
const zlib = require("zlib");
const aws = require("aws-sdk");
const sns = new aws.SNS({
apiVersion: "2010-03-31",
region: 'ap-northeast-1'
});
exports.handler = function(input, context) {
var payload = new Buffer.from(input.awslogs.data, 'base64');
zlib.gunzip(payload, function(e, result) {
if (e) {
context.fail(e);
} else {
result = JSON.parse(result.toString('UTF-8'));
const publishMessage = {
Subject: `Detect Error(or Warning) in ${result.logGroup}`,
Message: result.logEvents.map((l) => l.message).join('\n'),
TopicArn: process.env.topicArn
};
console.log(publishMessage);
sns.publish(publishMessage, (err, data) => {
console.log(err, data);
if(err){
context.fail(err);
}
});
}
});
};

View File

@ -1,15 +0,0 @@
{
"name": "mbj-newdwh2021-staging-publishfromlog",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.1011.0",
"zlib": "^1.0.5"
}
}

View File

@ -0,0 +1,49 @@
import json
import os
import urllib.request as request
def make_attaches(event):
"""Slackのリクエストボディ attachmentsを作成する"""
attachments = []
for evt in event.get('Records'):
sns_event = evt.get('Sns')
event_subscription_arn = evt.get('EventSubscriptionArn')
subject = sns_event.get('Subject', 'AWS Notification Message')
sns_timestamp = sns_event.get('Timestamp')
message = sns_event.get('Message')
message_id = sns_event.get('MessageId')
attachments.append({
"fallback": f"Notification from mbj-newdwh2021 AWS: {event_subscription_arn}",
"pretext": f"Notification from mbj-newdwh2021 AWS: {event_subscription_arn}",
"color":"#0000D0",
"fields":[
{
"title": f"{subject}",
"value": f"{sns_timestamp}: {message} (MessageId: {message_id})",
"short": False
}
]
})
return attachments
def lambda_handler(event, context):
attachments = make_attaches(event)
request_url = os.environ.get('SLACK_WEBHOOK_URL')
payload = {
'attachments': attachments
}
headers = {
'Content-Type': 'application/json',
}
print(payload)
req = request.Request(request_url, json.dumps(payload, ensure_ascii=False).encode(), headers, method='POST')
with request.urlopen(req) as res:
body = res.read()
print(body)

View File

@ -0,0 +1,29 @@
import base64
import gzip
import json
import os
from io import BytesIO
import boto3
sns_client = boto3.client('sns')
def lambda_handler(event, context):
awslogs_dict = event.get('awslogs')
base64_data = awslogs_dict.get('data')
try:
decoded_gzip_data = base64.b64decode(base64_data)
except Exception as e:
raise e
log_event_str = gzip.GzipFile(fileobj=BytesIO(decoded_gzip_data)).read()
log_event = json.loads(log_event_str)
publish_message = {
'Subject': f'Detect Error(or Warning) in {log_event.get("logGroup")}',
'Message': '\n'.join([log.get('message') for log in log_event.get('logEvents')]),
'TopicArn': os.environ.get('SNS_TOPIC_ARN')
}
print(publish_message)
sns_client.publish(**publish_message)