Integrate Google Cloud Functions with Astra DB Serverless
Google Cloud Functions is a functions-as-a-service (FaaS) offering that provides a serverless execution environment for your code.
You can use Cloud Functions with the Python driver or Java driver to connect to Astra DB for additional data processing capabilities, such as aggregating, summarizing, and validating data.
Prerequisites
-
You have an active Astra account.
-
You have created a Serverless (Vector) database.
-
You have created an application token with the Database Administrator role.
-
You have downloaded the Secure Connect Bundle (SCB) for your database and noted the path to the downloaded
secure-connect-DATABASE_NAME.zip
file. -
An active Google Cloud account.
-
A Google Cloud project with the following APIs enabled:
-
Cloud Functions API
-
Cloud Build API
-
Artifact Registry API
-
Cloud Run Admin API
-
Cloud Logging API
-
Cloud Pub/Sub API
-
Secret Manager API
-
Create a Secure Connect Bundle secret
Add the SCB as a secret in the Google Cloud Secret Manager.
-
In Google Cloud Secret Manager, select your Google Cloud project, and then click Create secret.
-
Enter a Name for the secret.
-
For the Secret value, select your database’s SCB zip file.
-
Click Create secret.
Create a Python or Java driver function
Write a Python or Java HTTP function that runs the Apache Cassandra® driver to connect to Astra DB. This guide uses the Google Cloud console. You can also use the Google Cloud CLI. For more information, see the Google Cloud Functions documentation.
-
Python Driver Function
-
Java Driver Function
-
In to the Google Cloud console, go to the Functions Overview page, and then click Create function.
-
Enter a Function name.
-
Select a Region.
-
For Environment type, select 2nd gen.
-
For Trigger type, select HTTPS, and then select Allow unauthenticated invocations.
Google recommends unauthenticated invocations for testing only. For more information, see the Google Cloud Functions documentation.
-
In the Runtime section, create the following Runtime environment variables:
-
ASTRA_DB_CLIENT_ID
: The literal, all-lowercase stringtoken
-
ASTRA_DB_CLIENT_SECRET
: Your application token
-
-
On the Security tab, click Reference a secret.
-
Select your SCB secret. If needed, grant the service account access to the secret.
-
For the Reference method, select Mounted as volume.
-
In the Mount path, enter
/secrets
.In your function code, you will use the directory in the Path field to reference the SCB.
-
Click Done, and then click Next.
-
For the Runtime, select Python 3.10 or your preferred Python version.
-
For the Source code, select Inline Editor.
-
For the Entry point, enter your function’s name from your function code. In this guide, the function name is
query_astra_db
. -
Edit the
requirements.txt
file to add the cassandra-driver:functions-framework==3.* cassandra-driver==3.25.0
-
Edit the
main.py
file to replace the file’s contents with the following code:import os from cassandra.cluster import Cluster from cassandra.auth import PlainTextAuthProvider ASTRA_DB_CLIENT_ID = os.environ.get('ASTRA_DB_CLIENT_ID') ASTRA_DB_CLIENT_SECRET = os.environ.get('ASTRA_DB_CLIENT_SECRET') cloud_config= { 'secure_connect_bundle': '/secrets/secure-connect-secret', 'use_default_tempdir': True } auth_provider = PlainTextAuthProvider(ASTRA_DB_CLIENT_ID, ASTRA_DB_CLIENT_SECRET) cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider, protocol_version=4) def query_astra_db(request): session = cluster.connect() row = session.execute("SELECT cql_version FROM system.local WHERE key = 'local';").one() print(row[0]) print ('Success')
-
In to the Google Cloud console, go to the Functions Overview page, and then click Create function.
-
Enter a Function name.
-
Select a Region.
-
For Environment type, select 1st gen.
-
Under the Trigger section, select HTTP, Allow unauthenticated invocations, and Require HTTPS.
Google recommends unauthenticated invocations for testing only. For more information, see the Google Cloud Functions documentation.
-
Under the Runtime section, create these Runtime environment variables:
-
ASTRA_DB_CLIENT_ID
: A Client ID is generated together with an application token. -
ASTRA_DB_CLIENT_SECRET
: A Client secret is generated together with an application token.
-
-
In the Security tab, click Reference a secret.
-
Select the secret you created with the SCB file. Grant the service account access to the secret, if needed.
-
In the Reference method field, select Mounted as volume , and enter
/secrets
in the Mount path field. The directory in the Path field should be used to access the SCB in the function code. -
Click Done, and then click Next.
-
Select Java 11 or your preferred version in the Runtime field.
-
Select Inline Editor in the Source code field.
-
Enter
com.example.AstraDBFunction
in the Entry point field. -
Add java-driver, a Java client library for Apache Cassandra®, Astra DB, and DataStax Enterprise (DSE), to the
pom.xml
file:pom.xml<dependency> <groupId>com.datastax.oss</groupId> <artifactId>java-driver-core</artifactId> <version>4.17.0</version> </dependency>
-
Rename the
Example.java
file toAstraDBFunction.java
and replace its content with the following code.package com.example; import com.google.cloud.functions.HttpFunction; import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpResponse; import java.io.BufferedWriter; import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; import java.nio.file.Paths; public class AstraDBFunction implements HttpFunction { public static final String ASTRA_DB_CLIENT_ID = System.getenv("ASTRA_DB_CLIENT_ID"); public static final String ASTRA_DB_CLIENT_SECRET = System.getenv("ASTRA_DB_CLIENT_SECRET"); public static CqlSession session = CqlSession.builder() .withCloudSecureConnectBundle(Paths.get("/secrets/secure-connect-secret")) .withAuthCredentials(ASTRA_DB_CLIENT_ID,ASTRA_DB_CLIENT_SECRET) .build(); public void service(HttpRequest request, HttpResponse response) throws Exception { BufferedWriter writer = response.getWriter(); ResultSet rs = session.execute("SELECT cql_version FROM system.local WHERE key = 'local';"); Row row = rs.one(); writer.write( row.getString("cql_version") ); writer.newLine(); writer.write("Success"); } }
Deploy and test the function
Deploy and test your Python or Java function in the Google Cloud console.
-
Click Deploy.
-
On the Cloud Functions overview page, find your deployed driver function, click more_vert More, and then select Test function.
-
Click Test the function, and then observe the output. A successful response returns the CQL version, such as
3.4.5
, and a200 OK
status code. -
To explore the log history, click Logs or View all logs to open the Logs Explorer.