Connect from Cloud Run
This page guides you through the process of deploying a sample application on a Cloud Run service by using the Google Cloud console and a client application. This sample application connects Cloud Run to AlloyDB for PostgreSQL.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project
: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles .
-
Verify that billing is enabled for your Google Cloud project .
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project
: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles .
-
Verify that billing is enabled for your Google Cloud project .
-
Enable the Cloud APIs necessary to run an AlloyDB for PostgreSQL sample application on Cloud Run.
-
In the Confirm project step, to confirm the name of the project that you are going to make changes to, click Next .
-
In the Enable APIsstep, click Enable to enable the following:
- AlloyDB API
- Cloud Run Admin API
- Compute Engine API
- Cloud Build API
- Container Registry API
- Service Networking API
- Cloud Resource Manager API
-
Create a cluster and its primary instance
-
In the Google Cloud console, go to the Clusterspage.
-
Click Create cluster.
The Google Cloud console presents you with a choice of cluster types. Select Highly available.
-
Click Continue.
The Configure your clustersection expands.
-
Under Basic info, in the Cluster IDfield, enter
my-cluster. -
In the Passwordfield, enter any password you'd like. Take note of this password. You use it again later in this quickstart.
-
In the Regionfield, select
us-central1 (Iowa). -
In Connectivity, in the Networkdrop-down, select
default.If you have not set up private services access yet, a dialog labeled Private services access connection requiredappears.
In the Private services access connection requireddialog, to create private services access connection, complete the following:
-
Click Set up connection.
A new pane labeled Enable Service Networking APIappears.
-
In the pane, select Use an automatically allocated IP range.
-
Click Continue.
-
Click Create connection, and wait for the connection configuration to finish.
The pane is removed.
-
-
Click Continue.
The Configure your primary instancesection expands.
-
Under Basic info, in the Instance IDfield, enter
my-primary. -
For a Machinevalue, select
2 vCPU, 16 GB. -
Click Create cluster.
Wait for the cluster to get created.
Connect to your instance and create a database
-
If you are not in your newly created cluster Overviewpage, then in the Google Cloud console, go to the Clusterspage.
-
To display the cluster Overviewpage, click the
my-clustercluster name. -
In the navigation menu, click AlloyDB Studio.
-
In the Databaselist on the Sign in to AlloyDB Studiopage, select
postgres. -
In the Userdrop-down list, select
postgres. -
In the Passwordfield, enter the password you created in Create a cluster and its primary instance .
-
Click Authenticate. The Explorerpane displays a list of the objects in your database.
-
In the Editor 1tab, create a database:
CREATE DATABASE quickstart_db ; -
Click Run. Wait for the
Statement executed successfullymessage to display in the Resultspane.
Find your project ID
-
Go to the Dashboardpage in the Google Cloud console.
-
Find the project ID on the Project infocard.
-
Take note of your project ID. You need it for the following step of this quickstart.
Populate Artifact Registry with an image of the sample application
-
In the Google Cloud console, open Cloud Shell.
-
In Cloud Shell Editor, use the following command to clone the repository with the sample application code from GitHub:
cloudshell_open --repo_url \ "https://github.com/GoogleCloudPlatform/alloydb-auth-proxy" \ --dir \ "examples/go" \ --force_new_clone -
Create a repository in Artifact Registry for docker images:
gcloud artifacts repositories create cloud-run-source-deploy --location us-central1 --repository-format = docker --project PROJECT_IDReplace
PROJECT_IDwith the ID of your project. -
In the Authorize Cloud Shelldialog, click Authorize. This prompt does not appear if you have done this step previously.
-
To build a Docker container and publish it to Artifact Registry, use the following command:
gcloud builds submit --tag us-central1-docker.pkg.dev/ PROJECT_ID /cloud-run-source-deploy/run-sql --project PROJECT_IDReplace
PROJECT_IDwith the ID of your project.
Understand the sample code
The sample application code for this quickstart is located in the examples/go
directory of the GoogleCloudPlatform/alloydb-auth-proxy
GitHub repository. The cloudshell_open
command in the previous step automatically clones this repository and opens the correct directory for you.
How the code works
The application is written in the Go programming language and serves as a tool for collecting votes. The main logic is contained in the main.go
file, which starts a web server and handles user requests.
The database connection is configured using environment variables, which you set up when you create the Cloud Run service. These variables tell the application how to connect to your AlloyDB instance, including the database name, user, password, and the instance's IP address. The code uses these variables to establish a secure connection to your AlloyDB database.
Why is the code in this repository?
The alloydb-auth-proxy
repository contains the AlloyDB Auth Proxy, a tool that helps you securely connect to your AlloyDB instances. The repository also includes related libraries and examples, such as the Go application used in this quickstart.
You don't need the entire alloydb-auth-proxy
project to connect to AlloyDB from Cloud Run. The cloudshell_open
command clones the entire repository for your convenience, but the only files used to build the container for this quickstart are located in the examples/go
directory.
Find the IP address for your new instance
-
In the Google Cloud console, go to the Clusterspage.
-
Find the row in the table whose Resource nameis
my-primary. -
Take note of that row's Private IP address. You need it for the following step of this quickstart.
The example format of the IP address is
172.19.209.2:5432.
Create a service for Cloud Run
-
In the Google Cloud console, go to the Cloud Runpage.
-
In the Servicestab, click Deploy Container.
-
In the Create servicepage, select the Deploy one revision from an existing container imageoption.
-
In the Container image URLfield, click Select.
-
In the Select container imagepane, complete the following steps:
- Select the Artifact registrytab.
- Expand
us-central1-docker.pkg.dev/ PROJECT_ID /cloud-run-source-deploy. - Expand
run-sql. - Select the latest image.
- Click Select.
-
In the Service namefield, enter
quickstart-service. -
Make sure
us-central1 (Iowa)is selected in the Regionlist by default. -
In the Authenticationsection, select Allow public access.
-
Ensure that CPU is only allocated during request processingis selected in the CPU allocation and pricingsection.
-
Expand the Container(s), Volumes, Networking, Securitysection.
-
In the Container(s)tab, select the Variables & secretstab and complete the following steps:
-
In the Environment variablessection, click Add variable.
-
Enter the following in the
NameandValuefields:-
DB_NAME:quickstart_db -
DB_USER:postgres -
DB_PASS: The password that you entered when you created the cluster in Create a cluster and its primary instance . -
DB_PORT:5432 -
INSTANCE_HOST: The IP address of your cluster's primary instance, which you found in Find the IP address for your new instance . Don't include the port number—for example, set to172.19.209.2, and not172.19.209.2:5432.
-
-
-
Switch from the Container(s)tab to the Networkingtab and complete the following steps:
-
Select the Connect to a VPC for outbound trafficcheckbox.
-
Select the Send traffic directly to a VPCoption.
-
In the Networkdrop-down list, choose the
defaultvalue. -
In the Subnetdrop-down list, ensure that the
defaultvalue is selected.
-
-
Click Createto create the Cloud Run service.
After the Cloud Run service is deployed, the quickstart-servicepage displays the URL of the sample application in the URLfield.
The application uses AlloyDB as its datastore.
View the sample application
-
In the Google Cloud console, go to the Cloud Runpage.
-
On the quickstart-servicepage, click content_copy Copy to clipboardto copy the application URL.
-
In a new browser tab, paste the copied application URL and click Enter.
The sample vote-collecting web application that appears looks similar to the following:

Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
Clean up the cluster
-
In the Google Cloud console, go to the Clusterspage.
-
Next to the
my-clustercluster that you want to delete, click more_vert Actions, and then click delete Delete. -
In the Delete clusterdialog that appears, type
my-clusterin the my-clusterfield, confirming that you want to delete it. -
Click Delete.
Clean up the service
-
In the Google Cloud console, go to the Cloud Runpage.
-
Select the checkbox next to the
quickstart-serviceservice name. -
Click delete Deleteat the top of the Cloud Runpage.
-
In the Delete quickstart-servicedialog that appears, click Deleteto confirm.

