Skip to content
Snippets Groups Projects
Commit de8f20d7 authored by Edouard DE BRYE's avatar Edouard DE BRYE
Browse files

Added slack endpoint for alerting

parent 02242ddb
No related branches found
No related tags found
No related merge requests found
......@@ -35,4 +35,5 @@ terraform.rc
*.zip
test/
**/node_modules/*
\ No newline at end of file
**/node_modules/*
.vscode/
......@@ -111,4 +111,4 @@ Production Destroy:
- terraform destroy -auto-approve
rules:
- if: '$CI_COMMIT_BRANCH == "production"'
when: manual
\ No newline at end of file
when: manual
data "aws_sns_topic" "cloudmon" {
name = "edebrye-cloudmon"
name = var.sns_topic_name
}
resource "aws_cloudwatch_metric_alarm" "lambda_throttles_alarm" {
......@@ -41,4 +41,46 @@ resource "aws_cloudwatch_metric_alarm" "lambda_error_alarm" {
FunctionName = aws_lambda_function.crud[each.key].function_name
}
tags = local.common_tags
}
resource "aws_lambda_function" "slack" {
filename = data.archive_file.lambda_slack_file.output_path
function_name = "${local.prefix}-slack-messaging"
role = aws_iam_role.iam_for_lambda.arn
handler = "slack.lambda_handler"
timeout = 10
source_code_hash = data.archive_file.lambda_slack_file.output_base64sha256
runtime = "python3.8"
reserved_concurrent_executions = 2
layers = [
"arn:aws:lambda:eu-west-1:580247275435:layer:LambdaInsightsExtension:14"
]
environment {
variables = {
SLACK_WEBHOOK_URL = var.slack_webhook_url
}
}
tags = local.common_tags
}
data "archive_file" "lambda_slack_file" {
type = "zip"
output_path = "${local.lambda_loc}/zip/slack.zip"
source_file = "${local.lambda_loc}/slack/slack.py"
}
resource "aws_sns_topic_subscription" "topic_lambda" {
topic_arn = data.aws_sns_topic.cloudmon.arn
protocol = "lambda"
endpoint = aws_lambda_function.slack.arn
}
resource "aws_lambda_permission" "with_sns" {
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.slack.arn
principal = "sns.amazonaws.com"
source_arn = data.aws_sns_topic.cloudmon.arn
}
\ No newline at end of file
#!/usr/bin/python3.8
import urllib3
import json
import os
http = urllib3.PoolManager()
def lambda_handler(event, context):
url = os.getenv("SLACK_WEBHOOK_URL")
print(json.dumps(event))
messages = [json.loads(i.get('Sns',{}).get('Message',"")) for i in event.get('Records',[])]
text=[]
for m in messages:
text.append("ALARM - Details :\n"+ \
"\t- Name : "+m.get('AlarmName')+"\n"+ \
"\t- Description : "+m.get('AlarmDescription')+"\n"+ \
"\t- New State Value : "+m.get('NewStateValue')+"\n"+ \
"\t- New State Reason : "+m.get('NewStateReason')+"\n"+ \
"\t- New State Time : "+m.get('StateChangeTime')+"\n")
msg = {
"text": "\n-------\n".join(text)
}
encoded_msg = json.dumps(msg).encode('utf-8')
resp = http.request('POST',url, body=encoded_msg)
print(json.dumps({
"messages": messages,
"status_code": resp.status,
"response": resp.data.decode("utf-8")
})
)
\ No newline at end of file
......@@ -18,4 +18,12 @@ variable "aws_region" {
default = "eu-west-1"
}
variable "slack_webhook_url" {
sensitive = true
type = string
}
variable "sns_topic_name" {
default = "edebrye-cloudmon"
type = string
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment