Skip to content

Instantly share code, notes, and snippets.

@jakebrinkmann
Last active April 22, 2024 13:42
Show Gist options
  • Save jakebrinkmann/ccd3537a61894fb676b10fa50fc2f9a3 to your computer and use it in GitHub Desktop.
Save jakebrinkmann/ccd3537a61894fb676b10fa50fc2f9a3 to your computer and use it in GitHub Desktop.
Using the Python poetry tool to manage dependencies of an AWS Serverless (AWS-SAM-CLI) project.
##### https://flake8.pycqa.org/en/latest/user/configuration.html
[flake8]
ignore =
# D100: Missing docstring at top of file
D100,
# D104: Missing docstring in __init__.py
D104,
# D401: Docstring first line should be imperative
D401,
# D101 Missing docstring in public class
D101,
# D102 Missing docstring in public method
D102,
exclude =
# No need to traverse our git directory
.git,
# There's no value in checking cache directories
__pycache__
# Limit on the calculated McCabe complexity of a unit of code
max-complexity = 10
# Black likes 88, it's a lucky number
max-line-length = 88

python-poetry-aws-sam

The goal is to demonstrate using the Python poetry tool to manage dependencies of an AWS Serverless project.

Dependencies

## Initialize project
poetry init --no-interaction

## Install dependencies, like aws-sam-cli
poetry install

## Initialize Lambda project
poetry run sam init \
  --runtime python3.9 \
  --dependency-manager pip \
  --app-template hello-world \
  --name sam-app \
  --output-dir .

## Add a dependency
poetry add sqlalchemy

## Add a development utility
poetry add --dev mypy

## Export Serverless dependencies
poetry export --without-hashes > hello_world/requirements.txt

## Build AWS Serverless project
poetry run sam build --use-container
# Used to create Docker image for unit testing python aws-lambda
#
# Build with:
# docker build -f Dockerfile.test -t python-poetry-aws-sam:latest .
FROM public.ecr.aws/sam/build-python3.9:latest
COPY poetry.lock pyproject.toml ./
ENV POETRY_VIRTUALENVS_CREATE=false
RUN pip install --no-cache-dir poetry==1.1.11 \
&& poetry install --no-root
CMD ["/bin/sh"]
# Test with:
# docker run -i -v ${PWD}:/var/task:Z -w /var/task python-poetry-aws-sam:latest flake8 src/
# docker run -i -v ${PWD}:/var/task:Z -w /var/task python-poetry-aws-sam:latest pytest tests/
[tool.poetry]
name = "python-poetry-aws-sam"
version = "0.1.0"
description = ""
authors = ["Jake Brinkmann <[email protected]>"]
#packages = [
# { include = "my_sub_package", from = "src" }
#]
[tool.poetry.dependencies]
python = "^3.9"
[tool.poetry.dev-dependencies]
black = "*"
coverage = {version = "*", extras = ["toml"]}
flake8 = "*"
flake8-black = "*"
flake8-bugbear = "*"
flake8-docstrings = "*"
flake8-isort = "*"
flake8-unused-arguments = "*"
isort = "*"
pep8-naming = "*"
pytest = "*"
pytest-cov = "*"
pytest-mock = "*"
aws-sam-cli = "*"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
##### isort configuration https://pycqa.github.io/isort/docs/configuration/config_files.html
[tool.isort]
profile = "black"
default_section = "THIRDPARTY"
sections = "FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER"
skip = "__init__.py"
##### black configuration https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file
[tool.black]
line-length = 88
target_version = ['py39']
##### pytest configuration: http://doc.pytest.org/en/latest/customize.html
[tool.pytest.ini_options]
minversion = "6.0"
python_files = [ "test_*.py" ]
norecursedirs = [
".git",
".pytest_cache",
".aws-sam"
]
filterwarnings = [
"error::UserWarning",
"error::DeprecationWarning",
"error::PendingDeprecationWarning"
]
addopts = """
--color=yes
-p no:cacheprovider
--verbose
--junitxml=report.xml
--cov-report term-missing:skip-covered
--cov-report xml
--cov src
"""
##### coverage configuration: https://coverage.readthedocs.io/en/latest/config.html
[tool.coverage]
[tool.coverage.paths]
source = ["src"]
[tool.coverage.run]
branch = true
source = ["src"]
omit = ["tests/*"]
[tool.coverage.report]
show_missing = true
fail_under = 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment