I spent a bit of time today poking around with Google Cloud Functions. The idea is very similar to AWS Lambda -- both tools allow you to run small, single purpose Python (or other) scripts in a serverless environment, avoiding the overhead of spinning up a compute instance, installing the appropriate software, configuring access, etc.
I first worked through the "Hello World" tutorial, which was essentially a function that would return "Hello <users name>!" or "Hello world!" message when the associated http endpoint was hit with or without a "name=<user name>" argument in the http request. That went fairly smoothly, though I try to force myself to type rather than copy/paste code when following tutorials, so there were a good deal of errors due to typos the first few tries.
But, like so many other things, there is usually a huge gap between the "Hello world" example and anything even somewhat approaching a real use case. This may be unavoidable given how many moving pieces there are in any "real" cloud deployment.
Because my eventual use case was to hit an API and download some data, I decided to tweak the "Hello World" program bit by bit to get closer to that. Luckily, there is a wonderful "fake api" available at http://jsonplaceholder.typicode.com/ which is great for testing.
It took a good hour of trial and error (and fixing more typos) to figure out which part of the sample "Hello World" code was just part of the sample script and which was needed as part of the Cloud Function deployment. (It turns out that there wasn't much "boilerplate" stuff needed.) It also took a while to realize that some Python packages that I needed weren't part of the standard library (and had to be specified in an associated requirements.txt file), while others were, and putting them in the requirements.txt file confused things.
I was working from the command line to deploy the scripts, and making changes to them in a text editor. It takes about ~2 min to deploy the script after making changes, and there were many times when I forgot to save the most recent changes before redeploying, confusing myself in the process when things didn't work as anticipated.
Luckily, I eventually realized that there is a good deal of logging that the service provides through the logs explorer (or through a gcloud command), which greatly improved my troubleshooting process. I think it took me 11 deployments to create a simple function that would hit the fake api and pull out the fake user names from the response json.

No comments:
Post a Comment