Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Always run Python scripts with python
Browse files Browse the repository at this point in the history
Removes python_cmd and shebangs. Now it will just use whatever version of Python it finds in the path. Adds tests for deploying Lambda functions with different Python versions while running different Python versions in the path. The .envrc files are used by Direnv to set the Python version when testing.
raymondbutcher committed Jan 4, 2019
1 parent 967d464 commit df00955
Showing 16 changed files with 79 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ This Terraform module creates and uploads an AWS Lambda function and hides the u

## Requirements

* Python
* Python 2.7 or higher
* Linux/Unix/Windows

## Usage
12 changes: 3 additions & 9 deletions archive.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
locals {
archive_external_cmd = "${concat(var.python_cmd, list("${path.module}/hash.py"))}"
archive_null_resource_cmd = "${concat(var.python_cmd, list(lookup(data.external.archive.result, "build_command")))}"
built_external_cmd = "${concat(var.python_cmd, list("${path.module}/built.py"))}"
}

# Generates a filename for the zip archive based on the contents of the files
# in source_path. The filename will change when the source code changes.
data "external" "archive" {
program = ["${local.archive_external_cmd}"]
program = ["python", "${path.module}/hash.py"]

query = {
runtime = "${var.runtime}"
@@ -22,7 +16,7 @@ resource "null_resource" "archive" {
}

provisioner "local-exec" {
command = "${join(" ", local.archive_null_resource_cmd)}"
command = "${lookup(data.external.archive.result, "build_command")}"
}
}

@@ -32,7 +26,7 @@ resource "null_resource" "archive" {
# deletes the Lambda function. If the file is rebuilt here, the build
# output is unfortunately invisible.
data "external" "built" {
program = ["${local.built_external_cmd}"]
program = ["python", "${path.module}/built.py"]

query = {
build_command = "${lookup(data.external.archive.result, "build_command")}"
10 changes: 4 additions & 6 deletions build.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/usr/bin/env python3
#
# Builds a zip file from the source_dir or source_file.
# Installs dependencies with pip automatically.
#

import base64
import json
@@ -101,11 +98,12 @@ def create_zip_file(source_dir, target_file):
target_dir = os.path.dirname(target_file)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
base, ext = os.path.splitext(target_file)
target_base, _ = os.path.splitext(target_file)
shutil.make_archive(
base if ext == '.zip' else target_file,
target_base,
format='zip',
root_dir=source_dir)
root_dir=source_dir,
)

json_payload = bytes.decode(base64.b64decode(sys.argv[1]))
query = json.loads(json_payload)
3 changes: 2 additions & 1 deletion built.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
# Builds missing zip files included in the
# Terraform state but missing from the disk.

import json
import os
5 changes: 1 addition & 4 deletions hash.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#!/usr/bin/env python3
#
# Generates a content hash of the source_path, which is used to determine if
# the Lambda code has changed, ignoring file modification and access times.
#
# Outputs a filename and a command to run if the archive needs to be built.
#

import base64
import datetime
@@ -145,7 +142,7 @@ def update_hash(hash_obj, file_root, file_path):
)

# Determine the command to run if Terraform wants to build a new archive.
build_command = "{build_script} {build_data}".format(
build_command = "python {build_script} {build_data}".format(
build_script=os.path.join(current_dir, 'build.py'),
build_data=bytes.decode(base64.b64encode(str.encode(
json.dumps({
2 changes: 0 additions & 2 deletions tests/python-cmd/lambda.py

This file was deleted.

25 changes: 0 additions & 25 deletions tests/python-cmd/main.tf

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions tests/python-versions/python2/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layout python2
33 changes: 33 additions & 0 deletions tests/python-versions/python2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
terraform {
backend "local" {
path = "terraform.tfstate"
}
}

provider "aws" {
region = "eu-west-1"
}

module "lambda_python2" {
source = "../../../"

function_name = "terraform-aws-lambda-test-python2-from-python2"
description = "Test python2 runtime from python2 environment in terraform-aws-lambda"
handler = "main.lambda_handler"
runtime = "python2.7"
timeout = 5

source_path = "${path.module}/../lambda"
}

module "lambda_python3" {
source = "../../../"

function_name = "terraform-aws-lambda-test-python3-from-python2"
description = "Test python3 runtime from python2 environment in terraform-aws-lambda"
handler = "main.lambda_handler"
runtime = "python3.7"
timeout = 5

source_path = "${path.module}/../lambda"
}
1 change: 1 addition & 0 deletions tests/python-versions/python3/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layout python3
33 changes: 33 additions & 0 deletions tests/python-versions/python3/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
terraform {
backend "local" {
path = "terraform.tfstate"
}
}

provider "aws" {
region = "eu-west-1"
}

module "lambda_python2" {
source = "../../../"

function_name = "terraform-aws-lambda-test-python2-from-python3"
description = "Test python2 runtime from python3 environment in terraform-aws-lambda"
handler = "main.lambda_handler"
runtime = "python2.7"
timeout = 5

source_path = "${path.module}/../lambda"
}

module "lambda_python3" {
source = "../../../"

function_name = "terraform-aws-lambda-test-python3-from-python3"
description = "Test python3 runtime from python3 environment in terraform-aws-lambda"
handler = "main.lambda_handler"
runtime = "python3.7"
timeout = 5

source_path = "${path.module}/../lambda"
}
21 changes: 0 additions & 21 deletions tests/python3-pip/main.tf

This file was deleted.

0 comments on commit df00955

Please sign in to comment.