Cloud Functions (Python SDK)
🏠 Back to home | Written by Artem Chebotko
📋 On this page
- A - Overview
- B - Prerequisites
- C - Creating a Google Cloud Function to Access Astra DB using Python SDK and Cloud Console
Cloud Functions is Google's function-as-a-service offering that provides a serverless execution environment for your code. Cloud Functions are commonly used to:
- Extend Astra DB with additional data processing capabilities, such as aggregating, summarizing and validating data periodically;
- Connect Astra DB with other cloud services into data pipelines that move, process and analyze data.
- Create an Astra Database
- Create an Astra Token
- Optionally, if you are new to Cloud Functions, practice creating a simpler function first
- Go to the Functions Overview page, select a project that has Cloud Functions enabled, and click Create function.
- Under the Basics section, specify preferred Function name and Region.
- Under the Trigger section, select HTTP, Allow unauthenticated invocations, and Require HTTPS.
-
Click Save.
-
Under the Runtime, build, connections and security settings section, customize additional settings and create these Runtime environment variables:
-
ASTRA_DB_ID
: A Database ID value can be found on the Astra DB dashboard. -
ASTRA_DB_REGION
: A Region name can be found on the overview page for a specific Astra DB database. -
ASTRA_DB_APPLICATION_TOKEN
: An Application Token can be generated for a specific Astra DB database (see the Prerequisites section above).
Note that, for better security, you can alternatively use the Secret Manager service to store and manage an application token as a secret. A secret can then be similarly exposed as an environment variable. The settings can be found under the Runtime, build, connections and security settings section, the Security tab, and the Secrets field.
-
Click Next.
-
Select Python 3.7 or your preferred version in the Runtime field.
-
Select Inline Editor in the Source code field.
-
Enter query_astra_db in the Entry point field.
-
Add AstraPy, a Pythonic SDK for DataStax Astra and Stargate, and its preferred version to the
requirements.txt
file.
- Replace the
main.py
content with:
from astrapy.rest import create_client, http_methods
import os
def query_astra_db(request):
ASTRA_DB_ID = os.environ.get('ASTRA_DB_ID')
ASTRA_DB_REGION = os.environ.get('ASTRA_DB_REGION')
ASTRA_DB_APPLICATION_TOKEN = os.environ.get('ASTRA_DB_APPLICATION_TOKEN')
astra_http_client = create_client(astra_database_id=ASTRA_DB_ID,
astra_database_region=ASTRA_DB_REGION,
astra_application_token=ASTRA_DB_APPLICATION_TOKEN)
# Retrieve a row with primary key value 'local'
# from table 'local' in keyspace 'system'
res = astra_http_client.request(
method=http_methods.GET,
path=f"/api/rest/v2/keyspaces/system/local/local"
)
# Print the 'cql_version' field value of the row
print(res["data"][0]['cql_version'])
print ('Success')
You can learn more about the code above by reading the AstraPy documentation.
-
Click Deploy.
-
On the Cloud Functions Overview page, find the newly deployed function.
- Under Actions, select Test function.
- On the testing page, click Test the function and observe the output.
Notice the CQL version output 3.4.5 and status code 200.
You can further explore the log history by either clicking on the Logs tab or the View all logs link that opens Logs Explorer.