Authenticate Astro to Azure
Prerequisites
- A user account on Azure with access to Azure cloud resources.
- The Azure CLI.
- The Astro CLI.
- An Astro project.
- If you’re using Windows, Windows Subsystem Linux.
Retrieve Azure user credentials locally
Run the following command to obtain your user credentials locally:
The CLI provides you with a link to a webpage where you authenticate to your Azure account. Once you complete the login, the CLI stores your user credentials in your local Azure configuration folder. The developer account credentials are used in place of the credentials associated with the Registered Application (Service Principal) in Microsoft Entra ID.
The default location of the Azure configuration folder depends on your operating system:
- Linux:
$HOME/.azure/ - Mac:
/Users/<username>/.azure - Windows:
%USERPROFILE%/.azure/
Configure your Astro project
docker-compose.override.yml. For Airflow 2, replace api-server with webserver and remove the dag-processor block.The Astro CLI runs Airflow in a Docker-based environment. To give Airflow access to your credential files, mount the .azure folder as a volume in Docker.
- In your Astro project, create a file named
docker-compose.override.ymlwith the following configuration:
Mac
Windows and Linux
- Add the following lines after the
FROMline in yourDockerfileto install the Azure CLI inside your Astro Runtime image:
If you’re using an Apple M1 Mac, you must use thelinux/amd64 distribution of Astro Runtime. Replace the first line in the Dockerfile of your Astro project with:
- Add the following environment variable to your
.envfile. Make sure the file path is the same volume location you configured indocker-compose.override.yml:
When you run Airflow locally, all Azure connections without defined credentials automatically fall back to your user credentials when connecting to Azure. Airflow applies and overrides user credentials for Azure connections in the following order:
- Mounted user credentials in
/~/.azure. - Configurations in
azure_client_id,azure_tenant_id, andazure_client_secret. - An explicit username & password provided in the connection.
For example, if you completed the configuration in this document and then created a new Azure connection with its own username and password, Airflow would use those credentials instead of the credentials in ~/.azure/config.
Test your credentials with a secrets backend
Now that Airflow has access to your user credentials, you can use them to connect to your cloud services. Use the following example setup to test your credentials by pulling values from different secrets backends.
-
Create a secret for an Airflow variable or connection in Azure Key Vault. All Airflow variables and connection keys must be prefixed with the following strings respectively:
airflow-variables-<my_variable_name>airflow-connections-<my_connection_name>
For example, to use a secret named
mysecretvarin your dag, you must name the secretairflow-variables-mysecretvar.You will need to store your connection in URI format.
-
In your Astro project, add the following line to Astro project
requirements.txtfile: -
Add the following environment variables to your Astro project
.envfile. For additional configuration options, see the Apache Airflow documentation. Make sure to specify yourvault_url.By default, this setup requires that you prefix any secret names in Key Vault with
airflow-connectionsorairflow-variables. If you don’t want to use prefixes in your Key Vault secret names, set the values for"connections_prefix"and"variables_prefix"to""withinAIRFLOW__SECRETS__BACKEND_KWARGS. Thevault_urlcan be found on the overview page of your Key vault underVault URI. -
Run the following command to start Airflow locally:
-
Access the Airflow UI at
localhost:8080and create an Airflow Azure connection namedazure_standardwith no credentials. See Connections.When you use this connection in your dag, it will fall back to using your configured user credentials.
-
Add a dag which uses the secrets backend to your Astro project
dagsdirectory. You can use the following example dag to retrieve a value fromairflow/variablesand print it to the terminal: -
In the Airflow UI, unpause your dag and click Play to trigger a dag run.
-
View logs for your dag run. If the connection was successful, your masked secrets appear in your logs. See Airflow logging.
