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

Merge branch 'refactor/lambdas-slack-and-apigw' into 'master'

refactor apigw.js and slack.py

See merge request edebrye/cloud-monitor!19
parents 0207c962 e1a4ce85
No related branches found
No related tags found
No related merge requests found
...@@ -42,3 +42,23 @@ provider "registry.terraform.io/hashicorp/aws" { ...@@ -42,3 +42,23 @@ provider "registry.terraform.io/hashicorp/aws" {
"zh:f2b292e813844d6d611db89017fc420ac05f2e3b25324e3c893481d375e23396", "zh:f2b292e813844d6d611db89017fc420ac05f2e3b25324e3c893481d375e23396",
] ]
} }
provider "registry.terraform.io/hashicorp/null" {
version = "3.1.0"
hashes = [
"h1:SFT7X3zY18CLWjoH2GfQyapxsRv6GDKsy9cF1aRwncc=",
"h1:vpC6bgUQoJ0znqIKVFevOdq+YQw42bRq0u+H3nto8nA=",
"h1:xhbHC6in3nQryvTQBWKxebi3inG5OCgHgc4fRxL0ymc=",
"zh:02a1675fd8de126a00460942aaae242e65ca3380b5bb192e8773ef3da9073fd2",
"zh:53e30545ff8926a8e30ad30648991ca8b93b6fa496272cd23b26763c8ee84515",
"zh:5f9200bf708913621d0f6514179d89700e9aa3097c77dac730e8ba6e5901d521",
"zh:9ebf4d9704faba06b3ec7242c773c0fbfe12d62db7d00356d4f55385fc69bfb2",
"zh:a6576c81adc70326e4e1c999c04ad9ca37113a6e925aefab4765e5a5198efa7e",
"zh:a8a42d13346347aff6c63a37cda9b2c6aa5cc384a55b2fe6d6adfa390e609c53",
"zh:c797744d08a5307d50210e0454f91ca4d1c7621c68740441cf4579390452321d",
"zh:cecb6a304046df34c11229f20a80b24b1603960b794d68361a67c5efe58e62b8",
"zh:e1371aa1e502000d9974cfaff5be4cfa02f47b17400005a16f14d2ef30dc2a70",
"zh:fc39cc1fe71234a0b0369d5c5c7f876c71b956d23d7d6f518289737a001ba69b",
"zh:fea4227271ebf7d9e2b61b89ce2328c7262acd9fd190e1fd6d15a591abfa848e",
]
}
...@@ -114,6 +114,7 @@ resource "aws_api_gateway_stage" "main" { ...@@ -114,6 +114,7 @@ resource "aws_api_gateway_stage" "main" {
destination_arn = aws_cloudwatch_log_group.apigw_logs.arn destination_arn = aws_cloudwatch_log_group.apigw_logs.arn
} }
} }
output "api_endpoint" { output "api_endpoint" {
value = aws_api_gateway_stage.main.invoke_url value = aws_api_gateway_stage.main.invoke_url
} }
......
...@@ -22,13 +22,8 @@ resource "aws_lambda_function" "apigw_logs" { ...@@ -22,13 +22,8 @@ resource "aws_lambda_function" "apigw_logs" {
resource "aws_cloudwatch_log_subscription_filter" "lambdafunction_logfilter" { resource "aws_cloudwatch_log_subscription_filter" "lambdafunction_logfilter" {
name = "${local.prefix}-lambdafunction-subscription" name = "${local.prefix}-lambdafunction-subscription"
log_group_name = aws_cloudwatch_log_group.apigw_logs.name log_group_name = aws_cloudwatch_log_group.apigw_logs.name
filter_pattern = " " filter_pattern = "{$.resourcePath = \"${aws_api_gateway_resource.access.path}\"}"
destination_arn = aws_lambda_function.apigw_logs.arn destination_arn = aws_lambda_function.apigw_logs.arn
lifecycle {
ignore_changes = [
filter_pattern
]
}
} }
resource "aws_lambda_permission" "log_subscription" { resource "aws_lambda_permission" "log_subscription" {
...@@ -39,6 +34,18 @@ resource "aws_lambda_permission" "log_subscription" { ...@@ -39,6 +34,18 @@ resource "aws_lambda_permission" "log_subscription" {
source_arn = "${aws_cloudwatch_log_group.apigw_logs.arn}:*" source_arn = "${aws_cloudwatch_log_group.apigw_logs.arn}:*"
} }
resource "null_resource" "lambda_logs_dependencies" {
provisioner "local-exec" {
command = "cd ${local.lambda_loc}/apigw_metrics && npm install"
}
triggers = {
package = base64sha256(file("${local.lambda_loc}/apigw_metrics/package.json"))
lock = base64sha256(file("${local.lambda_loc}/apigw_metrics/package-lock.json"))
node = base64sha256(join("", fileset(local.lambda_loc, "apigw_metrics/**/*.js")))
}
}
data "archive_file" "lambda_logs" { data "archive_file" "lambda_logs" {
type = "zip" type = "zip"
output_path = "${local.lambda_loc}/zip/apigw_metrics.zip" output_path = "${local.lambda_loc}/zip/apigw_metrics.zip"
......
...@@ -83,8 +83,22 @@ resource "aws_iam_role_policy_attachment" "lambda_insights" { ...@@ -83,8 +83,22 @@ resource "aws_iam_role_policy_attachment" "lambda_insights" {
######### #########
# Files # # Files #
######### #########
resource "null_resource" "lambda_dependencies" {
provisioner "local-exec" {
command = "cd ${local.lambda_loc}/src && npm install"
}
triggers = {
package = base64sha256(file("${local.lambda_loc}/src/package.json"))
lock = base64sha256(file("${local.lambda_loc}/src/package-lock.json"))
node = base64sha256(join("", fileset(local.lambda_loc, "src/**/*.js")))
}
}
data "archive_file" "lambda_file" { data "archive_file" "lambda_file" {
depends_on = [
null_resource.lambda_dependencies
]
type = "zip" type = "zip"
output_path = "${local.lambda_loc}/zip/lambda.zip" output_path = "${local.lambda_loc}/zip/lambda.zip"
source_dir = "${local.lambda_loc}/src" source_dir = "${local.lambda_loc}/src"
......
...@@ -4,7 +4,7 @@ const CW = new AWS.CloudWatch() ...@@ -4,7 +4,7 @@ const CW = new AWS.CloudWatch()
const APIGW = new AWS.APIGateway() const APIGW = new AWS.APIGateway()
exports.handler = async function(event,context){ exports.handler = async function(event,context){
return new Promise((resolve,reject)=>{ return new Promise((resolve,reject) => {
console.log("Event: "+JSON.stringify(event)) console.log("Event: "+JSON.stringify(event))
var payload = Buffer.from(event.awslogs.data, 'base64'); var payload = Buffer.from(event.awslogs.data, 'base64');
...@@ -31,7 +31,7 @@ exports.handler = async function(event,context){ ...@@ -31,7 +31,7 @@ exports.handler = async function(event,context){
MetricName: '400 Errors', MetricName: '400 Errors',
Dimensions: [ Dimensions: [
{ {
Name: 'API/REST', Name: 'ApiName',
Value: data.name Value: data.name
}, },
{ {
...@@ -59,7 +59,7 @@ exports.handler = async function(event,context){ ...@@ -59,7 +59,7 @@ exports.handler = async function(event,context){
}); });
//when all logs are processed, send the PutMetricData request //when all logs are processed, send the PutMetricData request
Promise.all(promises).then(metrics=>{ //if all the promises (cf line 26) succed (equivalent to all the getRestApiPromise) Promise.all(promises).then(metrics => { //if all the promises (cf line 26) succeed (equivalent to all the getRestApiPromise)
const params = { const params = {
MetricData:metrics, MetricData:metrics,
Namespace: "APIGW/custom" Namespace: "APIGW/custom"
......
...@@ -2,19 +2,26 @@ ...@@ -2,19 +2,26 @@
import urllib3 import urllib3
import json import json
import os import os
from string import Template
http = urllib3.PoolManager() http = urllib3.PoolManager()
def lambda_handler(event, context): def lambda_handler(event, context):
url = os.getenv("SLACK_WEBHOOK_URL") url = os.getenv("SLACK_WEBHOOK_URL")
print(json.dumps(event)) print(json.dumps(event))
messages = [json.loads(i.get('Sns',{}).get('Message',"")) for i in event.get('Records',[])] #get all messages in dict format
text=[] messages = [json.loads(r.get('Sns',{}).get('Message','{}')) for r in event.get('Records',[])]
for m in messages: text = []
text.append("ALARM - Details :\n"+ \ template = Template("\
"\t- Name : "+m.get('AlarmName')+"\n"+ \ ALARM - Details :\n\
"\t- Description : "+m.get('AlarmDescription')+"\n"+ \ \t- Name : $AlarmName \n\
"\t- New State Value : "+m.get('NewStateValue')+"\n"+ \ \t- Description : $AlarmDescription \n\
"\t- New State Reason : "+m.get('NewStateReason')+"\n"+ \ \t- New State : $NewStateValue \n\
"\t- New State Time : "+m.get('StateChangeTime')+"\n") \t- Reason : $NewStateReason \n\
\t- Time : $StateChangeTime \n")
for message in messages:
text.append(template.substitute(message))
print("Number of messages : " +str(len(text))) print("Number of messages : " +str(len(text)))
msg = { msg = {
"text": "\n-------\n".join(text) "text": "\n-------\n".join(text)
......
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