Ajax/jQuery on AWS Lambda
I’ve been trying to learn more about AWS Lambda, at the same time learning some web development. For a newcomer to web application development, there is the question of whether you work on leveling up with traditional server based apps or “serverless”, aka FaaS, aka Lambda. I wanted to try converting a simple Flask app to Lambda, this is how I did it.
What project should I work on?
I’m using Zappa to deploy the Python app to AWS Lambda. Zappa makes it easy to upload an app without having to do a lot of preparation on AWS and packaging the bits manually. This is probably not useful to everyone but I wanted to avoid the overhead of running Lambda in order to get the app functioning before I learned it in detail, and Zappa helped with that.
What’s so interesting about that?
This rudimentary application introduces two things to me. First I haven’t done Ajax/jQuery in a Python application before. That’s not exciting to many folks but when the concept is new to you its hard to work out the mechanics of the process. Second I knew the term Document Object Model but didn’t know how to manipulate it nor how to initiate the Ajax call.
jQuery is intimidating too, but approachable if you start with the fundamentals. Now I know $.getJSON() means roughly the same thing as $.ajax() (give or take, its shorthand for the ajax call.) But wow is it confusing, for example what are the mechanics of not providing the optional second parameter (data), yet allowing the third optional handler (success) to work? I use that here.
Local webserver vs. Lambda
So after getting this to work using a local Flask server, how do we transition to serving it from AWS? I initially tried to just upload the same app with Zappa to Lambda, but while the inital load of the page worked, any time I clicked the “New Quote” button nothing happened. This had me furiously searching google for whether or not I needed a full REST API to work on AWS Lambda or some other changes. I did find out I probably needed to enable CORS on the API Gateway.
There were other potential answers for why it wasn’t working that I found out about. Did I need to use lambda.invoke or some similar mechanism to call the new_quote function via the onClick action in the browser? Did I need to setup a full REST API with Cognito, or some other API Gateway configuration that I was missing?
In the end I realized I was calling the new_quote function using a relative path, but I needed to call the full URL to the API Gateway that I already knew from performing “zappa deploy dev” earlier, so when I ran “zappa update” it used the same gateway. That change looked like:
For reference the Zappa settings for the project were: