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

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project .

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project .

  6. Enable the Cloud APIs necessary to run an AlloyDB for PostgreSQL sample application on Cloud Run.

    Enable the APIs

    1. In the Confirm project step, to confirm the name of the project that you are going to make changes to, click Next .

    2. 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

  1. In the Google Cloud console, go to the Clusterspage.

    Go to Clusters

  2. Click Create cluster.

    The Google Cloud console presents you with a choice of cluster types. Select Highly available.

  3. Click Continue.

    The Configure your clustersection expands.

  4. Under Basic info, in the Cluster IDfield, enter my-cluster .

  5. In the Passwordfield, enter any password you'd like. Take note of this password. You use it again later in this quickstart.

  6. In the Regionfield, select us-central1 (Iowa) .

  7. 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:

    1. Click Set up connection.

      A new pane labeled Enable Service Networking APIappears.

    2. In the pane, select Use an automatically allocated IP range.

    3. Click Continue.

    4. Click Create connection, and wait for the connection configuration to finish.

      The pane is removed.

  8. Click Continue.

    The Configure your primary instancesection expands.

  9. Under Basic info, in the Instance IDfield, enter my-primary .

  10. For a Machinevalue, select 2 vCPU, 16 GB .

  11. Click Create cluster.

    Wait for the cluster to get created.

Connect to your instance and create a database

  1. If you are not in your newly created cluster Overviewpage, then in the Google Cloud console, go to the Clusterspage.

    Go to Clusters

  2. To display the cluster Overviewpage, click the my-cluster cluster name.

  3. In the navigation menu, click AlloyDB Studio.

  4. In the Databaselist on the Sign in to AlloyDB Studiopage, select postgres .

  5. In the Userdrop-down list, select postgres .

  6. In the Passwordfield, enter the password you created in Create a cluster and its primary instance .

  7. Click Authenticate. The Explorerpane displays a list of the objects in your database.

  8. In the Editor 1tab, create a database:

      CREATE 
      
     DATABASE 
      
     quickstart_db 
     ; 
     
    
  9. Click Run. Wait for the Statement executed successfully message to display in the Resultspane.

Find your project ID

  1. Go to the Dashboardpage in the Google Cloud console.

    Go to Dashboard

  2. Find the project ID on the Project infocard.

  3. 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

  1. In the Google Cloud console, open Cloud Shell.

    Open Cloud Shell

  2. 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 
    
  3. 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.

  4. In the Authorize Cloud Shelldialog, click Authorize. This prompt does not appear if you have done this step previously.

  5. 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

  1. In the Google Cloud console, go to the Clusterspage.

    Go to Clusters

  2. Find the row in the table whose Resource nameis my-primary .

  3. 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

  1. In the Google Cloud console, go to the Cloud Runpage.

    Go to Cloud Run

  2. In the Servicestab, click Deploy Container.

  3. In the Create servicepage, select the Deploy one revision from an existing container imageoption.

  4. In the Container image URLfield, click Select.

  5. In the Select container imagepane, complete the following steps:

    1. Select the Artifact registrytab.
    2. Expand us-central1-docker.pkg.dev/ PROJECT_ID /cloud-run-source-deploy .
    3. Expand run-sql .
    4. Select the latest image.
    5. Click Select.
  6. In the Service namefield, enter quickstart-service .

  7. Make sure us-central1 (Iowa) is selected in the Regionlist by default.

  8. In the Authenticationsection, select Allow unauthenticated invocations.

  9. Ensure that CPU is only allocated during request processingis selected in the CPU allocation and pricingsection.

  10. Expand the Container(s), Volumes, Networking, Securitysection.

  11. In the Container(s)tab, select the Variables & secretstab and complete the following steps:

    1. In the Environment variablessection, click Add variable.

    2. Enter the following in the Name and Value fields:

  12. Switch from the Container(s)tab to the Networkingtab and complete the following steps:

    1. Select the Connect to a VPC for outbound trafficcheckbox.

    2. Select the Send traffic directly to a VPCoption.

    3. In the Networkdrop-down list, choose the default value.

    4. In the Subnetdrop-down list, ensure that the default value is selected.

  13. 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

  1. In the Google Cloud console, go to the Cloud Runpage.

    Go to Cloud Run

  2. On the quickstart-servicepage, click Copy to clipboardto copy the application URL.

  3. 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:

    Tabs and spaces sample application

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

  1. In the Google Cloud console, go to the Clusterspage.

    Go to Clusters

  2. Next to the my-cluster cluster that you want to delete, click Actions, and then click Delete.

  3. In the Delete clusterdialog that appears, type my-cluster in the my-clusterfield, confirming that you want to delete it.

  4. Click Delete.

Clean up the service

  1. In the Google Cloud console, go to the Cloud Runpage.

    Go to Cloud Run

  2. Select the checkbox next to the quickstart-service service name.

  3. Click Deleteat the top of the Cloud Runpage.

  4. In the Delete quickstart-servicedialog that appears, click Deleteto confirm.

What's next

Design a Mobile Site
View Site in Mobile | Classic
Share by: