https://pulumi.com logo
#python
Title
# python
b

brave-knife-93369

03/18/2021, 10:50 AM
Hi all. I'd like to understand how to package my lambda functions (written in python) from pulumi (also using python). Am I expected to create a pulumi.Archive of my whole project, then pass that to aws.lambda.Function() ? Right now we build the zip manually, I'd like to switch to a cleaner way if that's an option
b

brave-planet-10645

03/18/2021, 12:31 PM
So what I've done previously is to have a folder with the handler code in it and then in the root have a bash script that does the
pip install
and then pulumi up. Then in the pulumi code for the function have something like
code=pulumi.AssetArchive({".": pulumi.FileArchive("app")})
Pulumi will then zip everything up for you and push it up
so something like:
Copy code
myfunction = aws.lambda_.Function(
    "myfunction",
    memory_size=256,
    runtime="python3.8",
    handler="app.handler",
    code=pulumi.AssetArchive({".": pulumi.FileArchive("app")}),
    role=function_role.arn,
    timeout=60,
)
And then the bash script is something like:
Copy code
#!/bin/bash

pushd ./app

pip3 install -r requirements.txt -t .

popd

pulumi up
so I just need to run the bash script to install the requirements and run pulumi up
A lot of text, but hopefully that helps
🙌 2
b

brave-knife-93369

03/31/2021, 8:31 AM
So, thanks to your help I now have something I'm not to unhappy with - I wrapped the pip installing of dependencies into a custom (dynamic) pulumi resource/provider, that gets called before the lambda. It's still a bit shaky (in particular, Asset/File archives can't take an Output in apparently, which is a bit messy), but I'll probably share my code once it's a bit cleaner
2 Views