Use the following CI/CD template to automate deploying Apache Airflow dags from an S3 bucket to Astro using AWS Lambda.
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, download astro_1.13.0_linux_amd64.tar.gz and rename it to astro_cli.tar.gz.
In your S3 bucket, create the following new folders:
dagscli_binaryAdd astro_cli.tar.gz to cli_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:
Configure the following Lambda environment variables for your Lambda function:
ASTRO_HOME: \tmpASTRO_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:
dags directory.dags/..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.
dags folder in your storage bucket