Integrate Google Cloud Functions with Astra DB Serverless

query_builder 30 min

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

Create a Secure Connect Bundle secret

Add the SCB as a secret in the Google Cloud Secret Manager.

  1. In Google Cloud Secret Manager, select your Google Cloud project, and then click Create secret.

  2. Enter a Name for the secret.

  3. For the Secret value, select your database’s SCB zip file.

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

  1. In to the Google Cloud console, go to the Functions Overview page, and then click Create function.

  2. Enter a Function name.

  3. Select a Region.

  4. For Environment type, select 2nd gen.

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

  6. In the Runtime section, create the following Runtime environment variables:

    • ASTRA_DB_CLIENT_ID: The literal, all-lowercase string token

    • ASTRA_DB_CLIENT_SECRET: Your application token

      gcp integration runtime variables
  7. On the Security tab, click Reference a secret.

  8. Select your SCB secret. If needed, grant the service account access to the secret.

  9. For the Reference method, select Mounted as volume.

  10. In the Mount path, enter /secrets.

    In your function code, you will use the directory in the Path field to reference the SCB.

    gcp integration reference secret
  11. Click Done, and then click Next.

  12. For the Runtime, select Python 3.10 or your preferred Python version.

  13. For the Source code, select Inline Editor.

  14. For the Entry point, enter your function’s name from your function code. In this guide, the function name is query_astra_db.

  15. Edit the requirements.txt file to add the cassandra-driver:

    functions-framework==3.*
    cassandra-driver==3.25.0
  16. 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')
  1. In to the Google Cloud console, go to the Functions Overview page, and then click Create function.

  2. Enter a Function name.

  3. Select a Region.

  4. For Environment type, select 1st gen.

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

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

  7. In the Security tab, click Reference a secret.

  8. Select the secret you created with the SCB file. Grant the service account access to the secret, if needed.

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

  10. Click Done, and then click Next.

  11. Select Java 11 or your preferred version in the Runtime field.

  12. Select Inline Editor in the Source code field.

  13. Enter com.example.AstraDBFunction in the Entry point field.

  14. 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>
    gcp integration java pom
  15. Rename the Example.java file to AstraDBFunction.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.

  1. Click Deploy.

  2. On the Cloud Functions overview page, find your deployed driver function, click More, and then select Test function.

  3. Click Test the function, and then observe the output. A successful response returns the CQL version, such as 3.4.5, and a 200 OK status code.

  4. To explore the log history, click Logs or View all logs to open the Logs Explorer.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com