Skip to content

Commit

Permalink
Merge pull request #6 from optiflows/housekeeping
Browse files Browse the repository at this point in the history
Housekeeping: Pipenv & Slimmer package
  • Loading branch information
builtinnya authored Jun 10, 2018
2 parents d7cd5d5 + f2a6001 commit 5e12459
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ virtualenv/
terraform.tfstate.backup
terraform.tfstate
secrets.tfvars
_build/

# Mac OS
.DS_Store
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ module "sns_to_slack" {
source = "github.com/builtinnya/aws-sns-slack-terraform/module"
slack_webhook_url = "hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
slack_channel_map = "{ \"topic-name\": \"#slack-channel\" }"
slack_channel_map = {
"topic-name" = "#slack-channel"
}
# The following variables are optional.
lambda_function_name = "sns-to-slack"
Expand Down Expand Up @@ -55,7 +57,7 @@ resource "aws_sns_topic_subscription" "lambda_sns_to_slack" {
| **Variable** | **Description** | **Required** | **Default** |
|:--------------------------:|:-----------------------------------------------------------------:|--------------|--------------------------------|
| **slack_webhook_url** | Slack incoming webhook URL without protocol name. | yes | |
| **slack_channel_map** | Topic-to-channel mapping string in JSON. | 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"` |
Expand Down Expand Up @@ -131,18 +133,16 @@ $ AWS_ACCESS_KEY_ID=<ACCESS_KEY> \
## Development

The main AWS Lambda function code is located in [sns-to-slack/](/sns-to-slack) directory.
To prepare development, you need to create [virtualenv](https://virtualenv.pypa.io/en/stable/) for this project and install required pip packages as following.
To prepare development, you need to use [Pipenv](https://docs.pipenv.org/) for this project and install required dependencies as following.

```bash
$ virtualenv sns-to-slack/virtualenv
$ source sns-to-slack/virtualenv/bin/activate
$ pip install -r sns-to-slack/requirements.txt
$ cd sns-to-slack
$ pipenv install
```

You need to create [module/lambda/sns-to-slack.zip](/module/lambda/sns-to-slack.zip) to update the code as following.

```bash
$ source sns-to-slack/virtualenv/bin/activate # if you haven't yet
$ ./build-function.sh
```

Expand Down
15 changes: 6 additions & 9 deletions build-function.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ outdir="${__dir}/module/lambda"
zipname="sns-to-slack.zip"

pushd sns-to-slack
pipenv install
pipenv run pip install -r <(pipenv lock -r) --target _build/
cp lambda_function.py _build/

zip -u "${outdir}/${zipname}" lambda_function.py

pushd $VIRTUAL_ENV/lib/python2.7/site-packages

zip -u -r "${outdir}/${zipname}" . \
--exclude pip\* \
--exclude setuptools\* \
--exclude virtualenv\*

pushd _build
zip -r ${zipname} *
cp ${zipname} ${outdir}
popd

popd
6 changes: 3 additions & 3 deletions module/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

resource "aws_iam_role" "lambda_sns_to_slack" {
name = "${var.lambda_iam_role_name}"
name = "${var.lambda_iam_role_name}"
assume_role_policy = "${file("${path.module}/policies/lambda-assume-role.json")}"
}

Expand All @@ -12,7 +12,7 @@ resource "aws_iam_role" "lambda_sns_to_slack" {
#

resource "aws_iam_role_policy" "lambda_sns_to_slack" {
name = "${var.lambda_iam_policy_name}"
role = "${aws_iam_role.lambda_sns_to_slack.id}"
name = "${var.lambda_iam_policy_name}"
role = "${aws_iam_role.lambda_sns_to_slack.id}"
policy = "${file("${path.module}/policies/lambda-role-policy.json")}"
}
18 changes: 9 additions & 9 deletions module/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
#

resource "aws_lambda_function" "sns_to_slack" {
filename = "${path.module}/lambda/sns-to-slack.zip"
function_name = "${var.lambda_function_name}"
role = "${aws_iam_role.lambda_sns_to_slack.arn}"
handler = "lambda_function.lambda_handler"
filename = "${path.module}/lambda/sns-to-slack.zip"
function_name = "${var.lambda_function_name}"
role = "${aws_iam_role.lambda_sns_to_slack.arn}"
handler = "lambda_function.lambda_handler"
source_code_hash = "${base64sha256(file("${path.module}/lambda/sns-to-slack.zip"))}"
runtime = "python2.7"
runtime = "python2.7"

environment {
variables = {
WEBHOOK_URL = "${var.slack_webhook_url}"
CHANNEL_MAP = "${base64encode("${var.slack_channel_map}")}"
WEBHOOK_URL = "${var.slack_webhook_url}"
CHANNEL_MAP = "${base64encode(jsonencode(var.slack_channel_map))}"
DEFAULT_USERNAME = "${var.default_username}"
DEFAULT_CHANNEL = "${var.default_channel}"
DEFAULT_EMOJI = "${var.default_emoji}"
DEFAULT_CHANNEL = "${var.default_channel}"
DEFAULT_EMOJI = "${var.default_emoji}"
}
}
}
Binary file modified module/lambda/sns-to-slack.zip
Binary file not shown.
14 changes: 7 additions & 7 deletions module/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ variable "slack_webhook_url" {
}

variable "slack_channel_map" {
type = "string"
type = "map"
}

variable "lambda_function_name" {
type = "string"
type = "string"
default = "sns-to-slack"
}

variable "default_username" {
type = "string"
type = "string"
default = "AWS Lambda"
}

variable "default_channel" {
type = "string"
type = "string"
default = "#webhook-tests"
}

variable "default_emoji" {
type = "string"
type = "string"
default = ":information_source:"
}

variable "lambda_iam_role_name" {
type = "string"
type = "string"
default = "lambda-sns-to-slack"
}

variable "lambda_iam_policy_name" {
type = "string"
type = "string"
default = "lambda-sns-to-slack-policy"
}
12 changes: 12 additions & 0 deletions sns-to-slack/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "==2.13.0"

[dev-packages]

[requires]
python_version = "2.7"
29 changes: 29 additions & 0 deletions sns-to-slack/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion sns-to-slack/requirements.txt

This file was deleted.

0 comments on commit 5e12459

Please sign in to comment.