Configure Azure Key Vault as a secrets backend on Astro Private Cloud
In this section, you’ll learn how to use Azure Key Vault as a secrets backend on Astro Private Cloud.
Prerequisites
- A Deployment.
- The Astro CLI.
- An Astro project initialized with
astro dev init
. - An existing Azure Key Vault linked to a resource group.
- Your Key Vault URL. To find this, go to your Key Vault overview page > Vault URI.
If you do not already have Key Vault configured, see the Microsoft Azure documentation.
Step 1: Register Astro Private Cloud as an app on Azure
Follow the Microsoft Azure documentation to register a new application for Astro Private Cloud.
At a minimum, you need to add a secret that Astro Private Cloud can use to authenticate to Key Vault.
Note the value of the application’s client ID and secret for Step 3.
Step 2: Create an access policy
Follow the Microsoft documentation to create a new access policy for the application that you just registered. The settings you need to configure for your policy are:
- Configure from template: Select
Key, Secret, & Certificate Management
. - Select principal: Select the name of the application that you registered in Step 1.
Step 3: Set up Key Vault locally
In your Astro project, add the following line to your requirements.txt
file:
In your Dockerfile
, add the following environment variables with your own values:
This tells Airflow to look for variable information at the airflow-variables-*
path in Azure Key Vault and connection information at the airflow-connections-*
path. In the next step, you’ll run an example dag to test this configuration locally.
airflow-connections
or airflow-variables
. If you don’t want to use prefixes in your Key Vault secret names, set the values for sep
, "connections_prefix"
, and "variables_prefix"
to ""
within AIRFLOW__SECRETS__BACKEND_KWARGS
.If you want to deploy your project to a hosted Git repository before deploying to Astronomer, be sure to save <your-client-id>
, <your-tenant-id>
, and <your-client-secret>
in a secure manner. When you deploy to Astronomer, you should set these values as secrets with the Astro Private Cloud UI.
Step 4: Test Key Vault locally
To test your Key Vault setup on Astro Private Cloud locally, create a new secret in Key Vault containing either a variable or a connection.
Once you create a test secret, write a simple dag which calls the secret and add this dag to your project’s dags
directory. For example, you can use the following dag to print the value of a variable to your task logs:
To test your changes:
-
Run
astro dev stop
followed byastro dev start
to push your changes to your local Airflow environment. -
In the Airflow UI (
http://localhost:8080/admin/
), trigger your new dag. -
Click on
test-task
> View Logs. If you ran the example dag above, you should see the contents of your secret in the task logs:
Once you confirm that the setup was successful, you can delete this dag.
Step 5: Push changes to Astro Private Cloud
Once you’ve confirmed that your secrets are being imported correctly to your local environment, you’re ready to configure the same feature in a Deployment on Astro Private Cloud.
- In the Astro Private Cloud UI, add the same environment variables found in your
Dockerfile
to your Deployment environment variables. Specify theAZURE_CLIENT_ID
,AZURE_TENANT_ID
, andAZURE_CLIENT_SECRET
variables as Secret to ensure that your credentials are stored securely. - In your Astro project, delete the environment variables from your
Dockerfile
. - Deploy your changes to Astro Private Cloud.
From here, you can store any Airflow variables or connections as secrets on Key Vault and use them in your project.