ABBY CAREY: Hey, Martin.
How's it going?
MARTIN OMANDER: Hey, Abby.
Well, I'm having a European wine party with a group of friends
soon, and we each want to buy a vintage wine from a small wine
store in Paris.
The website shows how many euros it is,
but it doesn't convert to US dollars.
ABBY CAREY: You know, I think we could build you
a new tool with Python that'll save you
and your friends some conversion time.
I'll show you how.
[THEME MUSIC]
ABBY CAREY: Python is one of the most popular programming
languages and growing.
It's well suited for a number of use cases,
including web applications, machine learning, and big data.
Cloud Run lets you use any runtime you want,
making it easy to deploy Python in a serverless way.
Cloud Run is also fully managed, meaning
you don't have to worry about infrastructure scaling
if your service starts getting a ton of traffic.
MARTIN OMANDER: That sounds awesome.
Do I have to change my Python code
to get it to run on Cloud Run?
ABBY CAREY: If you have an existing stateless Python app,
all you need to do is add one file to deploy
a surface to Cloud Run.
No code changes needed.
We like to use Flask for small services
like this because it's a lightweight framework that's
easy to set up.
Our service has the basics, an HTML
file where we create a form to get user input, a simple CSS
file, and an app.py file where we set routes
and define functions.
MARTIN OMANDER: OK.
That looks exactly like the web apps I've built before.
What is that extra file I need for Cloud Run?
ABBY CAREY: The last file we need to define
is the Docker file.
This allows us to customize the runtime of our container
to suit our needs exactly.
Containers are a way to isolate our application
to make it run the same no matter where it's deployed.
When creating a Docker file, we first
need to specify a base Docker image with the front command.
This is where we set our Python runtime.
The copy command adds files from your Docker client's
current directory.
The run command installs Flask, gunicorn, and currency
converter dependencies for our service.
And finally, CMD is a command to start our application
inside the container and binds it to a port.
The app app at the end means import our app
from the app.py file.
MARTIN OMANDER: What if I want to use
a different version of Python?
ABBY CAREY: One of the advantages of Cloud Run
is that you can run any Python version you
want as long as there is a base Docker image available for it.
I can even use the newest version of Python, version 3.8,
if I want to.
Now that we have our Docker file,
we can build our container with Cloud Build.
This bundles up our code along with everything
we've added in our Docker file and pushes it
to the Container Registry, a place
to store container images.
And finally, we deploy the service to Cloud Run.
It only takes two commands to get the service out
to the world.
In our terminal, we first build the container
using the builds command.
And then we deploy the service using the container
image we just built.
If we check out the Cloud Run section of Google Cloud
console, we now see our Cloud Run service.
If we click the service, we can see
important info, like metrics and the URL of our service.
We can also redirect or split user traffic
to previous revisions if we discover our latest
revision has a breaking change.
MARTIN OMANDER: Wow.
This sounds great.
Let me test it out.
Oh, wait.
I think I'm doing something wrong.
I'm being taken to an error page.
How can I find out what's going on?
ABBY CAREY: That's where Cloud Monitoring comes to the rescue.
Everything that happens within this service
is logged in Cloud Monitoring, including errors.
You can see the logs of our service
from the Cloud Monitoring-powered Logs tab.
I think this is showing a little too much info though.
If I had to the Metrics tab, I can
see the latest errors filtering out all the other noise.
Let's take a look.
MARTIN OMANDER: Hmm.
Looks like an error value was triggered on line 20.
ABBY CAREY: Ah!
I didn't account for submitting an empty string.
Let's fix that, build, and deploy one more time.
Ah.
That should do the trick.
MARTIN OMANDER: Awesome.
Looks like it's now working.
Let me see how much this wine is going to cost me.
Oh, I think I need to take some money out
of my savings account.
ABBY CAREY: Oh, no.
I'm sure it'll be worth it, though.
MARTIN OMANDER: But, anyways, thanks.
I can't wait to share this service with my friends.
I think it'll come in handy.
ABBY CAREY: If you want to check out the code for this video,
look in the description below.
Until next time.
[THEME MUSIC]