Application Deployment on GCP
Google Cloud Platform
Google Cloud Platform (opens in a new tab) (GCP) is a suite of cloud computing services that runs on the same infrastructure that Google uses internally for its end-user products, such as Google Search, YouTube, and Google Drive.
Deploying the generated application with a PostgreSQL database on Google Cloud Platform (GCP) involves multiple steps, including setting up a PostgreSQL database on Cloud SQL and deploying the Next.js app on a service like Google App Engine or Google Cloud Run.
There are so many ways to deploy applications on Google Cloud Platform. In this tutorial, we will use Google App Engine and Google Cloud SQL. Below is a guide on how to do this.
The Billing features must be enabled to use the Google Cloud Platform. You need a valid payment method even for free trial period (opens in a new tab). Go here (opens in a new tab) for further explanation.
gcloud
The Google Cloud CLI (opens in a new tab) or gcloud
is a set of tools to create and manage Google Cloud resources. You can use these tools to perform many common platform tasks from the command line or through scripts and other automation. We will use gcloud
on Windows OS. For another operating system, please download Google Cloud CLI from this link (opens in a new tab).
The installation in Windows is straightforward, and at the end of the installation, it will help us to use an existing or create a new Google Cloud project.
Connect the Project
To connect the generated application with a Google Cloud project. For this scenario, we can use the gcloud init
command on the root application:
PS E:\_workspaces\ROQ\anime-scaled-fig-2d3f-v3> gcloud init
Welcome! This command will take you through the configuration of gcloud.
Settings from your current configuration [default] are:
accessibility:
screen_reader: 'False'
core:
account: anna.w@gmail.com
disable_usage_reporting: 'True'
project: roq-project
Pick configuration to use:
[1] Re-initialize this configuration [default] with new settings
[2] Create a new configuration
Please enter your numeric choice: 1
Your current configuration has been set to: [default]
You can skip diagnostics next time by using the following flag:
gcloud init --skip-diagnostics
Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).
Choose the account you would like to use to perform operations for this configuration:
[1] anna.w@gmail.com
[2] Log in with a new account
Please enter your numeric choice: 1
You are logged in as: [anna.w@gmail.com].
This account has no projects.
Would you like to create one? (Y/n)? y
Enter a Project ID. Note that a Project ID CANNOT be changed later.
Project IDs must be 6-30 characters (lowercase ASCII, digits, or
hyphens) in length and start with a lowercase letter. anime-figure-store
Waiting for [operations/cp.6165428578280918017] to finish...done.
Your current project has been set to: [anime-figure-store].
If we don't have a Google Cloud project or we don't want to use the existing one, the gcloud
will offer us to create a new Google project.
Google Cloud SQL
Google Cloud SQL (opens in a new tab) is a Google service that offers a fully managed database service for MySQL, PostgreSQL, and SQL Server. We will use PostgreSQL because it's the default ROQ's generated application database.
PostgreSQL
Follow these steps to set up a PostgreSQL database in Google Cloud SQL.
Create Instance
Sign in to Google Cloud Console (opens in a new tab) and navigate to SQL menu in the left sidebar or directly go here (opens in a new tab).
To create PostgreSQL, click Create instance, then select the PostgreSQL database engine.
In order to create an instance, you have to enable the Compute Engine API first. Google Cloud SQL Console will provide the Enable API button.
Settings & Configuration
The next step is to configure the database instance settings such as username, password, PostgreSQL version, region, etc.
There are some mandatory settings and configurations that need to be set. For example, our generated application is about Anime Figure Store. We can set the fields:
- Instance ID:
animefigurestore
- Password: We can fill in anything or generate the password.
- Database Version: The default version is the latest version of PostgreSQL. We will use version 15.
The Cloud SQL edition preset, region, and availability are configurable. Choose as per your needs. Be aware that these settings and configurations will affect the database pricing estimation.
Create Database
After creating the database instance, we can create a new one for the generated application. Go to the SQL Databases menu, then add a new database by clicking the Create Database button.
We will use animefiguredb
as the generated application database name.
Create Users
Google Cloud SQL provides the default admin user postgres
without any password. You need to set a password for this user before you can log in. You can do this in the Google Cloud console or using the gcloud
command. The database instance for this example is animefigurestore
.
gcloud sql users set-password postgres \
--instance=animefigurestore \
--password=SECRETP4ST$
Another option is to create new users. We can do this using the Users menu and then clicking ADD USER ACCOUNT.
We will use owner
as the database username.
DATABASE_URL
After creating the database, we can get the connection credentials, such as the IP address, username, password, and database name, from the Connections section.
We can configure the Cloud SQL instance to have a public IPv4 address and to accept connections from specific IP addresses or a range of addresses by adding authorized addresses to your instance.
For the deployment purpose, we will allow database access from the public internet. Set the allowed IP address to 0.0.0.0/0
In order to configure IP on PostgreSQL on Google Cloud SQL, please visit the following link (opens in a new tab).
From all the settings, we can form the PostgreSQL URI to connect our generated application.
DATABASE_URL=postgresql://owner:password@34.28.181.197:5432/animefiguredb
Google App Engine
Google App Engine (opens in a new tab) (GAE) allows developers to build, deploy, and run applications on Google's highly scalable and reliable infrastructure without worrying about the underlying hardware and software layers.
To create an App Engine, click the Create Application button, and then we can leave other settings to its default.
When we first create an App Engine application, the platform automatically uses default settings for many configuration aspects like runtime, scaling, and so on. This allows us to get started quickly without having to specify a lot of details. However, we can customize the settings with configure app.yaml
in the generated application.
Prepare the Project
app.yaml
Before uploading the application to Google App Engine, it is important to configure the project. Create app.yaml
file in the generated application root folder and fill it with these contents:
runtime: nodejs18 # specify the Node.js runtime
env: standard
manual_scaling:
instances: 1
entrypoint: npm run start
This file specifies that the project uses runtime Node.js version 18 and the standard environment for Google App Engine. It also specifies the entry point or script to run after file uploading.
ROQ Environment
ROQ's generated application needs environment variables from the ROQ Console. We can create a new environment for the Google App Engine.
Select the desired application from the ROQ Console and add a new environment, or navigate to the ROQ Console Environment (opens in a new tab) for the active application.
We can also copy the environment variables from another environment. For example, to copy environment variables from the Preview environment. Go to the Environment Sync menu and then click the Copy Configuration button.
Google App Engine URL
Google App Engine has the following format URL for the default service:
https://PROJECT_ID.REGION_ID.r.appspot.com:
- PROJECT_ID: is the ID of your Google Cloud Project.
- REGION_ID: is the region where you deploy your App Engine application.
This URL is required as the base URL for the ROQ Platform configuration. Therefore, configure the environment variables in the generated application.
There are no exact ways to know this URL. The easiest way (and tricky) to know this URL at initially created GAE is by connecting the project first and then running the gcloud app deploy
command:
gcloud app deploy
This command will return the information about the GAE application:
Services to deploy:
descriptor: [E:\_workspaces\ROQ\anime-scaled-fig-2d3f-v3\app.yaml]
source: [E:\_workspaces\ROQ\anime-scaled-fig-2d3f-v3]
target project: [anime-figure-store]
target service: [default]
target version: [20230907t115249]
target url: [https://anime-figure-store.uc.r.appspot.com]
target service account: [anime-figure-store@appspot.gserviceaccount.com]
Do you want to continue (Y/n)?
Do not continue because the deployment will be failed.
From the command response above, we have the Google App Engine deployment URL for our application:
https://anime-figure-store.uc.r.appspot.com
Setup the Base URL
The base URL needs to be setup in the ROQ Console. Go to the Application Details → Settings.
We need to setup the Base URL and all the Authentication URLs.
Description | URL |
---|---|
Base URL | https://anime-figure-store.uc.r.appspot.com |
Sign-in URL | https://anime-figure-store.uc.r.appspot.com/api/auth/login |
Allowed Logout URLs | https://anime-figure-store.uc.r.appspot.com/api/auth/logout |
Allowed Callback URLs | https://anime-figure-store.uc.r.appspot.com/api/auth/callback |
.env
Create an environment .env
file if it doesn't exist in the generated application root folder, then copy all the environment variables from the ROQ Console and add the DATABASE_URL
from our previous PostgreSQL database setup:
# get all the environment variables from the ROQ Console
# ...
# PostgreSQL on Google Cloud SQL
DATABASE_URL=postgresql://owner:password@34.28.181.197:5432/animefiguredb
Build & Configure
Before uploading the code to the Google App Engine, we must build the project locally:
npm run build
It is necessary to run the Prisma migration on the PostgreSQL database for the generated application to function correctly. While other platforms, such as Vercel and Heroku, perform this step automatically along with the build process, no such automation is available on Google App Engine.
To use the PostgreSQL database we set up earlier, we need to run the migration manually:
npx prisma migrate deploy
Deploying Application
Deploying to Google App Engine using gcloud
is a simple process. After building and configuring the project, uploading is done with a single command.
gcloud app deploy
We can check the Google App Engine logs to confirm the successful upload:
gcloud app logs tail -s default
To verify if the application has been successfully deployed in Google App Engine (GAE), we can utilize the gcloud
command.
gcloud app browse
The simplicity of Google App Engine is one of its benefits. Each time, we make changes to the generated application, build it, and then upload it. GAE will keep a version of it.