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.
-
Make sure 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.
-
Make sure 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 APIs step, 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 Clusters page.
Click Create cluster.
The Google Cloud console presents you with a choice of cluster types. Select Highly available.
Click Continue.
The Configure your cluster section expands.
Under Basic info, in the Cluster ID field, enter
my-cluster
.In the Password field, enter any password you'd like. Take note of this password. You use it again later in this quickstart.
In the Region field, select
us-central1 (Iowa)
.Under Networking, select
default
.If you have not set up private services access yet, a dialog labeled Private service access connection required appears.
In the Private service access connection required dialog, to create private services access connection, complete the following:
Click Set up connection.
A new pane labeled Enable Service Networking API appears.
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 instance section expands.
Under Basic info, in the Instance ID field, enter
my-primary
.For a Machine value, 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 Overview page, then in the Google Cloud console, go to the Clusters page.
To display the cluster Overview page, click the
my-cluster
cluster name.In the navigation menu, click AlloyDB Studio.
In the Database list on the Sign in to AlloyDB Studio page, select
postgres
.In the User drop-down list, select
postgres
.In the Password field, enter the password you created in Create a cluster and its primary instance.
Click Authenticate. The Explorer pane displays a list of the objects in your database.
In the Editor 1 tab, create a database:
CREATE DATABASE quickstart_db;
Click Run. Wait for the
Statement executed successfully
message to display in the Results pane.
Find your project ID
Go to the Dashboard page in the Google Cloud console.
Find the project ID on the Project info card.
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_ID
Replace
PROJECT_ID
with the ID of your project.In the Authorize Cloud Shell dialog, 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_ID
Replace
PROJECT_ID
with the ID of your project.
Find the IP address for your new instance
In the Google Cloud console, go to the Clusters page.
Find the row in the table whose Resource name is
my-primary
.Take note of that row's Private IP address. You need it for the following step of this quickstart.
Create a service for Cloud Run
In the Google Cloud console, go to the Cloud Run page.
In the Services tab, click Create Service.
In the Container image URL field, click Select.
In the Select container image pane, complete the following steps:
- Select the Artifact registry tab.
- Expand
us-central1-docker.pkg.dev/PROJECT_ID/cloud-run-source-deploy
. - Expand
run-sql
. - Select the latest image.
- Click Select.
In the Service name field, enter
quickstart-service
.Make sure
us-central1 (Iowa)
is selected in the Region list by default.In the Authentication section, select Allow unauthenticated invocations.
Ensure that CPU is only allocated during request processing is selected in the CPU allocation and pricing section.
Expand the Container(s), Volumes, Networking, Security section.
In the Container(s) tab, select the Variables & secrets tab and complete the following steps:
In the Environment variables section, click Add variable.
Enter the following in the
Name
andValue
fields: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.
Switch from the Container(s) tab to the Networking tab and complete the following steps:
Select the Connect to a VPC for outbound traffic checkbox.
Select the Send traffic directly to a VPC option.
In the Network drop-down list, choose the
default
value.In the Subnet drop-down list, ensure that the
default
value is selected.
Click Create to create the Cloud Run service.
After the Cloud Run service is deployed, the quickstart-service page displays the URL of the sample application in the URL field.
The application uses AlloyDB as its datastore.
View the sample application
In the Google Cloud console, go to the Cloud Run page.
On the quickstart-service page, click content_copy Copy to clipboard to 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 Clusters page.
Next to the
my-cluster
cluster that you want to delete, click more_vert Actions, and then click delete Delete.In the Delete cluster dialog that appears, type
my-cluster
in the my-cluster field, confirming that you want to delete it.Click Delete.
Clean up the service
In the Google Cloud console, go to the Cloud Run page.
Select the checkbox next to the
quickstart-service
service name.Click delete Delete at the top of the Cloud Run page.
In the Delete quickstart-service dialog that appears, click Delete to confirm.