diff --git a/.gitignore b/.gitignore index 35424755082c774001728ae21f34232b274be88f..62ef93bff19f32288bc17934650351cd413b4569 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ terraform.rc *.zip test/ -**/node_modules/* \ No newline at end of file +**/node_modules/* +.vscode/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 27e61ea2367f962aa176fa25af38c294dd9c298b..9ca5a9d1fb69e1cb447077a30fb61f55955a64fc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/deploy/cloudwatch_alarms.tf b/deploy/cloudwatch_alarms.tf index dd7d2233426ce73a1622d9f2c6cc348408b1e5e5..2aaadeb63c2f4e32eb39bf0f8182204e87467ea1 100644 --- a/deploy/cloudwatch_alarms.tf +++ b/deploy/cloudwatch_alarms.tf @@ -1,5 +1,5 @@ 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 diff --git a/deploy/lambda/slack/slack.py b/deploy/lambda/slack/slack.py new file mode 100644 index 0000000000000000000000000000000000000000..50c0ac5b030a99e042f9c3662add3c60bd5c0145 --- /dev/null +++ b/deploy/lambda/slack/slack.py @@ -0,0 +1,29 @@ +#!/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 diff --git a/deploy/variables.tf b/deploy/variables.tf index ead798a891bcccfcb3ac0bf247de8b2a74e66871..2cb2f1aa25838b3b3157512e380b2b5a17cf9d83 100644 --- a/deploy/variables.tf +++ b/deploy/variables.tf @@ -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 +}