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

Recommended packaging setup for SAM stacks #7925

Open
basvandriel opened this issue Mar 7, 2025 · 0 comments
Open

Recommended packaging setup for SAM stacks #7925

basvandriel opened this issue Mar 7, 2025 · 0 comments
Labels
stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.

Comments

@basvandriel
Copy link

I'm currently working on an project that uses multiple AWS SAM stacks.
Some include one or more lambda's and various resources using Python 3.12.

We want the code to be easily testable and decoupled from eachother. Also, we want to keep our deployed lambda functions small, we don't want to deploy the whole project.
Per stack, I was thinking the underneath structure.

# lambda one code
~/project_sam_stack_one/template.yaml
~/project_sam_stack_one/src/lambda_one/__init__.py
~/project_sam_stack_one/src/lambda_one/module_one.py
~/project_sam_stack_one/src/lambda_one/module_one.py
~/project_sam_stack_one/src/lambda_one/somedir/__init__.py
~/project_sam_stack_one/src/lambda_one/somedir/module_nested.py

# lambda two code
~/project_sam_stack_one/src/lambda_two/__init__.py
~/project_sam_stack_one/src/lambda_two/module_one.py
~/project_sam_stack_one/src/lambda_two/module_one.py
~/project_sam_stack_one/src/lambda_two/somedir/__init__.py
~/project_sam_stack_one/src/lambda_two/somedir/module_nested.py

# shared library
~/project_sam_stack_one/src/shared_code/__init__.py
~/project_sam_stack_one/src/shared_code/postgres.py

# testing 
~/project_sam_stack_one/tests/...

# packaging to make the project installable and easy to test
~/project_sam_stack_one/pyprojerct.toml

When then deploying the lamdbas, to ensure we don'at deploy the full project, we set the CodeUri to each of the lambdas:

~/project_sam_stack_one/src/lambda_two/

This means, from within the runtime of the lambda, the PYTHONPATH will be set to the CodeUri. This breaks.

The option I can think of: doubling the directory structure to simulate the package being installedModify the PYTHONPATH and double your directory structure to
For this approach, the lambda packages are doubled. This is inspired by this article, doubling the directory structure.

# lambda one code
~/project_sam_stack_one/template.yaml
~/project_sam_stack_one/src/lambda_one/__init__.py
~/project_sam_stack_one/src/lambda_one/module_one.py
~/project_sam_stack_one/src/lambda_one/module_one.py
~/project_sam_stack_one/src/lambda_one/somedir/__init__.py
~/project_sam_stack_one/src/lambda_one/somedir/module_nested.py

# lambda two code
~/project_sam_stack_one/src/lambda_two/__init__.py
~/project_sam_stack_one/src/lambda_two/module_one.py
~/project_sam_stack_one/src/lambda_two/module_one.py
~/project_sam_stack_one/src/lambda_two/somedir/__init__.py
~/project_sam_stack_one/src/lambda_two/somedir/module_nested.py

# shared library
~/project_sam_stack_one/src/shared_code/__init__.py
~/project_sam_stack_one/src/shared_code/postgres.py

# testing 
~/project_sam_stack_one/tests/...

# packaging to make the project installable and easy to test
~/project_sam_stack_one/pyprojerct.toml

For local development, this requires the PYTHONPATH to be modified, but feels weird with importing.

Questions

  • Aside from the list above, are there any othere recommended approaches?
  • Can we implement something in the application to ease this process?
@basvandriel basvandriel added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.
Projects
None yet
Development

No branches or pull requests

1 participant