PRIYANKA VERGADIA: Welcome to "Get
Cooking in Cloud," where we share the best recipes
to apply in your cloud kitchen.
I am Priyanka Vergadia, and in this episode,
we will talk about creating serverless websites
on Google Cloud.
[MUSIC PLAYING]
Imagine you're a small company that
got really big, really quickly.
And now, you're seeing a lot of traffic on your simple website.
But you don't want to deal with the infrastructure
setup and maintenance when you could be focusing on your core
business.
So let's walk through how not to worry about the infrastructure
setup for your website by deploying it
on managed serverless platform like Cloud Run.
If you would like to deploy on App Engine instead,
there is a link in the description just for you.
Cloud Run is a managed compute platform that automatically
scales stateless containers.
Stateless is important here, since container instances
can be started or stopped at any time.
These containers can be invoked by web requests or Pub/Sub
events.
Cloud Run is also serverless.
It [INAUDIBLE] all the infrastructure management
so you can focus on what matters most--
building great applications.
So let's see how to build a website with Cloud Run.
First, our setup logistics.
Identify or create a GCP project that you would like to use.
Make sure that the billing is enabled.
Enable the Cloud Run and Cloud Build APIs.
If you don't have the Google Cloud SDK installed,
today's your day.
Go ahead and get that.
OK.
Back to our example.
At the time of this recording, Cloud Run is in beta,
so we are going to install the beta components of the Google
Cloud SDK using gcloud components install
beta command, and then we will update the components.
This may take a few minutes.
To deploy, you can use any web app of your choice.
For the purpose of this demo, we will
create a hello world web application in Python
and then deploy it using Cloud Run.
Create a directory using mkdir helloworld, and cd into it.
Then create a file named app.py and paste this code into it.
This code creates a basic web server
that listens on the ports defined by port environment
variable.
Now we are ready to containerize our app and upload
to container registry.
For that, we need to create a new file called
Dockerfile in the same directory as the source files.
Here, we define the official Python image.
Copy the local code to the container image.
Install production dependencies and a command
to run the web service on container startup.
Now we are ready to build using Cloud Build by running
the build command from the directory containing
the Dockerfile.
Gcloud build submit with tag gcr.io slash project ID
slash service name.
I'm just calling it helloworld.
You will see success when the build is successful.
To deploy the container image, we now
need to run gcloud beta run deploy image with the image
name, gcr.io slash project ID slash
service name, platform managed.
When prompted to select the region,
choose 1 for US central1.
Provide a service name of your choice
and respond with the yes to allow
unauthenticated invocations.
On success, we see the service URL.
We can visit our website by opening
that in our web browser.
Congratulations.
We have just deployed a web application
packed in container image on Cloud Run.
Cloud Run automatically and horizontally scales
your container image to handle the received request, then
scales back down when the demand decreases.
You only pay for the CPU, memory, and networking consumed
during request handling.
So today, we learned a second recipe
for deploying a web application on Google Cloud.
We created and deployed a dynamic web application
on Cloud Run and learned that if you host a website on Cloud
Run, you can rely on Google Cloud for it
to scale with the demand.
That's all for today on "Get Cooking in Cloud."
Here's hoping you can whip up something great.
Join us next time, because we will share the recipe to host
websites on compute engine.
If you liked this video, then check out
the previous episodes, too.
And to see more such content, don't
forget to like and subscribe to our channel.