diff --git a/deploy/.terraform.lock.hcl b/deploy/.terraform.lock.hcl index e1ec1ea8b12df7a000c4c1d4f7a069b802d284df..5a8d7f6f103b7f788697a52c44588cfca268dd1b 100644 --- a/deploy/.terraform.lock.hcl +++ b/deploy/.terraform.lock.hcl @@ -42,3 +42,23 @@ provider "registry.terraform.io/hashicorp/aws" { "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", + ] +} diff --git a/deploy/api_gateway.tf b/deploy/api_gateway.tf index 599967028453ead1a93a96938790ef563e0468b1..68e5f7e3b1fd90ab4c778c45dbbc746e60614549 100644 --- a/deploy/api_gateway.tf +++ b/deploy/api_gateway.tf @@ -114,6 +114,7 @@ resource "aws_api_gateway_stage" "main" { destination_arn = aws_cloudwatch_log_group.apigw_logs.arn } } + output "api_endpoint" { value = aws_api_gateway_stage.main.invoke_url } diff --git a/deploy/cloudwatch_api_logs.tf b/deploy/cloudwatch_api_logs.tf index be6dc0654a8ae9685a4084aece2f931bae1340a0..8e132fb398bb5caa6ece2c0d516cecd8b4844c73 100644 --- a/deploy/cloudwatch_api_logs.tf +++ b/deploy/cloudwatch_api_logs.tf @@ -22,13 +22,8 @@ resource "aws_lambda_function" "apigw_logs" { resource "aws_cloudwatch_log_subscription_filter" "lambdafunction_logfilter" { name = "${local.prefix}-lambdafunction-subscription" 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 - lifecycle { - ignore_changes = [ - filter_pattern - ] - } } 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}:*" } +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" { type = "zip" output_path = "${local.lambda_loc}/zip/apigw_metrics.zip" diff --git a/deploy/lambda.tf b/deploy/lambda.tf index aec22bceb0501b08d0734e4aa361e3e2ce8d6ee8..0863e8bd71fc8ec093247f29cf84f1a51a78455e 100644 --- a/deploy/lambda.tf +++ b/deploy/lambda.tf @@ -83,8 +83,22 @@ resource "aws_iam_role_policy_attachment" "lambda_insights" { ######### # 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" { + depends_on = [ + null_resource.lambda_dependencies + ] type = "zip" output_path = "${local.lambda_loc}/zip/lambda.zip" source_dir = "${local.lambda_loc}/src" diff --git a/deploy/lambda/apigw_metrics/apigw_metrics.js b/deploy/lambda/apigw_metrics/apigw_metrics.js index 4e6396025a50e64f04dd706eb65cc31b97abcfcf..6d3c1a03810d6157799ccff5cbd01d4648a9eef2 100644 --- a/deploy/lambda/apigw_metrics/apigw_metrics.js +++ b/deploy/lambda/apigw_metrics/apigw_metrics.js @@ -4,7 +4,7 @@ const CW = new AWS.CloudWatch() const APIGW = new AWS.APIGateway() exports.handler = async function(event,context){ - return new Promise((resolve,reject)=>{ + return new Promise((resolve,reject) => { console.log("Event: "+JSON.stringify(event)) var payload = Buffer.from(event.awslogs.data, 'base64'); @@ -31,7 +31,7 @@ exports.handler = async function(event,context){ MetricName: '400 Errors', Dimensions: [ { - Name: 'API/REST', + Name: 'ApiName', Value: data.name }, { @@ -59,7 +59,7 @@ exports.handler = async function(event,context){ }); //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 = { MetricData:metrics, Namespace: "APIGW/custom" diff --git a/deploy/lambda/slack/slack.py b/deploy/lambda/slack/slack.py index 471178003f4fe77537127a1c86573403d1f0a0e9..4ddd4a64df5ec95e22fb084dc4f821f2f4d1e759 100644 --- a/deploy/lambda/slack/slack.py +++ b/deploy/lambda/slack/slack.py @@ -2,19 +2,26 @@ import urllib3 import json import os +from string import Template 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") + #get all messages in dict format + messages = [json.loads(r.get('Sns',{}).get('Message','{}')) for r in event.get('Records',[])] + text = [] + template = Template("\ +ALARM - Details :\n\ +\t- Name : $AlarmName \n\ +\t- Description : $AlarmDescription \n\ +\t- New State : $NewStateValue \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))) msg = { "text": "\n-------\n".join(text)