Skip to content

Commit cb99cd7

Browse files
committed
Require output_path, create intermediate paths
1 parent d69b1c8 commit cb99cd7

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ or .pyc files in the archive so the hash is stable unless the source or dependen
1212
change.
1313

1414
## Example
15+
1516
```
1617
module "python_lambda_archive" {
1718
source = "rojopolis/lambda-python-archive/aws"
1819
src_dir = "${path.module}/python"
20+
output_path = "${path.module}/lambda.zip"
1921
}
2022
2123
resource "aws_iam_role" "iam_for_lambda" {

examples/lambda_python_archive.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module "python_lambda_archive" {
2-
source = "../"
3-
src_dir = "${path.module}/python"
2+
source = "../"
3+
src_dir = "${path.module}/python"
4+
output_path = "${path.module}/artifacts/lambda.zip"
45
}
56

67
resource "aws_iam_role" "iam_for_lambda" {

main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
data "external" "lambda_archive" {
22
program = ["python", "${path.module}/scripts/build_lambda.py"]
33
query = {
4-
src_dir = "${var.src_dir}"
4+
src_dir = "${var.src_dir}"
5+
output_path = "${var.output_path}"
56
}
67
}

scripts/build_lambda.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
from distutils.dir_util import copy_tree
33

44
import base64
5-
import glob
5+
import errno
66
import hashlib
77
import json
8+
import logging
89
import os
910
import shutil
1011
import subprocess
1112
import sys
1213
import tempfile
1314
import zipfile
1415

15-
def build(src_dir):
16+
def build(src_dir, output_path):
1617
with tempfile.TemporaryDirectory() as build_dir:
1718
copy_tree(src_dir, build_dir)
1819
if os.path.exists(os.path.join(src_dir, 'requirements.txt')):
@@ -27,19 +28,26 @@ def build(src_dir):
2728
check=True,
2829
stdout=subprocess.DEVNULL,
2930
)
30-
artifact=tempfile.NamedTemporaryFile(delete=False)
31-
make_archive(build_dir, artifact)
32-
return artifact.name
31+
make_archive(build_dir, output_path)
32+
return output_path
3333

3434

35-
def make_archive(src_dir, archive_file):
36-
with zipfile.ZipFile(archive_file, 'w') as archive:
35+
def make_archive(src_dir, output_path):
36+
try:
37+
os.makedirs(os.path.dirname(output_path))
38+
except OSError as e:
39+
if e.errno == errno.EEXIST:
40+
pass
41+
else:
42+
raise
43+
44+
with zipfile.ZipFile(output_path, 'w') as archive:
3745
for root, dirs, files in os.walk(src_dir):
3846
for file in files:
3947
if file.endswith('.pyc'):
4048
break
4149
metadata = zipfile.ZipInfo(
42-
os.path.join(root, file).replace(src_dir, '')
50+
os.path.join(root, file).replace(src_dir, '').lstrip(os.sep)
4351
)
4452
with open(os.path.join(root, file), 'rb') as f:
4553
data = f.read()
@@ -48,17 +56,19 @@ def make_archive(src_dir, archive_file):
4856
data
4957
)
5058

51-
def get_hash(archive_file):
59+
def get_hash(output_path):
5260
'''
5361
Return base64 encoded sha256 hash of archive file
5462
'''
55-
with open(archive_file, 'rb') as f:
63+
with open(output_path, 'rb') as f:
5664
h = hashlib.sha256()
5765
h.update(f.read())
5866
return base64.standard_b64encode(h.digest()).decode('utf-8', 'strict')
5967

6068

6169
if __name__ == '__main__':
70+
logging.basicConfig(level='DEBUG')
6271
query = json.loads(sys.stdin.read())
63-
archive = build(query['src_dir'])
72+
logging.debug(query)
73+
archive = build(query['src_dir'], query['output_path'])
6474
print(json.dumps({'archive': archive, "base64sha256":get_hash(archive)}))

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
variable "src_dir" {
22
description = "Path to root of Python source to package."
33
type = "string"
4+
}
5+
6+
variable "output_path" {
7+
description = "The output of the archive file."
8+
type = "string"
49
}

0 commit comments

Comments
 (0)