Azure Functions¶
Overview¶
Azure Functions is Microsoft Azure's function-as-a-service offering that provides a serverless execution environment for your code. Azure 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.
Prerequisites¶
- You should have an Astra account
- You should Create an Astra Database
- You should Have an Astra Token
- You should Download your Secure Connect Bundle
Using Python Driver¶
✅ 1. Create a function.¶
-
Follow the Quickstart to create a Python function in Azure from the command line using the
v1
Python programming model and Azure CLI. Complete all the steps to successfully deploy and test the function in Azure. -
Use Azure CLI to add these runtime environment variables to the application settings:
ASTRA_DB_CLIENT_ID
: A Client ID is generated together with an application token (see the Prerequisites section above).ASTRA_DB_CLIENT_SECRET
: A Client secret is generated together with an application token (see the Prerequisites section above).Note thataz functionapp config appsettings set --name <APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings "ASTRA_DB_CLIENT_ID=Hdisr..." az functionapp config appsettings set --name <APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings "ASTRA_DB_CLIENT_SECRET=UB3Tm8cR,Ic..."
<APP_NAME>
and<RESOURCE_GROUP_NAME>
must be replaced with correct application and resurce group names used in the previous step. Similarly,ASTRA_DB_CLIENT_ID
andASTRA_DB_CLIENT_SECRET
must be assigned correct values generated for your database.
-
Copy the secure connect bundle file to the project directory. (See the Prerequisites section above if you need to download your secure connect bundle.)
-
Add cassandra-driver, a Python client library for Apache Cassandra, DataStax Astra DB and DataStax Enterprise, to the
requirements.txt
file: -
Replace the
__init__.py
content with:You can learn more about the code above by reading the python-driver documentation. Note thatfrom cassandra.cluster import Cluster from cassandra.auth import PlainTextAuthProvider import logging import azure.functions as func import os ASTRA_DB_CLIENT_ID = os.environ['ASTRA_DB_CLIENT_ID'] ASTRA_DB_CLIENT_SECRET = os.environ['ASTRA_DB_CLIENT_SECRET'] cloud_config= { 'secure_connect_bundle': 'secure-connect-bundle-for-your-database.zip', '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 main(req: func.HttpRequest) -> func.HttpResponse: session = cluster.connect() row = session.execute("SELECT cql_version FROM system.local WHERE key = 'local';").one() cql_version = row[0] logging.info(f"{cql_version} Success") return func.HttpResponse(f"{cql_version} Success")
secure-connect-bundle-for-your-database.zip
must be replaced with a correct file name for your secure connect bundle.
✅ 2. Deploy the function.¶
-
Use Astra CLI to deploy the updated function:
-
On the Microsoft Azure portal, find the newly deployed function:
✅ 3. Test the function.¶
-
Under Developer, select Code + Test and then Test/Run. Locate the Run button:
-
Click Run and observe the output and logs:
Notice the CQL version output 3.4.5 and status code 200.
Using Python SDK¶
✅ 1. Create a function.¶
-
Follow the Quickstart to create a Python function in Azure from the command line using the
v1
Python programming model and Azure CLI. Complete all the steps to successfully deploy and test the function in Azure. -
Use Azure CLI to add these runtime environment variables to the application settings:
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 thataz functionapp config appsettings set --name <APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings "ASTRA_DB_ID=0c2f6f34-41ea-..." az functionapp config appsettings set --name <APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings "ASTRA_DB_REGION=us-east1" az functionapp config appsettings set --name <APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings "ASTRA_DB_APPLICATION_TOKEN=AstraCS:avDWzU..."
<APP_NAME>
and<RESOURCE_GROUP_NAME>
must be replaced with correct application and resurce group names used in the previous step. Similarly,ASTRA_DB_ID
,ASTRA_DB_REGION
andASTRA_DB_APPLICATION_TOKEN
must be assigned correct values associated with your database.
-
Add AstraPy, a Pythonic SDK for DataStax Astra and Stargate, to the
requirements.txt
file: -
Replace the
__init__.py
content with:You can learn more about the code above by reading the AstraPy documentation.from astrapy.rest import create_client, http_methods import logging import azure.functions as func import os ASTRA_DB_ID = os.environ['ASTRA_DB_ID'] ASTRA_DB_REGION = os.environ['ASTRA_DB_REGION'] ASTRA_DB_APPLICATION_TOKEN = os.environ['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) def main(req: func.HttpRequest) -> func.HttpResponse: res = astra_http_client.request( method=http_methods.GET, path=f"/api/rest/v2/keyspaces/system/local/local" ) cql_version = res["data"][0]['cql_version'] logging.info(f"{cql_version} Success") return func.HttpResponse(f"{cql_version} Success")
✅ 2. Deploy the function.¶
-
Use Astra CLI to deploy the updated function:
-
On the Microsoft Azure portal, find the newly deployed function:
✅ 3. Test the function.¶
-
Under Developer, select Code + Test and then Test/Run. Locate the Run button:
-
Click Run and observe the output and logs:
Notice the CQL version output 3.4.5 and status code 200.