Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An AWS lamda Python template that supports dependencies #799

Open
JonathanWoollett-Light opened this issue Jun 8, 2024 · 1 comment
Open
Labels
kind/enhancement Improvements or new features

Comments

@JonathanWoollett-Light
Copy link

JonathanWoollett-Light commented Jun 8, 2024

Hello!

  • Vote on this issue by adding a 👍 reaction
  • If you want to implement this feature, comment to let us know (we'll work with you on design, scheduling, etc.)

Issue details

It is normal to want to use non-std packages when writing Python code.

There should be a template with a workflow that supports this.

The current template https://www.pulumi.com/templates/serverless-application/aws/ does not support this.

I am currently trying to figure out how to add a Python dependency to my Lambda function. I shouldn't need stack overflow answers for this. This is something 90% of users will need to do and should be have documentation for.

I would argue any template that doesn't include support for this, is not really useful as this is such a fundamental thing.

Surely someone thought of this when creating these templates? If its something where there are multiple approaches there should be multiple template or guides to these approaches.

@JonathanWoollett-Light JonathanWoollett-Light added kind/enhancement Improvements or new features needs-triage Needs attention from the triage team labels Jun 8, 2024
@cnunciato
Copy link
Contributor

cnunciato commented Jun 13, 2024

Hi @JonathanWoollett-Light! Thanks for submitting this issue -- and you're right, it'd be good to provide an example of how to do this (or perhaps better, update all serverless templates to include this across the board). I'll leave this issue open to track that.

In the meantime, if you're still stuck, here's an example that might help.

It sounds like you're using the serverless-aws-python template. If you add the following lines to the top of that template, you can add a step to run pip install before the deployment begins, then let the template bundle the resulting assets into the Lambda function for you:

# ...
import subprocess

# Install Python dependencies.
result = subprocess.run(
    ["pip", "install", "-r", "requirements.txt", "--target", ".", "--upgrade"],
    stdout=subprocess.PIPE,
    cwd="./function",
    check=True,
)

# ...

Then, in ./function/requirements.txt, you can add any dependencies you need and reference them in handler.py in the usual way. Here's what I did:

In ./function/requirements.txt:

cowsay

In ./function/handler.py:

from datetime import datetime
import cowsay

def handler(event, context):
    return {
        'statusCode': 200,
        'body': cowsay.get_output_string('cow', datetime.now().isoformat())
    }

And finally, in ./www/index.html (just to get things to render right):

<h1>Serverless with Pulumi</h1>

<p>The current time is: </p>

<pre id="date"></pre>.

<script>
setInterval(() => {
    fetch("date")
    .then(response => response.text())
    .then(data => document.getElementById("date").innerText = data)
}, 1000);
</script>

The result:

cowsay

The AWS docs explain the requirements from Lambda's perspective: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html

Full Pulumi program example here for reference:

https://github.com/cnunciato/serverless-aws-python-with-deps

Hope that helps, and apologies for the rough edges!

@cnunciato cnunciato removed the needs-triage Needs attention from the triage team label Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements or new features
Projects
None yet
Development

No branches or pull requests

2 participants