Getting serverless with Python and Cloud Run
Tags: serverlessexpeditions, python
Published: 2020-10-02 19:09:05

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.
{% youtube s2TIWIzCftM %}
 
Source code for this blog post is available on GitHub.

{% github GoogleCloudPlatform/serverless-expeditions no-readme %}

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](https://cloud.google.com/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](https://flask.palletsprojects.com/en/1.1.x/). 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.

{% gist https://gist.github.com/abbycar/b52a0468cd038faeda5afc5b8127c3df file=form.html %}

A simple css file.

{% gist https://gist.github.com/abbycar/b52a0468cd038faeda5afc5b8127c3df file=style.css %}

And an app.py file where functions and routes are defined.

{% gist https://gist.github.com/abbycar/b52a0468cd038faeda5afc5b8127c3df file=app.py %}

For the full source of this project, check out our [repository on GitHub](https://github.com/GoogleCloudPlatform/serverless-expeditions/tree/master/using-python-on-google-cloud-with-cloud-run).


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.

{% gist https://gist.github.com/abbycar/b52a0468cd038faeda5afc5b8127c3df file=Dockerfile %}

Taking a closer look…
- `FROM` specifies a base Docker image, the Python runtime.
- `COPY` adds files from your computer's current directory to a directory inside the container.
- `RUN` installs Flask ,[Gunicorn](https://docs.gunicorn.org/en/stable/), and our currency converter's dependencies.
- `CMD` is 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](https://hub.docker.com/_/python) 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.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x7w03ny67yubwd161pge.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fv9nrk7fmnxbqd8okbe6.png)

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](https://cloud.google.com/monitoring) comes in.


Cloud Run has a Logs tab where you can see everything being logged by your service.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/upnp3hmw2igal4b2wuov.png)

To dig a little deeper, there’s [Error Reporting](https://cloud.google.com/error-reporting). Here we see a value error was triggered on line 20.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4xi00yi1nkmcvwh6kprl.png)

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](https://twitter.com/abbyapplebees) and [@martinomander](https://twitter.com/martinomander).