Connect your application to AlloyDB data and deploy on Cloud Run

1. Overview

With this codelab, we will demonstrate a simple, easy-to-do method for setting up AlloyDB and connecting our application to it. It is a quick L100 level learning to upgrade the developer experience for integrating advanced AI applications to sophisticated data features that AlloyDB has to offer.

What you'll build

A simple web application.

As part of this, you will:

  1. Create an AlloyDB instance and cluster in one click installation
  2. Create a sample application to connect to this instance and set up data

The application will just connect to the database setup you did in step 1 and will create a table and insert one record in it.

Requirements

  • A browser, such as Chrome or Firefox
  • A Google Cloud project with billing enabled.

2. Before you begin

Create a project

  1. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
  2. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
  1. You'll use Cloud Shell, a command-line environment running in Google Cloud. Click Activate Cloud Shell at the top of the Google Cloud console.

Activate Cloud Shell button image

  1. Once connected to Cloud Shell, you check that you're already authenticated and that the project is set to your project ID using the following command:
gcloud auth list
  1. Run the following command in Cloud Shell to confirm that the gcloud command knows about your project.
gcloud config list project
  1. If your project is not set, use the following command to set it:
gcloud config set project <YOUR_PROJECT_ID>
  1. Enable the required APIs: Follow the link and enable the APIs.

Alternatively you can use the gcloud command for this. Refer documentation for gcloud commands and usage.

3. Database setup

In this lab we'll use AlloyDB as the database for the test data. It uses clusters to hold all of the resources, such as databases and logs. Each cluster has a primary instance that provides an access point to the data. Tables will hold the actual data.

Let's create an AlloyDB cluster, instance and table where the test dataset will be loaded.

  1. Click the button or Copy the link below to your browser where you have the Google Cloud Console user logged in.

  1. Once this step is complete the repo will be cloned to your local cloud shell editor and you will be able to run the command below from with the project folder (important to make sure you are in the project directory):
sh run.sh
  1. Now use the UI (clicking the link in the terminal or clicking the "preview on web" link in the terminal.
  2. Enter your details for project id, cluster and instance names to get started.
  3. Go grab a coffee while the logs scroll & you can read about how it's doing this behind the scenes here.

4. Create the sample application to test the connection

In the Cloud Shell Terminal, run the following command

git clone https://github.com/AbiramiSukumaran/verify-easy-alloydb-connection

Make changes to the verify_connection.py file for your AlloyDB setup you just configured in the last section:

# Replace this with the Private IP of your AlloyDB Instance
DB_HOST = <<>> 
DB_PORT = "5432"
DB_NAME = "postgres"
DB_USER = "postgres"
# Replace this with your actual AlloyDB password    
DB_PASS = <<>>

5. Deploy to Cloud Run

In the Cloud Shell Terminal make sure you are inside your main folder and within the project folder.

Once you are sure you are in the project folder, run the following command:

gcloud beta run deploy verify-alloydb \
    --source . \
    --region=us-central1 \
    --network=easy-alloydb-vpc \
    --subnet=easy-alloydb-subnet \
    --allow-unauthenticated \
    --vpc-egress=all-traffic

Once deployed, you should receive a deployed Cloud Run Endpoint that looks like this:

https://verify-alloydb-**********-uc.a.run.app/

6. Demo

To confirm it worked you can see the result on the browser or the logs explorer.

You can navigate to the AlloyDB on Google Cloud Console and open the newly created instance. Click "AlloyDB Studio" from the navigation pane on the left and connect with your credentials.

In the studio, on the left pane, refresh the database objects and you should see the newly created table:

c6365c17d2d49eb7.png

You can quickly query it to verify that the insert is successfully done:

480c24fbd45ba150.png

7. Cleanup

Once this trial lab is done, do not forget to delete alloyDB cluster and instance.

It should clean up the cluster along with its instance(s).

8. Conclusion

Get started with setting up your data with AlloyDB quick & easy!!!