Skip to content

Commit 4ef843b

Browse files
committed
Commit zipped Lambda function into git
The Geodesic container does not contain pip, which is required to bundle the Python Lambda function dependencies into the zip archive. Rather than pulling pip into Geodesic and after discussion with @igor, it was deemed better to check in a zipped up archive of the Lambda function containing code and dependencies, and add a Make target to regenerate that, from within a Docker container. I’m not too fond of checking in binaries like zip files, as this is an extra manual step compared to the previous solution. If you change the function or dependencies, you need to remember to run `make build`
1 parent a05e933 commit 4ef843b

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Compiled files
22
*.tfstate
33
*.tfstate.backup
4-
*.zip
54

65
# Module directory
76
.terraform

Makefile

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
SHELL := /bin/bash
1+
SHELL := /bin/bash
2+
LAMBDA_DIR := lambda
3+
DEPS_CONTAINER := alpine:3.8
24

35
# List of targets the `readme` target should call before generating the readme
46
export README_DEPS ?= docs/targets.md docs/terraform.md
@@ -7,4 +9,22 @@ export README_DEPS ?= docs/targets.md docs/terraform.md
79

810
## Lint terraform code
911
lint:
10-
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate
12+
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate
13+
14+
define docker
15+
docker run -it -v $(PWD)/$(LAMBDA_DIR)/:/code -w /code $(DEPS_CONTAINER) /bin/sh -c '$(1)'
16+
endef
17+
18+
## Install dependencies
19+
dependencies:
20+
@echo "==> Installing Lambda function dependencies..."
21+
@$(call docker, apk add --update py-pip && \
22+
pip install virtualenv && \
23+
virtualenv venv --always-copy && \
24+
source ./venv/bin/activate && \
25+
./venv/bin/pip install -qUr requirements.txt)
26+
27+
## Build Lambda function zip
28+
build: dependencies
29+
@echo "==> Building Lambda function zip..."
30+
@cd $(LAMBDA_DIR) && zip -r ../elasticsearch_cleanup.zip * && cd ../

elasticsearch_cleanup.zip

13.6 MB
Binary file not shown.

main.tf

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ data "aws_subnet_ids" "default" {
2323
vpc_id = "${var.vpc_id}"
2424
}
2525

26-
data "archive_file" "default" {
27-
depends_on = ["null_resource.pip_install_deps"]
28-
type = "zip"
29-
source_dir = "${local.lambda_dir}"
30-
output_path = "${local.lambda_path}"
31-
}
32-
3326
data "aws_iam_policy_document" "assume_role" {
3427
statement {
3528
actions = ["sts:AssumeRole"]
@@ -90,30 +83,13 @@ module "label" {
9083
# Locals
9184
#--------------------------------------------------------------
9285
locals {
93-
lambda_dir = "${path.module}/lambda"
94-
lambda_path = "${local.lambda_dir}/elasticsearch_cleanup.zip"
86+
lambda_path = "${path.module}/elasticsearch_cleanup.zip"
9587
function_name = "${module.label.id}-elasticsearch-cleanup"
9688
enabled = "${var.enabled == "true" ? true : false }"
9789
}
9890

9991
# Resources
10092
#--------------------------------------------------------------
101-
resource "null_resource" "pip_install_deps" {
102-
triggers {
103-
pip = "${base64sha256(file("${local.lambda_dir}/requirements.txt"))}"
104-
}
105-
106-
provisioner "local-exec" {
107-
command = <<EOT
108-
cd "${local.lambda_dir}"
109-
pip install virtualenv
110-
virtualenv venv --always-copy
111-
source ./venv/bin/activate
112-
./venv/bin/pip install -Ur requirements.txt
113-
EOT
114-
}
115-
}
116-
11793
resource "aws_lambda_function" "default" {
11894
count = "${local.enabled == true ? 1 : 0}"
11995
filename = "${local.lambda_path}"
@@ -123,7 +99,7 @@ resource "aws_lambda_function" "default" {
12399
runtime = "python${var.python_version}"
124100
role = "${aws_iam_role.default.arn}"
125101
handler = "es-cleanup.lambda_handler"
126-
source_code_hash = "${data.archive_file.default.output_base64sha256}"
102+
source_code_hash = "${base64sha256(file(local.lambda_path))}"
127103
tags = "${module.label.tags}"
128104

129105
environment {

0 commit comments

Comments
 (0)