smithy-rs/rust-runtime/aws-smithy-http-server-python
John DiSanti 4835af9d6b
Make `RustReservedWordsSymbolProvider` configurable (#2382)
* Remove `toEnumVariantName` from `RustSymbolProvider`

The `toEnumVariantName` function existed on symbol provider to work
around enum definitions not being shapes. In the future when we refactor
to use `EnumShape` instead of `EnumTrait`, there will be `MemberShape`s
for each enum member. This change incrementally moves us to that future
by creating fake `MemberShape`s in the enum generator from the enum
definition.

* Fix escaping of `Self` in symbol providers

* Clean up an old hack

* Make `RustReservedWordsSymbolProvider` configurable

* Update changelog

* Incorporate feedback
2023-03-13 19:15:55 +00:00
..
examples Upgrade Kotlin and Ktlint (#2392) 2023-02-22 18:21:06 +00:00
src Python: Add tests and fix the issues with `Timestamp` and `ByteStream` (#2431) 2023-03-07 10:04:56 +00:00
Cargo.toml Make `RustReservedWordsSymbolProvider` configurable (#2382) 2023-03-13 19:15:55 +00:00
LICENSE Implement Python runtime crate and shared socket (#1399) 2022-05-20 17:08:57 +00:00
README.md formatting: run pre-commit on all files (#2236) 2023-01-20 16:02:26 +00:00

README.md

aws-smithy-http-server-python

Server libraries for smithy-rs generated servers, targeting pure Python business logic.

Running servers on AWS Lambda

aws-smithy-http-server-python supports running your services on AWS Lambda.

You need to use run_lambda method instead of run method to start the custom runtime instead of the Hyper HTTP server.

In your app.py:

from pokemon_service_server_sdk import App
from pokemon_service_server_sdk.error import ResourceNotFoundException

# ...

# Get the number of requests served by this server.
@app.get_server_statistics
def get_server_statistics(
    _: GetServerStatisticsInput, context: Context
) -> GetServerStatisticsOutput:
    calls_count = context.get_calls_count()
    logging.debug("The service handled %d requests", calls_count)
    return GetServerStatisticsOutput(calls_count=calls_count)

# ...

-app.run()
+app.run_lambda()

aws-smithy-http-server-python comes with a custom runtime so you should run your service without any provided runtimes. You can achieve that with a Dockerfile similar to this:

# You can use any image that has your desired Python version
FROM public.ecr.aws/lambda/python:3.8-x86_64

# Copy your application code to `LAMBDA_TASK_ROOT`
COPY app.py ${LAMBDA_TASK_ROOT}

# When you build your Server SDK for your service, you will get a Python wheel.
# You just need to copy that wheel and install it via `pip` inside your image.
# Note that you need to build your library for Linux, and Python version used to
# build your SDK should match with your image's Python version.
# For cross compiling, you can consult to:
# https://pyo3.rs/latest/building_and_distribution.html#cross-compiling
COPY wheels/ ${LAMBDA_TASK_ROOT}/wheels
RUN pip3 install ${LAMBDA_TASK_ROOT}/wheels/*.whl

# You can install your application's other dependencies listed in `requirements.txt`.
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

# Create a symlink for your application's entrypoint,
# so we can use `/app.py` to refer it
RUN ln -s ${LAMBDA_TASK_ROOT}/app.py /app.py

# By default `public.ecr.aws/lambda/python` images comes with Python runtime,
# we need to override `ENTRYPOINT` and `CMD` to not call that runtime and
# instead run directly your service and it will start our custom runtime.
ENTRYPOINT [ "/var/lang/bin/python3.8" ]
CMD [ "/app.py" ]

See https://docs.aws.amazon.com/lambda/latest/dg/images-create.html#images-create-from-base for more details on building your custom image.

This crate is part of the AWS SDK for Rust and the smithy-rs code generator. In most cases, it should not be used directly.