Are you a small company that has experienced rapid growth? Are you now dealing with high traffic on your website? If you don’t want to get caught up in infrastructure setup and maintenance, then deploying your website on a managed serverless platform like Cloud Run might be the solution for you. In this article, we will walk you through the process of building a serverless website on Google Cloud.
Setting Up the Logistics
Before we dive into the technical aspects, let’s start by setting up the necessary logistics:
- Identify or create a GCP project that you want to use.
- Ensure that billing is enabled.
- Enable the Cloud Run and Cloud Build APIs.
- If you haven’t already, install the Google Cloud SDK.
With the logistics in place, let’s move on to the next steps.
Creating the Web Application
To demonstrate the deployment process, we will create a simple “Hello World” web application using Python. Follow these steps:
- Create a directory named
helloworldand navigate into it.
- Create a file named
app.pyand paste the provided code for a basic web server.
- Create a Dockerfile in the same directory. The Dockerfile defines the image and instructions for building the container.
- Build the container image using Cloud Build.
Deploying on Cloud Run
Now that we have our web application and container image ready, let’s deploy it on Cloud Run:
- Use the command
gcloud build submit --tag gcr.io/projectID/serviceNameto build the container image.
- Once the build is successful, deploy the container image using the command
gcloud beta run deploy serviceName --image gcr.io/projectID/serviceName --platform managed.
- When prompted, choose the region for deployment and allow unauthenticated invocations.
- On success, the service URL will be displayed. You can visit your website by opening the URL in your web browser.
Congratulations! You have successfully deployed a serverless web application on Cloud Run. The best part is that Cloud Run automatically scales your container image to handle incoming requests and scales down when the demand decreases. This means you only pay for the resources consumed during request handling.
In this article, we learned how to deploy a serverless website on Google Cloud using Cloud Run. By hosting your website on Cloud Run, you can rely on Google Cloud’s scaling capabilities to handle high traffic. If you’re looking for a hassle-free infrastructure setup and maintenance experience, Cloud Run is the way to go.
Stay tuned for more articles where we will share recipes for hosting websites on compute engines. Don’t forget to like and subscribe to our channel for more informative content.