diff --git a/README.md b/README.md index 099c573..f1663c9 100644 --- a/README.md +++ b/README.md @@ -52,24 +52,29 @@ resource "aws_sns_topic_subscription" "lambda_sns_to_slack" { } ``` -### Configurable variables - -| **Variable** | **Description** | **Required** | **Default** | -|:--------------------------:|:-----------------------------------------------------------------:|--------------|--------------------------------| -| **slack_webhook_url** | Slack incoming webhook URL without protocol name. | yes | | -| **slack_channel_map** | Topic-to-channel mapping. | yes | | -| **lambda_function_name** | AWS Lambda function name for the Slack notifier | no | `"sns-to-slack"` | -| **default_username** | Default username for notifications used if no matching one found. | no | `"AWS Lambda"` | -| **default_channel** | Default channel used if no matching channel found. | no | `"#webhook-tests"` | -| **default_emoji** | Default emoji used if no matching emoji found. | no | `":information_source:"` | -| **lambda_iam_role_name** | IAM role name for lambda functions. | no | `"lambda-sns-to-slack"` | -| **lambda_iam_policy_name** | IAM policy name for lambda functions. | no | `"lambda-sns-to-slack-policy"` | - -### Output variables - -| **Variable** | **Description** | -|-------------------------|-----------------------------------| -| **lambda_function_arn** | AWS Lambda notifier function ARN. | +## Configurable variables + +### Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| default\_channel | Default channel used if no matching channel found | string | `#webhook-tests` | no | +| default\_emoji | Default emoji used if no matching emoji found | string | `:information_source:` | no | +| default\_username | Default username for notifications used if no matching one found | string | `AWS Lambda` | no | +| lambda\_function\_name | AWS Lambda function name for the Slack notifier | string | `sns-to-slack` | no | +| lambda\_iam\_policy\_name | IAM policy name for lambda functions | string | `lambda-sns-to-slack-policy` | no | +| lambda\_iam\_role\_name | IAM role name for lambda functions | string | `lambda-sns-to-slack` | no | +| slack\_channel\_map | Topic-to-channel mapping | map | - | yes | +| slack\_webhook\_url | Slack incoming webhook URL without protocol name | string | - | yes | +| username\_prefix | if sepecified the usernames that are looked up will be prefixed by this. Useful in situations where multiple accounts report to a single slack channel. | string | `` | no | + +### Outputs + +| Name | Description | +|------|-------------| +| lambda\_function\_arn | AWS Lambda notifier function ARN | + + ## Examples diff --git a/build-function.sh b/build-function.sh index 2de68e7..dc70899 100755 --- a/build-function.sh +++ b/build-function.sh @@ -16,6 +16,8 @@ pipenv run pip install -r <(pipenv lock -r) --target _build/ cp lambda_function.py _build/ pushd _build +find . -name '*.pyc' -delete +rm -f ${zipname} zip -r ${zipname} * cp ${zipname} ${outdir} popd diff --git a/module/lambda.tf b/module/lambda.tf index 5a051c3..fc782e6 100644 --- a/module/lambda.tf +++ b/module/lambda.tf @@ -17,6 +17,7 @@ resource "aws_lambda_function" "sns_to_slack" { DEFAULT_USERNAME = "${var.default_username}" DEFAULT_CHANNEL = "${var.default_channel}" DEFAULT_EMOJI = "${var.default_emoji}" + USERNAME_PREFIX = "${var.username_prefix}" } } } diff --git a/module/lambda/sns-to-slack.zip b/module/lambda/sns-to-slack.zip index 154b633..cfd215d 100644 Binary files a/module/lambda/sns-to-slack.zip and b/module/lambda/sns-to-slack.zip differ diff --git a/module/outputs.tf b/module/outputs.tf index 15fe2e5..7682dc4 100644 --- a/module/outputs.tf +++ b/module/outputs.tf @@ -1,3 +1,4 @@ output "lambda_function_arn" { - value = "${aws_lambda_function.sns_to_slack.arn}" + value = "${aws_lambda_function.sns_to_slack.arn}" + description = "AWS Lambda notifier function ARN" } diff --git a/module/variables.tf b/module/variables.tf index 82e95f5..fb9af89 100644 --- a/module/variables.tf +++ b/module/variables.tf @@ -1,37 +1,51 @@ variable "slack_webhook_url" { - type = "string" + type = "string" + description = "Slack incoming webhook URL without protocol name" } variable "slack_channel_map" { - type = "map" + type = "map" + description = "Topic-to-channel mapping" } variable "lambda_function_name" { - type = "string" - default = "sns-to-slack" + type = "string" + default = "sns-to-slack" + description = "AWS Lambda function name for the Slack notifier" } variable "default_username" { - type = "string" - default = "AWS Lambda" + type = "string" + default = "AWS Lambda" + description = "Default username for notifications used if no matching one found" +} + +variable "username_prefix" { + type = "string" + default = "" + description = "if sepecified the usernames that are looked up will be prefixed by this. Useful in situations where multiple accounts report to a single slack channel." } variable "default_channel" { - type = "string" - default = "#webhook-tests" + type = "string" + default = "#webhook-tests" + description = "Default channel used if no matching channel found" } variable "default_emoji" { - type = "string" - default = ":information_source:" + type = "string" + default = ":information_source:" + description = "Default emoji used if no matching emoji found" } variable "lambda_iam_role_name" { - type = "string" - default = "lambda-sns-to-slack" + type = "string" + default = "lambda-sns-to-slack" + description = "IAM role name for lambda functions" } variable "lambda_iam_policy_name" { - type = "string" - default = "lambda-sns-to-slack-policy" + type = "string" + default = "lambda-sns-to-slack-policy" + description = "IAM policy name for lambda functions" } diff --git a/sns-to-slack/lambda_function.py b/sns-to-slack/lambda_function.py index 0b359ec..7baed46 100644 --- a/sns-to-slack/lambda_function.py +++ b/sns-to-slack/lambda_function.py @@ -46,6 +46,7 @@ DEFAULT_USERNAME = os.environ.get('DEFAULT_USERNAME', 'AWS Lambda') DEFAULT_CHANNEL = os.environ.get('DEFAULT_CHANNEL', '#webhook-tests') DEFAULT_EMOJI = os.environ.get('DEFAULT_EMOJI', ':information_source:') +USERNAME_PREFIX = os.environ.get('USERNAME_PREFIX', '') def get_slack_emoji(event_src, topic_name, event_cond='default'): @@ -92,7 +93,7 @@ def get_slack_username(event_src): 'rds': 'AWS RDS'} try: - return username_map[event_src] + return "{0}{1}".format(USERNAME_PREFIX, username_map[event_src]) except KeyError: return DEFAULT_USERNAME