Deploy dags from an AWS S3 bucket to Astro using AWS Lambda
Use the following CI/CD template to automate deploying Apache Airflow dags from an S3 bucket to Astro using AWS Lambda.
Prerequisites
- An AWS S3 bucket
- An Astro Deployment with dag-only deploys enabled.
- A Deployment API token, Workspace API token, or Organization API token.
- An Astro project containing your project configurations.
Dag deploy template
This CI/CD template can be used to deploy dags from a single S3 bucket to a single Astro Deployment. When you create or modify a dag in the S3 bucket, a Lambda function triggers and initializes an astro
project to deploy your dags using Astro CLI.
dags
folder isn’t up to date with your S3 dags bucket when you trigger this deploy, you will revert your dags back to the version hosted in your Astro project.-
Download the latest Astro CLI binary from GitHub releases, then rename the file to,
astro_cli.tar.gz
. For example, to use Astro CLI version 1.13.0 in your template, downloadastro_1.13.0_linux_amd64.tar.gz
and rename it toastro_cli.tar.gz
. -
In your S3 bucket, create the following new folders:
dags
cli_binary
-
Add
astro_cli.tar.gz
tocli_binary
. -
In the AWS IAM console, create a new role for AWS Lambda with the following permissions. Replace
<account_id>
,<lambda_function_name>
, and<bucket_name>
with your values. -
Author a new AWS Lambda function from scratch with the following configurations:
- Function name: Any
- Runtime: Python 3.9
- Architecture: Any
- Execution role: Click Use an existing role and enter the role you created.
-
Configure the following Lambda environment variables for your Lambda function:
ASTRO_HOME
:\tmp
ASTRO_API_TOKEN
: The value for your Workspace or Organization API token.ASTRO_DEPLOYMENT_ID
: Your Deployment ID.
For production Deployments, Astronomer recommends storing your API credentials in AWS Secrets Manager and referencing them from Lambda. See https://docs.aws.amazon.com/lambda/latest/dg/configuration-database.html
-
Add the following code to
lambda_function.py
. Replace<bucket_name>
with your value. -
Create a trigger for your Lambda function with the following configuration:
- Source: Select S3.
- Bucket: Select the bucket that contains your
dags
directory. - Event types: Select All object create events.
- Prefix: Enter
dags/
. - Suffix: Enter
.py
.
-
If you haven’t already, deploy your complete Astro project to your Deployment. See Deploy code.
If you stage multiple commits to dag files and push them all at once to your remote branch, the template only deploys dag code changes from the most recent commit. It will miss any code changes made in previous commits.
To avoid this, either push commits individually or configure your repository to Squash commits for pull requests that merge multiple commits simultaneously.
- Add your dags to the
dags
folder in your storage bucket - In the Astro UI, select a Workspace, click Deployments, and then select your Deployment. Confirm that your Lambda function worked by checking the Deployment Dag bundle version. The version’s name should include the time that you added the dags to your S3 bucket.