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

Falcon app on AWS lambda ? #1552

Open
Gorbus opened this issue Jun 4, 2019 · 18 comments
Open

Falcon app on AWS lambda ? #1552

Gorbus opened this issue Jun 4, 2019 · 18 comments

Comments

@Gorbus
Copy link

Gorbus commented Jun 4, 2019

Hi,

I have a little api working locally thanks to falcon and it can connect to my AWS RDS postgresql database. It is mainly a GET API as I already have the data I need in the database but need them to be accessible.

The code is not really relevant for the question except that I need to import two packages :

import falcon
import psycopg2

and I am using gunicorn to run it locally.

So far so good, However I am trying to reproduce this on a AWS lambda function with an API gateway and I just cannot manage to have it run. (idea would be to have a publicly accessible api) I started a python lambda with the hello world template and copied my code. However I am pretty sure that the packages I import are not there (falcon and psycopg2). How can I have them in it ? Also is gunicorn needed to run the function or not ?

Afterwards I also tried to create the api gateway in aws but it didnt want to work when I tried to go to the http address generated : I got wrong authentification token.

I am not a python dev, rather a Js one but for this one I need it to be in python as the rest get function is complex and already exists in python.

If you have any lead or tutorial to explain how to convert my little 100 lines of code with its 2/3 dependencies into a working http api online on Lambda, I'd be more than glad. I also tried using Zappa but somehow went from error to error and as my code is pretty simple and just need a few packages I though I could probably do it manually or with a little help from the falcon community.

Thanks for any lead.

@open-collective-bot
Copy link

Hi 👋,

Thanks for using Falcon. The large amount of time and effort needed to
maintain the project and develop new features is not sustainable without
the generous financial support of community members like you.

Please consider helping us secure the future of the Falcon framework with a
one-time or recurring donation.

Thank you for your support!

@vytas7
Copy link
Member

vytas7 commented Jun 4, 2019

Hi @Gorbus ,
I have admittedly never tried this combination myself, but FWIW @jmvrbanac has done some basic protyping on Lambda ➡� WSGI adapter for Falcon: https://github.com/jmvrbanac/falcon-lambda . Hopefully this could serve as inspiration!

Furthermore, do not hesitate to ask questions/share insights on our Gitter channels: https://falconframework.org/#sectionCommunity

Kindly,
Vytas

@jmvrbanac
Copy link
Member

jmvrbanac commented Jun 4, 2019

@Gorbus It's been awhile since I ran Falcon on Lambda as I'm no longer on a project that requires it these days. However, when I did, I was able to use the falcon-lambda package to handle most of the interaction issues. Here is a quick and dirty example of how you would hook it up in the lambda handler:

import logging
from falcon_lambda import logger, wsgi
from example.app import MyFalconApp

logger.setup_lambda_logger(logging.DEBUG)
log = logging.getLogger(__name__)

api = MyFalconApp()


def lambda_handler(event, context):
    log.debug(event)
    resp = wsgi.adapter(api, event, context)
    log.debug(resp)
    return resp

API Gateway Definition:

swagger: "2.0"
info:
  version: "v1"
  title: "My App"
  description: ""
schemes:
  - https
consumes:
  - application/json
produces:
  - application/json
paths:
  /{proxy+}:
    x-amazon-apigateway-any-method:
      responses: {}
      x-amazon-apigateway-integration:
        type: "aws_proxy"
        credentials: "${lambda_role}"
        uri: "${lambda_arn}"
        httpMethod: "POST"

From there, I believe the biggest annoyance going forward is packaging your function. The product I was on had their own lambda packaging system, so unfortunately I'm not much help there, but hopefully this gets you pointed in the right direction.

@Gorbus
Copy link
Author

Gorbus commented Jun 5, 2019

Hey to both of you,

Thank you for your quick answer, I can see Falcon is a very nice and active community.

Unfortunately my knowledge in python and lambda are very low, I was hoping there would be a library that would do the hard work (including packaging) for me just as claudiajs does it for lambda in nodejs.

I guess I'll have to give up on the lambda solution and go for a vps or anything else to run my api with falcon (or any other proposal of easy to deploy solution for mini falcon app ??)

@xorb0ss
Copy link

xorb0ss commented Jul 30, 2019

You can use Zappa to package the function for you

@antb123
Copy link

antb123 commented Sep 12, 2019

I noticed that google cloud functions has a falcon example for deployment. Its pretty easy to deploy any pip packages there as well.

@vytas7
Copy link
Member

vytas7 commented May 29, 2020

Posting for reference what I previously googled:

Apparently there is a book out there on Python serverless whose newer edition also covers Falcon: https://www.oreilly.com/library/view/building-serverless-python/9781788837613/1d0f32fb-d94f-460e-b859-066d5adeba22.xhtml
Their examples from that book appear to be open source: https://github.com/PacktPublishing/Building-Serverless-Python-Web-Services-with-Zappa/tree/master/Chapter07

@vytas7
Copy link
Member

vytas7 commented Jul 13, 2020

I have recently heard from our community (@zacharyabresch) that this package also gets the job done: https://github.com/truckpad/aws-lambda-wsgi .
Maybe we should consider adding a couple of snippets to our Recipes or FAQ sections.

@zacharyabresch
Copy link

Ya know, I found this issue early on in my research and forgot all about it.

Can confirm, this module is working in my project to send AWS Lambda events (from API Gateway) to Falcon. This comes with one caveat ... my project is only working with incoming requests. Currently, API Gateway responses are timing out but I have not determined the cause of this. As this app involves a lot of file uploading & downloading, it is more likely my source than the above-mentioned WSGI module).

I'll respond back with my results once I can verify that requests are making the round trip as expected.

@vytas7
Copy link
Member

vytas7 commented Jul 14, 2020

Hi again @zacharyabresch ,
regarding functions timing out, you might want to check Configuring functions in the AWS Lambda console and AWS Lambda limits.

The default timeout is only 3 seconds, which may not be enough to handle file uploads:

Timeout – The amount of time that Lambda allows a function to run before stopping it. The default is 3 seconds. The maximum allowed value is 900 seconds.

Furthermore, note that memory is scaled together with CPU:

Lambda allocates CPU power linearly in proportion to the amount of memory configured. At 1,792 MB, a function has the equivalent of one full vCPU (one vCPU-second of credits per second).

Using a low value (is 128MB default?) might give you a very poor vCPU share, which in turn may further exacerbate the timings issue.

I have never used Lambda for HTTP though, only for scaling out CPU-expensive background jobs to the cloud.

@zacharyabresch
Copy link

Good call but, in this case, it's the API Gateway timeout I'm hitting which is locked at 30 seconds max. After further debugging, I was able to prove that my issue is unrelated to the WSGI adapter so this can be considered a viable solution. In that process I swapped out aws-lambda-wsgi for the one it was based on, awsgi and both worked. I only mention this because neither repo seems to be super active but awsgi has had the most recent changes.

Regardless, these are both viable solutions to running Falcon inside a Lambda. I plan on writing a post about it someday. :)

@vytas7
Copy link
Member

vytas7 commented Jul 17, 2020

Aha, sorry!
It would be awesome if you could share a post on this.

I'm thinking whether we should actually document Falcon on AWS Lambda as a Deployment article 🤔

@zacharyabresch
Copy link

I started a draft for this post last night. Hoping to work on it over the week and get an example posted soon.

@zacharyabresch
Copy link

Got sample code repository up (https://github.com/zacharyabresch/falcon-on-aws-lambda) and have tested it in AWS. Learned something too ... the API Gateway must be using the 1.0 payload version which is only available, AFAICT, with HTTP APIs, not REST. Explains some things I was seeing when hooking my work project up. Post forthcoming.

@ilietrasca
Copy link

Got sample code repository up (https://github.com/zacharyabresch/falcon-on-aws-lambda) and have tested it in AWS. Learned something too ... the API Gateway must be using the 1.0 payload version which is only available, AFAICT, with HTTP APIs, not REST. Explains some things I was seeing when hooking my work project up. Post forthcoming.

I just created a PR for you repo, when I tried to run the example that you provided in Lambda I got the error::
module initialization error: module 'falcon' has no attribute 'App'

I replaced App with API() and now everything is working properly, I have an API function in front of a Lambda falcon API and everything is working (y)

@vytas7
Copy link
Member

vytas7 commented Jul 31, 2020

@ilietrasca Just to clarify App vs API: Falcon 2.0 has only API, which will be renamed to App in Falcon 3.0 (currently the first alpha is available). API will be maintained as a compatibility alias.
@zacharyabresch was testing the unstable version.

@zacharyabresch
Copy link

@ilietrasca Thanks for the heads up! As @vytas7 mentioned, I'm using the new API but I'll make a note of that in the README on my project.

@CaselIT
Copy link
Member

CaselIT commented Feb 2, 2021

For falcon 3 with asgi I think we can check if https://github.com/jordaneremieff/mangum works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants