Skip to content

Commit

Permalink
change to fix a little bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitadugar committed Apr 15, 2020
1 parent a8a2178 commit aefd895
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
]
Expand Down Expand Up @@ -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 | `<list>` | no |
| kms\_key\_arn | The ARN for the KMS encryption key. | string | `""` | no |
| label\_order | Label order, e.g. `name`,`application`. | list | `<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 | `<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 | `<list>` | no |
| managedby | ManagedBy, eg 'CloudDrove' or 'AnmolNagpal'. | string | `"[email protected]"` | no |
Expand Down
2 changes: 1 addition & 1 deletion README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
]
Expand Down
2 changes: 1 addition & 1 deletion _example/complete-function/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
]
Expand Down
3 changes: 1 addition & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ 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
layer_name = element(var.names, count.index)
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
Expand Down
12 changes: 12 additions & 0 deletions slack/build.sh
Original file line number Diff line number Diff line change
@@ -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/
Empty file added slack/packages/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions slack/slack/build_layer.sh
Original file line number Diff line number Diff line change
@@ -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}
5 changes: 5 additions & 0 deletions slack/slack/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
urllib5==5.0.0
certifi==2020.4.5.1
chardet==3.0.4
idna==2.9
requests==2.23.0
80 changes: 80 additions & 0 deletions slack/src/index.py
Original file line number Diff line number Diff line change
@@ -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
)

6 changes: 3 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}

Expand Down

0 comments on commit aefd895

Please sign in to comment.