On this edition of Serverless Expeditions, we take a look at how to deploy a Cloud Run service written in Python.
Check out the video version of this blog post.
Source code for this blog post is available on GitHub.
Managing the resources for an app can be a challenge. Deploying your app in a serverless fashion is popular because it helps take care of that for you. Cloud Run is fully managed, meaning you don’t have to worry about infrastructure scaling as traffic changes. Since it’s container based, you can also use any language and framework. An added bonus is that you can redeploy with just one command.
Let’s say you and your friends are planning a foreign wine night. Most of the shops your group want to order from are in the EU and family owned. Their online stores don't display your country's currency (USD).
To get an accurate gauge on price, a unit conversion service that converts Euros to USD can be made and deployed to Cloud Run with Flask. Flask is a popular framework for creating Python web apps because it’s lightweight and easy to set up.
Your Cloud Run service has the basics:
An html file where a form gets user input.
A simple css file.
And an app.py file where functions and routes are defined.
For the full source of this project, check out our repository on GitHub.
Once you’ve created your service, Cloud Run only needs one extra file for deployment. No code changes needed.
This is the Docker file. Here you customize the runtime of your container to suit your needs exactly. Containers are a way to isolate your service to make it run the same no matter where it’s deployed.
Taking a closer look…
FROMspecifies a base Docker image, the Python runtime.
COPYadds files from your computer's current directory to a directory inside the container.
RUNinstalls Flask ,Gunicorn, and our currency converter's dependencies.
CMDis a command that starts your app inside the container and binds it to a port. App:app means import the app from the app.py file.
If you want to run a different version of Python, it’s no big deal. With Cloud Run you can run any version of Python you want as long as there’s a Python base docker image available for it.
With Docker file in hand, you can now kick off your first deployment. You first build a container with Cloud Build. This bundles up your code along with everything you’ve added in your Docker file and pushes it to the Container Registry. Container Registry is where your container images are stored. Finally, you deploy the service to Cloud Run.
All this is handled with just two commands. From your terminal, run the following commands to build a container image and deploy the service using that image.
$ gcloud builds submit --tag gcr.io/PROJECT_ID/euro-to-usd $ gcloud run deploy --image gcr.io/PROJECT_ID/euro-to-usd --platform managed
And that’s it! Your Cloud Run service is now deployed.
Going to the Cloud Run section of Google Cloud now shows your deployed service and its URL.
Here you also have access to important metrics and the Revisions tab, where you can easily redirect and split traffic to different versions of service if you’ve deployed it more than once. This is handy if you discover a breaking change and want to temporarily roll back.
But what if your friends hit an error when trying your service out? How could you figure out what’s going wrong? That’s where Cloud Monitoring comes in.
Cloud Run has a Logs tab where you can see everything being logged by your service.
To dig a little deeper, there’s Error Reporting. Here we see a value error was triggered on line 20.
After taking a look at the stack trace, making a quick fix, and redeploying, you now have a working app to share. Goodbye Euros, hello USD!
Now, with correctly calculated wine prices, the whole group is ready to buy fancy wine that fits their budget.
About Serverless Expeditions
Serverless Expeditions is a fun and cheeky video series that looks at what serverless means and how to build serverless apps with Google Cloud.
Follow these hosts on Twitter at @abbyapplebees and @martinomander.
Top comments (0)