A Developer's Guide to Building Serverless Applications on the Cloud - Part 1 (Deployment)
In this series, I'll discuss few topics crucial to building serverless applications while using Google Cloud, they would span across deployment, CI/CD, tooling, backend-as-a-service and more.
In this article, I will discuss how Developers at DevShopZ easily moved from local development to the Cloud. This covers simple functions, web-based application, and containerized applications including code samples.
If you are new to Serverless, check out this post
DevShopZ recently started a new project for a client, her engineers had made lots of progress in building the project, however, the project manager wants it deployed so the client can have an idea of what he's getting and also provide feedback to the team.
Developers at DevShopZ started the project by building simple API endpoints, then moved on to integrating the frontend and other tools such as Docker. We shall see how the deployment was done at each stage of the project.
Before you begin:
- The developers made use of Google Cloud account for free, details here
- They created a new GCP project, you can also do the same here
- The developers used the Google Cloud Shell terminal to execute the deployment commands.
Deploying Functions
The Engineers built the first simple endpoint which is a Python function that returns a JSON message. Tip: You could use any language of choice
The endpoint function was deployed to Cloud Functions
Google Cloud Functions is a lightweight compute solution for developers to create single-purpose, stand-alone functions that respond to cloud events without the need to manage a server or runtime environment.
gcloud functions deploy hello_world --runtime python37 --trigger-http
Execute the command above on Cloud Shell after cloning the sample codes from the Gist or setting up your own codes.
Deployed Function
Deploying Web Applications (without containers)
The project has grown into a full Web Application built using the Flask Web framework and now includes static files. Tip: You could use any framework of choice
The Web Application was deployed to App Engine
App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. You can choose from several popular languages, libraries, and frameworks to develop your apps, then let App Engine take care of provisioning servers and scaling your app instances based on demand.
gcloud app deploy
Execute the command above on Cloud Shell after cloning the sample codes from the Gist or setting up your own codes
Deployed Web Application
Deploying containers
The team decided to adopt containerization technologies by Dockerizing the existing Web Application. The source codes were updated to include a Dockerfile that was used to build the container image.
There are multiple options for deploying container images on Google Cloud, see this short video
Cloud Run is a managed compute platform that automatically deploys and scales your stateless containers. Cloud Run is serverless; it abstracts away all infrastructure management, so you can focus on what matters most โ building great applications.
gcloud builds submit --config cloudbuild.yaml
The cloudbuild.yaml file is a configuration file for Google Cloud Build which contains two steps: one to build the container image & upload to Google Container Registry; step two then deploys the image to Cloud Run.
Execute the command above on Cloud Shell after cloning the sample codes from the Gist or setting up your own codes
Deployed Containerized Web App
Useful Links
- Google Cloud - Choosing a Serverless Option
- Cloud Function - Functions Framework
- App Engine - Choosing an App Engine environment
- Ahmetb - Cloud Run FAQ
- Unofficial - Google Cloud Compute Options Guide
Stay tuned for the next article where I will discuss a more exciting serverless topic.