pulumi / templates

Templates used by `pulumi new`

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

An AWS lamda Python template that supports dependencies

JonathanWoollett-Light opened this issue Β· comments

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.

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!