From aefd895c75371d67f0329938c6b080dbe0c9b8c6 Mon Sep 17 00:00:00 2001 From: nikitadugar Date: Wed, 15 Apr 2020 15:19:31 +0530 Subject: [PATCH 1/2] change to fix a little bug --- README.md | 4 +- README.yaml | 2 +- _example/complete-function/example.tf | 2 +- main.tf | 3 +- slack/build.sh | 12 ++++ slack/packages/.gitkeep | 0 slack/slack/build_layer.sh | 4 ++ slack/slack/requirements.txt | 5 ++ slack/src/index.py | 80 +++++++++++++++++++++++++++ variables.tf | 6 +- 10 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 slack/build.sh create mode 100644 slack/packages/.gitkeep create mode 100755 slack/slack/build_layer.sh create mode 100644 slack/slack/requirements.txt create mode 100644 slack/src/index.py diff --git a/README.md b/README.md index f9911d9..2f3c988 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ Here are some examples of how you can use this module in your inventory structur names = [ "python_layer" ] - layer_filename = "./../../lambda/packages/Python3-lambda.zip" + layer_filenames = ["./../../lambda/packages/Python3-lambda.zip"] compatible_runtimes = [ ["python3.8"] ] @@ -172,7 +172,7 @@ Here are some examples of how you can use this module in your inventory structur | iam\_actions | The actions for Iam Role Policy. | list | `` | no | | kms\_key\_arn | The ARN for the KMS encryption key. | string | `""` | no | | label\_order | Label order, e.g. `name`,`application`. | list | `` | no | -| layer\_filename | The path to the function's deployment package within the local filesystem. If defined, The s3\_-prefixed options cannot be used. | string | `""` | no | +| layer\_filenames | The path to the function's deployment package within the local filesystem. If defined, The s3\_-prefixed options cannot be used. | list | `` | no | | layers | List of Lambda Layer Version ARNs \(maximum of 5\) to attach to your Lambda Function. | string | `""` | no | | license\_infos | License info for your Lambda Layer. See License Info. | list | `` | no | | managedby | ManagedBy, eg 'CloudDrove' or 'AnmolNagpal'. | string | `"anmol@clouddrove.com"` | no | diff --git a/README.yaml b/README.yaml index 96c546a..104bb9e 100644 --- a/README.yaml +++ b/README.yaml @@ -92,7 +92,7 @@ usage : |- names = [ "python_layer" ] - layer_filename = "./../../lambda/packages/Python3-lambda.zip" + layer_filenames = ["./../../lambda/packages/Python3-lambda.zip"] compatible_runtimes = [ ["python3.8"] ] diff --git a/_example/complete-function/example.tf b/_example/complete-function/example.tf index 133046d..29382a8 100644 --- a/_example/complete-function/example.tf +++ b/_example/complete-function/example.tf @@ -23,7 +23,7 @@ module "lambda" { names = [ "python_layer" ] - layer_filename = "./../../lambda/packages/Python3-lambda.zip" + layer_filenames = ["./../../lambda/packages/Python3-lambda.zip"] compatible_runtimes = [ ["python3.8"] ] diff --git a/main.tf b/main.tf index 98f6b29..a615b23 100644 --- a/main.tf +++ b/main.tf @@ -69,7 +69,7 @@ resource "aws_iam_role_policy_attachment" "default" { # Description : Terraform module to create Lambda layers resource on AWS. resource "aws_lambda_layer_version" "default" { count = length(var.names) > 0 && var.enabled ? length(var.names) : 0 - filename = length(var.layer_filename) > 0 ? var.layer_filename : null + filename = length(var.layer_filenames) > 0 ? element(var.layer_filenames, count.index) : null s3_bucket = length(var.s3_buckets) > 0 ? element(var.s3_buckets, count.index) : null s3_key = length(var.s3_keies) > 0 ? element(var.s3_keies, count.index) : null s3_object_version = length(var.s3_object_versions) > 0 ? element(var.s3_object_versions, count.index) : null @@ -77,7 +77,6 @@ resource "aws_lambda_layer_version" "default" { compatible_runtimes = element(var.compatible_runtimes, count.index) description = length(var.descriptions) > 0 ? element(var.descriptions, count.index) : "" license_info = length(var.license_infos) > 0 ? element(var.license_infos, count.index) : "" - source_code_hash = length(var.layer_filename) > 0 ? filesha256(var.layer_filename) : "" } # Module : Archive file diff --git a/slack/build.sh b/slack/build.sh new file mode 100644 index 0000000..130c00e --- /dev/null +++ b/slack/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +export WRKDIR=$(pwd) +export LYR_PDS_DIR="slack" + +#Init Packages Directory +mkdir -p packages/ + +# Building Python-pandas layer +cd ${WRKDIR}/${LYR_PDS_DIR}/ +${WRKDIR}/${LYR_PDS_DIR}/build_layer.sh +zip -r ${WRKDIR}/packages/Python3-slack.zip . +rm -rf ${WRKDIR}/${LYR_PDS_DIR}/python/ \ No newline at end of file diff --git a/slack/packages/.gitkeep b/slack/packages/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/slack/slack/build_layer.sh b/slack/slack/build_layer.sh new file mode 100755 index 0000000..b5e3846 --- /dev/null +++ b/slack/slack/build_layer.sh @@ -0,0 +1,4 @@ +#!/bin/bash +export PKG_DIR="python" +rm -rf ${PKG_DIR} && mkdir -p ${PKG_DIR} && docker run -rm -v $(pwd):/foo lambci/lambda:build-python3.8 +pip install -r requirements.txt --no-deps -t ${PKG_DIR} \ No newline at end of file diff --git a/slack/slack/requirements.txt b/slack/slack/requirements.txt new file mode 100644 index 0000000..79b7dd9 --- /dev/null +++ b/slack/slack/requirements.txt @@ -0,0 +1,5 @@ +urllib5==5.0.0 +certifi==2020.4.5.1 +chardet==3.0.4 +idna==2.9 +requests==2.23.0 \ No newline at end of file diff --git a/slack/src/index.py b/slack/src/index.py new file mode 100644 index 0000000..d01d5ee --- /dev/null +++ b/slack/src/index.py @@ -0,0 +1,80 @@ +import boto3 +import os +import logging +import json +import requests +import collections +import datetime +import sys +import pprint + +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +ec = boto3.client("ec2") + +SLACK_CHANNEL = os.environ['SLACK_CHANNEL'] +SLACK_WEBHOOK = os.environ['SLACK_WEBHOOK'] +ICON_EMOJI = ':cloudtrail:' +USERNAME = 'CloudTrail Bot' + +def lambda_handler(event, context): + message = json.loads(event["Records"][0]["Sns"]["Message"]) + payload = create_slack_payload({ + 'Message': message + }) + post_to_slack(payload) + +def create_slack_payload(json_dict, color='#FF0000', reason='Alarm Event.'): + logger.info('Creating slack payload from the following json: {}'.format(json_dict)) + payload ={ + "attachments": [ + { + "fallback": reason, + "color": color, + "title": reason, + "fields": [ + { + "title": "Action", + "value": "Config Rules Notification", + "short": True + }, + { + "title": "Message", + "value": '```\n{}\n```'.format(json.dumps(json_dict['Message'], indent=4)), + "short": False + } + ], + "footer": "CloudDrove", + "footer_icon": "https://clouddrove.com/media/images/favicon.ico", + } + ], + 'channel': SLACK_CHANNEL, + 'username': USERNAME, + 'icon_emoji': ICON_EMOJI + } + + return payload + + +def post_to_slack(payload): + logger.info('POST-ing payload: {}'.format(json.dumps(payload,indent=4))) + + try: + req = requests.post(SLACK_WEBHOOK, data=str(payload), timeout=3) + logger.info("Message posted to {} using {}".format(payload['channel'], SLACK_WEBHOOK)) + except requests.exceptions.Timeout as e: + fatal("Server connection failed: {}".format(e)) + except requests.exceptions.RequestException as e: + fatal("Request failed: {}".format(e)) + + if req.status_code != 200: + fatal( + "Non 200 status code: {}\nResponse Headers: {}\nResponse Text: {}".format( + req.status_code, + req.headers, + json.dumps(req.text, indent=4) + ), + code=255 + ) + diff --git a/variables.tf b/variables.tf index 2f7c83f..4d56676 100644 --- a/variables.tf +++ b/variables.tf @@ -127,9 +127,9 @@ variable "kms_key_arn" { description = "The ARN for the KMS encryption key." } -variable "layer_filename" { - type = string - default = "" +variable "layer_filenames" { + type = list + default = [] description = "The path to the function's deployment package within the local filesystem. If defined, The s3_-prefixed options cannot be used." } From a57f2e6f31f4772afc9549e9fc74ae850540d3cb Mon Sep 17 00:00:00 2001 From: nikitadugar Date: Wed, 15 Apr 2020 15:19:53 +0530 Subject: [PATCH 2/2] change to fix a little bug --- slack/build.sh | 12 ------ slack/packages/.gitkeep | 0 slack/slack/build_layer.sh | 4 -- slack/slack/requirements.txt | 5 --- slack/src/index.py | 80 ------------------------------------ 5 files changed, 101 deletions(-) delete mode 100644 slack/build.sh delete mode 100644 slack/packages/.gitkeep delete mode 100755 slack/slack/build_layer.sh delete mode 100644 slack/slack/requirements.txt delete mode 100644 slack/src/index.py diff --git a/slack/build.sh b/slack/build.sh deleted file mode 100644 index 130c00e..0000000 --- a/slack/build.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -export WRKDIR=$(pwd) -export LYR_PDS_DIR="slack" - -#Init Packages Directory -mkdir -p packages/ - -# Building Python-pandas layer -cd ${WRKDIR}/${LYR_PDS_DIR}/ -${WRKDIR}/${LYR_PDS_DIR}/build_layer.sh -zip -r ${WRKDIR}/packages/Python3-slack.zip . -rm -rf ${WRKDIR}/${LYR_PDS_DIR}/python/ \ No newline at end of file diff --git a/slack/packages/.gitkeep b/slack/packages/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/slack/slack/build_layer.sh b/slack/slack/build_layer.sh deleted file mode 100755 index b5e3846..0000000 --- a/slack/slack/build_layer.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -export PKG_DIR="python" -rm -rf ${PKG_DIR} && mkdir -p ${PKG_DIR} && docker run -rm -v $(pwd):/foo lambci/lambda:build-python3.8 -pip install -r requirements.txt --no-deps -t ${PKG_DIR} \ No newline at end of file diff --git a/slack/slack/requirements.txt b/slack/slack/requirements.txt deleted file mode 100644 index 79b7dd9..0000000 --- a/slack/slack/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -urllib5==5.0.0 -certifi==2020.4.5.1 -chardet==3.0.4 -idna==2.9 -requests==2.23.0 \ No newline at end of file diff --git a/slack/src/index.py b/slack/src/index.py deleted file mode 100644 index d01d5ee..0000000 --- a/slack/src/index.py +++ /dev/null @@ -1,80 +0,0 @@ -import boto3 -import os -import logging -import json -import requests -import collections -import datetime -import sys -import pprint - -logger = logging.getLogger() -logger.setLevel(logging.INFO) - -ec = boto3.client("ec2") - -SLACK_CHANNEL = os.environ['SLACK_CHANNEL'] -SLACK_WEBHOOK = os.environ['SLACK_WEBHOOK'] -ICON_EMOJI = ':cloudtrail:' -USERNAME = 'CloudTrail Bot' - -def lambda_handler(event, context): - message = json.loads(event["Records"][0]["Sns"]["Message"]) - payload = create_slack_payload({ - 'Message': message - }) - post_to_slack(payload) - -def create_slack_payload(json_dict, color='#FF0000', reason='Alarm Event.'): - logger.info('Creating slack payload from the following json: {}'.format(json_dict)) - payload ={ - "attachments": [ - { - "fallback": reason, - "color": color, - "title": reason, - "fields": [ - { - "title": "Action", - "value": "Config Rules Notification", - "short": True - }, - { - "title": "Message", - "value": '```\n{}\n```'.format(json.dumps(json_dict['Message'], indent=4)), - "short": False - } - ], - "footer": "CloudDrove", - "footer_icon": "https://clouddrove.com/media/images/favicon.ico", - } - ], - 'channel': SLACK_CHANNEL, - 'username': USERNAME, - 'icon_emoji': ICON_EMOJI - } - - return payload - - -def post_to_slack(payload): - logger.info('POST-ing payload: {}'.format(json.dumps(payload,indent=4))) - - try: - req = requests.post(SLACK_WEBHOOK, data=str(payload), timeout=3) - logger.info("Message posted to {} using {}".format(payload['channel'], SLACK_WEBHOOK)) - except requests.exceptions.Timeout as e: - fatal("Server connection failed: {}".format(e)) - except requests.exceptions.RequestException as e: - fatal("Request failed: {}".format(e)) - - if req.status_code != 200: - fatal( - "Non 200 status code: {}\nResponse Headers: {}\nResponse Text: {}".format( - req.status_code, - req.headers, - json.dumps(req.text, indent=4) - ), - code=255 - ) -