aws-samples / cookiecutter-aws-sam-python

A Cookiecutter template to create a Serverless App based on Serverless Application Model (SAM) and Python 3.8
MIT No Attribution
221 stars 72 forks source link

Convert dummy example into a concrete example #36

Open heitorlessa opened 4 years ago

heitorlessa commented 4 years ago

Ideas to pick from when time allows:

kbakk commented 2 years ago

A few examples on things I find difficult which I think can help others:

heitorlessa commented 2 years ago

hey @kbakk thank you so much for that. Now that Powertools has a Router functionality, @michaelbrewer and I have been discussing revamping this with two options: Monolithic and Micro function - both with shared and Lambda Layer (build optimization + dev dependency).

https://awslabs.github.io/aws-lambda-powertools-python/latest/core/event_handler/api_gateway/#considerations

The monolithic option would take an hour to add since we already have a good project structure that works in both PyCharm and VSCode. The latter requires an additional CLI (e.g. PyInvoke) to support building separate functions while supporting relative and absolute imports, since SAM CLI discards the top level directory - the latter also needs additional cycles to ensure C bindings are built within a Docker Container.

kbakk commented 2 years ago

You may want to look into Poetry. I'm experimenting with this currently:

[tool.poetry]
# ...
packages = [
    { include = "my_app_dir", from = "functions/my_app_dir" },
    { include = "tests" }
]

This creates a .pth in the site-packages folder, I believe this is similar to modifying PYTHONPATH (for the local dev-environment).

cat $(poetry env info -p)/lib/python3.9/site-packages/my_project.pth
~/Code/my_project
~/Code/my_project/functions/my_app_dir

My handler is in functions/my_app_dir/my_app_dir/lambda_handler. The double-dir is to be able to say import my_app_dir.sub_module in the Lambda handler.

  MyLogicFn:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/my_app_dir
      Handler: my_app_dir.lambda_handler.create_asset
heitorlessa commented 2 years ago

Definitely - we use Poetry for Lambda Powertools and don’t look back.

On Tue, 30 Nov 2021 at 18:59, Kristoffer Bakkejord @.***> wrote:

You may want to look into Poetry. I'm experimenting with this currently:

[tool.poetry]# ...packages = [ { include = "my_app_dir", from = "functions/my_app_dir" }, { include = "tests" } ]

This creates a .pth in the site-packages folder, I believe this is similar to modifying PYTHONPATH (for the local dev-environment).

cat $(poetry env info -p)/lib/python3.9/site-packages/my_project.pth ~/Code/my_project ~/Code/my_project/functions/my_app_parent

My handler is in functions/my_app_dir/my_app_dir/lambda_handler. The double-dir is to be able to say import my_app_dir.sub_module in the Lambda handler.

MyLogicFn: Type: AWS::Serverless::Function Properties: CodeUri: functions/my_app_dir Handler: my_app_dir.lambda_handler.create_asset

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aws-samples/cookiecutter-aws-sam-python/issues/36#issuecomment-982881608, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZPQBCWOT7JBLOECLD2YOLUOUGH7ANCNFSM4RZUGLOQ .

marcellodesales commented 2 years ago

Hi @heitorlessa, do you have any update on this? I'd like to see both approaches you described at your comments...

hey @kbakk thank you so much for that. Now that Powertools has a Router functionality, @michaelbrewer and I have been discussing revamping this with two options: Monolithic and Micro function - both with shared and Lambda Layer (build optimization + dev dependency).

https://awslabs.github.io/aws-lambda-powertools-python/latest/core/event_handler/api_gateway/#considerations

The monolithic option would take an hour to add since we already have a good project structure that works in both PyCharm and VSCode. The latter requires an additional CLI (e.g. PyInvoke) to support building separate functions while supporting relative and absolute imports, since SAM CLI discards the top level directory - the latter also needs additional cycles to ensure C bindings are built within a Docker Container.