Hi @Swathi
Thank you for posting you query!
The issue you are facing occurs because Databricks does not automatically expand secrets in cluster configurations. The {{secrets/...}}
syntax only works within Databricks notebooks or jobs when using dbutils.secrets.get()
, but not in the cluster’s Spark configurations.
Here's how to properly set up your ADLS access with secrets:
How to set up secrets in cluster configs?
Using Init Scripts for Cluster-Level Secrets - For cluster-wide settings, meaning settings that apply to all notebooks and jobs on the cluster, you'll want to use an init script.
Example Python Init Script:
Create a file (e.g., adls_secrets_init.py
) with the following Python code:
from pyspark.sql import SparkSession
# Define function to get dbutils
def get_dbutils():
try:
from pyspark.dbutils import DBUtils
return DBUtils(SparkSession.builder.getOrCreate())
except ImportError:
import IPython
return IPython.get_ipython().user_ns["dbutils"]
# Initialize dbutils
dbutils = get_dbutils()
# Retrieve secrets from Databricks Secret Scope
client_id = dbutils.secrets.get(scope="<secret-scope>", key="<client-id-key>")
client_secret = dbutils.secrets.get(scope="<secret-scope>", key="<client-secret-key>")
tenant_id = dbutils.secrets.get(scope="<secret-scope>", key="<tenant-id-key>")
# Get Spark session
spark = SparkSession.builder.getOrCreate()
# Set Spark configurations
spark.conf.set("fs.azure.account.oauth2.client.id.<storage-account>.dfs.core.windows.net", client_id)
spark.conf.set("fs.azure.account.oauth2.client.secret.<storage-account>.dfs.core.windows.net", client_secret)
spark.conf.set("fs.azure.account.oauth2.client.endpoint.<storage-account>.dfs.core.windows.net", f"https://login.microsoftonline.com/{tenant_id}/oauth2/token")
Important - Replace
<secret-scope>
,<client-id-key>
,<client-secret-key>
,<tenant-id-key>
, and<storage-account>
with your actual values. Save this file to a location accessible by your Databricks cluster (e.g., DBFS).
Configure Your Cluster - In your Databricks cluster configuration, go to "Advanced Options" -> "Init Scripts." Add the init script you created (either by uploading it or specifying the DBFS path). Restart your cluster.
Verify the Secret Scope - Double-check that your Secret Scope is correctly set up in Databricks and contains the correct client ID, client secret, and tenant ID.
Is this related to enabling the dbutils.secrets.get functionality in our Databricks cluster to securely access the secrets? If so, will it solve the issue?
Why This Works?
Secrets are retrieved at runtime using dbutils.secrets.get()
, ensuring security. The init script exports them as environment variables. Spark configs use environment variables instead of hardcoded secrets. This approach ensures that your Databricks cluster can access ADLS securely without hardcoding credentials.
I hope this information helps. Please do let us know if you have any further queries.
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful. And, if you have any further query do let us know.