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

Lambda support for Python servers #1807

Merged
merged 8 commits into from
Oct 11, 2022
Merged

Lambda support for Python servers #1807

merged 8 commits into from
Oct 11, 2022

Conversation

unexge
Copy link
Contributor

@unexge unexge commented Oct 3, 2022

This PR exposes app.run_lambda() function to allow Python servers to run on Lambda. Closes #1750.

Why we use custom Rust runtime

We could also use builtin Python runtime, I decided to use custom Rust runtime because there was some performance overheads.

If you use Python runtime:

  • Receive requests with Python (Python)
  • Pass requests to Rust (Rust)
  • You basically receive a JSON object in API Gateway schema and you need to convert it to Hyper Request, this is handled by lambda_http crate but it is not designed to be reusable and requires some modification/copying (Rust)
  • Call Python handler to get result (Python)
  • Convert that result to Http response (Rust)
  • Pass that result to Python (Python)
  • Finally return response to Lambda runtime to complete (Python)

If you use Rust runtime:

  • Receive Hyper Request with Rust (Conversion from JSON object to Hyper Request is already handled by lambda_http crate) (Rust)
  • Call Python handler to get result (Python)
  • Convert that result to Http response (Rust)
  • Finally return response to Lambda runtime to complete (Rust)

Testing

It can be tested with a Dockerfile similar to this:

FROM public.ecr.aws/lambda/python:3.8-x86_64

COPY app.py ${LAMBDA_TASK_ROOT}
# Shared library should be on the same folder with application code, so it can be imported
COPY lib_pokemon_service_server_sdk.so ${LAMBDA_TASK_ROOT}

RUN ln -s ${LAMBDA_TASK_ROOT}/app.py /app.py

# override `ENTRYPOINT` to not call Python runtime, so we can use Rust runtime
ENTRYPOINT [ "/var/lang/bin/python3.8" ] 
CMD [ "/app.py" ]

@unexge unexge added the python-server Python server SDK label Oct 3, 2022
@github-actions
Copy link

github-actions bot commented Oct 3, 2022

A new generated diff is ready to view.

A new doc preview is ready to view.

@github-actions
Copy link

github-actions bot commented Oct 3, 2022

A new generated diff is ready to view.

A new doc preview is ready to view.

@unexge unexge force-pushed the unexge/python-lambda branch from 7641106 to c1cae65 Compare October 5, 2022 10:28
@unexge unexge marked this pull request as ready for review October 5, 2022 10:28
@unexge unexge requested a review from a team as a code owner October 5, 2022 10:28
@github-actions
Copy link

github-actions bot commented Oct 5, 2022

A new generated diff is ready to view.

A new doc preview is ready to view.

rust-runtime/aws-smithy-http-server-python/src/server.rs Outdated Show resolved Hide resolved
Comment on lines +31 to +33

-app.run()
+app.run_lambda()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about renaming the entrypoint of the standard server from run() to run_server() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think run() is clearer. I don't know if it is idiomatic but, if we decide to unify run_lambda and any possible run_* versions into a big run function with keyword arguments like run(lambda=True, foo="bar", ...) that would allow us to do without renaming it again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, makes sense!

@@ -22,6 +22,7 @@ bytes = "1.2"
futures = "0.3"
http = "0.2"
hyper = { version = "0.14.20", features = ["server", "http1", "http2", "tcp", "stream"] }
lambda_http = "0.6.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker for this PR, but I would probably create an issue to feature-gate lambda in the future so that we do not impose this dependency on all customers. I am not even sure if its worth it thou.. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes good point. But I'm not sure if we currently support to generate SDKs with specific features on dependencies?

@unexge unexge force-pushed the unexge/python-lambda branch from 35d40d6 to 2b2876d Compare October 5, 2022 13:32
@github-actions
Copy link

github-actions bot commented Oct 5, 2022

A new generated diff is ready to view.

A new doc preview is ready to view.

Copy link
Contributor

@crisidev crisidev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good and I am very happy to see we can use this from the pure python lambda context! Are you planning to add an example of the Python code here?

@unexge
Copy link
Contributor Author

unexge commented Oct 5, 2022

This looks good and I am very happy to see we can use this from the pure python lambda context! Are you planning to add an example of the Python code here?

By example code you mean like having Lambda example of Pokemon service? If so, I think there is no need for that because only code change is changing app.run() to app.run_lambda(). What do you think?

@unexge unexge force-pushed the unexge/python-lambda branch from 83b7a96 to 576faf7 Compare October 6, 2022 13:54
@github-actions
Copy link

github-actions bot commented Oct 6, 2022

A new generated diff is ready to view.

A new doc preview is ready to view.

@github-actions
Copy link

github-actions bot commented Oct 7, 2022

A new generated diff is ready to view.

A new doc preview is ready to view.

@unexge unexge force-pushed the unexge/python-lambda branch from ba5576c to 5e5f672 Compare October 10, 2022 10:43
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@crisidev crisidev self-requested a review October 10, 2022 15:04
@unexge unexge force-pushed the unexge/python-lambda branch from 5e5f672 to 424f9f5 Compare October 11, 2022 10:15
@unexge unexge force-pushed the unexge/python-lambda branch from 516fbd1 to 32c8521 Compare October 11, 2022 10:27
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@unexge unexge merged commit eb6ffe3 into main Oct 11, 2022
@unexge unexge deleted the unexge/python-lambda branch October 11, 2022 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python-server Python server SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Python] Lambda support
3 participants