Skip to content

Commit

Permalink
Download Python packages according to requested runtime
Browse files Browse the repository at this point in the history
Packages should be downloaded for the Python version of the lambda's
runtime requested and not according to the version of the Python
interpreter running e3-aws.
  • Loading branch information
pierretr committed Sep 15, 2023
1 parent 6fe7fc8 commit 7ac6f0f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/e3/aws/troposphere/awslambda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,14 @@ def create_data_dir(self, root_dir: str) -> None:

# Install the requirements
if self.requirement_file is not None:
assert self.runtime is not None
p = Run(
[
sys.executable,
"-m",
"pip",
"install",
f"--python-version={self.runtime.lstrip('python')}",
f"--target={package_dir}",
"-r",
self.requirement_file,
Expand Down
35 changes: 35 additions & 0 deletions tests/tests_e3_aws/troposphere/awslambda/awslambda_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import pytest
import json
import io
from unittest.mock import patch
import sys

from flask import Flask, send_file
from troposphere.awslambda import (
Expand Down Expand Up @@ -395,6 +397,39 @@ def test_pyfunction(stack: Stack) -> None:
assert stack.export()["Resources"] == EXPECTED_PYFUNCTION_TEMPLATE


def test_pyfunction_with_requirements(tmp_path, stack: Stack) -> None:
"""Test PyFunction creation."""
stack.s3_bucket = "cfn_bucket"
stack.s3_key = "templates/"
code_dir = tmp_path

with patch("e3.aws.troposphere.awslambda.Run") as mock_run:
mock_run.return_value.status = 0
PyFunction(
name="mypylambda",
description="this is a test",
role="somearn",
runtime="python3.9",
code_dir=str(code_dir),
handler="app.main",
requirement_file="requirements.txt",
).create_data_dir("dummy")
# Ensure the right pip command is called
mock_run.assert_called_once_with(
[
sys.executable,
"-m",
"pip",
"install",
"--python-version=3.9",
"--target=dummy/Mypylambda/package",
"-r",
"requirements.txt",
],
output=None,
)


def test_pyfunction_policy_document(stack: Stack) -> None:
"""Test cfn_policy_document of PyFunction."""
stack.s3_bucket = "cfn_bucket"
Expand Down

0 comments on commit 7ac6f0f

Please sign in to comment.