question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lacking examples of managing/deploying Python dependencies

See original GitHub issue

❓ Guidance Question

The Question

As mentioned in aws/aws-cdk#3660, we are lacking a good example of a more realistic Lambda which includes dependencies. How should a developer ensure that handlers are packaged with their required dependencies? The current examples/boilerplate code only seems to demonstrate managing deployment code (i.e. cdk dependencies) but does not demonstrate best-practice for handling runtime dependencies.

Perhaps the correct solution is to utilize Lambda Layers to package dependencies? If so, it would be helpful to have that demonstrated in an example project.

Whatever the solution, it should demonstrate:

  • separating runtime dependencies from build-time dependencies

Additionally, it would be nice if we could demonstrate:

  • supporting the concept of packaging different handlers with only the dependencies that they need

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
alukachcommented, May 2, 2020

@JamieMcKernanKaizen I ended up doing something like the following:

import os
import subprocess
from typing import List

from aws_cdk import (
    core,
    aws_lambda,
)

class MyStack(core.Stack):
    def __init__(
        self,
        scope: core.Construct,
        id: str,
        requirements_path: str = "./requirements.txt",
        layer_dir: str = "./.layer",
        **kwargs,
    ) -> None:
        super().__init__(scope, id, **kwargs)

        dependenciesLayer = self.create_dependencies_layer(
            requirements_path=requirements_path, output_dir=layer_dir
        )
        self.create_handler(
            layers=[dependenciesLayer],
        )

    def create_dependencies_layer(
        self, requirements_path: str, output_dir: str
    ) -> aws_lambda.LayerVersion:
        # Install requirements for layer
        if not os.environ.get("SKIP_PIP"):
            subprocess.check_call( 
                # Note: Pip will create the output dir if it does not exist
                f"pip install -r {requirements_path} -t {output_dir}/python".split()
            )
        return aws_lambda.LayerVersion(
            self, "HandlerDependencies", code=aws_lambda.Code.from_asset(output_dir)
        )
    
    def create_handler(
        self,
        layers: List[aws_lambda.LayerVersion],
    ):
        return aws_lambda.Function(
            self,
            "Some Function",
            code=aws_lambda.Code.from_asset('path/to/code'),
            handler='handlers.my_handler',
            runtime=aws_lambda.Runtime.PYTHON_3_7,
            layers=layers,
        )
4reactions
gmiretticommented, Jun 30, 2020

Making an aws-samples/aws-cdk-examples for python using bundling would be great.

Mainly because the example code in section “Bundling asset code” in CDK Python docs (v 1.47.1) doesn’t work without some tweaking ( as published in https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_lambda.README.html#id3 )

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for Python Dependency Management - Medium
Dependency management is like your city's sewage system. ... For example, just run pip install numpy to install numpy and its dependencies.
Read more >
How to Best Manage Python Dependencies - ActiveState
There are several different approaches to dealing with Python dependencies - Pinenv, Virtualenv or an alternate custom solution.
Read more >
A Comprehensive Guide To Python Dependency Management
Learn how to manage Python dependencies using pip. Know how to install, uninstall, and upgrade packages. Understand how to use the ...
Read more >
Managing Application Dependencies
For alternatives, see Other Tools for Application Dependency Management. Installing Pipenv¶. Pipenv is a dependency manager for Python projects. If you're ...
Read more >
How to Manage Python Dependencies in PySpark - Databricks
pyFiles configuration, but this functionality cannot cover many cases, such as installing wheel files or when the Python libraries are dependent ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found