Skip to content

Commit

Permalink
Merge pull request #9 from allcloud-jonathan/master
Browse files Browse the repository at this point in the history
Username Prefix && housekeeping
  • Loading branch information
builtinnya authored Dec 8, 2018
2 parents 21638fa + c71f4cf commit 06187a0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 34 deletions.
41 changes: 23 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions build-function.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions module/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
}
}
Binary file modified module/lambda/sns-to-slack.zip
Binary file not shown.
3 changes: 2 additions & 1 deletion module/outputs.tf
Original file line number Diff line number Diff line change
@@ -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"
}
42 changes: 28 additions & 14 deletions module/variables.tf
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 2 additions & 1 deletion sns-to-slack/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 06187a0

Please sign in to comment.