|
| 1 | +# MWAA Serverless Examples |
| 2 | + |
| 3 | +This directory contains example MWAA Serverless DAGs demonstrating integration with various AWS services. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +These DAGs demonstrate how to use Airflow 3.0.6 with AWS services, including proper IAM permissions, error handling, and resource management. Each DAG is self-contained and includes inline documentation. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- Amazon MWAA environment (version 3.0.6 or later) |
| 12 | +- AWS CLI configured with appropriate permissions |
| 13 | +- IAM roles with required permissions (see [IAM Policy](#iam-policy)) |
| 14 | + |
| 15 | +---- |
| 16 | +*Note:* Throughout this post, we use example values that you'll need to replace with your own: |
| 17 | +- Replace `amzn-s3-demo-bucket` with your S3 bucket name |
| 18 | +- Replace `111122223333` with your AWS account ID |
| 19 | +- Replace `us-east-2` with your AWS Region. MWAA Serverless is available in multiple AWS Regions. Check the [List of AWS Services Available by Region](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/) for current availability. |
| 20 | + |
| 21 | +---- |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## DAG Examples |
| 26 | + |
| 27 | +### S3 Operations (`s3_dag.yaml`) |
| 28 | +Demonstrates S3 bucket and object operations including: |
| 29 | +- Creating and deleting S3 buckets |
| 30 | +- Creating, listing, and deleting S3 objects |
| 31 | +- Using S3KeySensor to wait for objects |
| 32 | + |
| 33 | +### AWS Glue Jobs (`glue_dag.yaml`) |
| 34 | +Illustrates Glue ETL job execution: |
| 35 | +- Creating and uploading Glue scripts to S3 |
| 36 | +- Running Glue 5.0 jobs with Python |
| 37 | +- Monitoring job completion with sensors |
| 38 | + |
| 39 | +### Amazon Athena (`athena_dag.yaml`) |
| 40 | +Demonstrates Athena query execution: |
| 41 | +- Running SQL queries against data lakes |
| 42 | +- Managing query results and outputs |
| 43 | +- Integration with CloudFormation for resource creation |
| 44 | + |
| 45 | + |
| 46 | +## Setup |
| 47 | + |
| 48 | +1. **Upload YAML files to your S3 bucket:** |
| 49 | + ```bash |
| 50 | + aws s3 cp . s3://amzn-s3-demo-bucket/yaml_dags/ --recursive --exclude "*.md" --exclude "*.json" |
| 51 | + ``` |
| 52 | + |
| 53 | +2. **Create the required IAM execution role:** |
| 54 | + ```bash |
| 55 | + aws iam create-role \ |
| 56 | + --role-name mwaa-serverless-execution-role \ |
| 57 | + --assume-role-policy-document file://trust-policy.json |
| 58 | + |
| 59 | + aws iam put-role-policy \ |
| 60 | + --role-name mwaa-serverless-execution-role \ |
| 61 | + --policy-name mwaa-airflow3-policy \ |
| 62 | + --policy-document file://mwaa-comprehensive-policy.json |
| 63 | + ``` |
| 64 | + |
| 65 | +3. **Create your MWAA Serverless** to use the defnitions, i.e..: |
| 66 | + ```bash |
| 67 | + aws mwaa-serverless create-workflow \ |
| 68 | + --name athena_dag \ |
| 69 | + --definition-s3-location '{ "Bucket": "amzn-s3-demo-bucket", "ObjectKey": "yaml_dags/athena_dag.yaml" }' \ |
| 70 | + --role-arn arn:aws:iam::111122223333:role/mwaa-serverless-access-role \ |
| 71 | + --region us-east-2 |
| 72 | + ``` |
| 73 | + |
| 74 | +## IAM Policy |
| 75 | + |
| 76 | +The comprehensive IAM policy required for all DAGs includes permissions for: |
| 77 | + |
| 78 | +- S3 operations (bucket and object management) |
| 79 | +- Lambda function lifecycle management |
| 80 | +- Glue job creation and execution |
| 81 | +- Athena query execution |
| 82 | +- Batch job submission and monitoring |
| 83 | +- EMR Serverless application management |
| 84 | +- SageMaker processing jobs |
| 85 | +- CloudFormation stack operations |
| 86 | +- CloudWatch logging |
| 87 | + |
| 88 | +```json |
| 89 | +
|
| 90 | +{ |
| 91 | + "Version": "2012-10-17", |
| 92 | + "Statement": [ |
| 93 | + { |
| 94 | + "Sid": "S3Permissions", |
| 95 | + "Effect": "Allow", |
| 96 | + "Action": [ |
| 97 | + "s3:CreateBucket", |
| 98 | + "s3:DeleteBucket", |
| 99 | + "s3:ListBucket", |
| 100 | + "s3:GetObject", |
| 101 | + "s3:PutObject", |
| 102 | + "s3:DeleteObject" |
| 103 | + ], |
| 104 | + "Resource": [ |
| 105 | + "arn:aws:s3:::*", |
| 106 | + "arn:aws:s3:::*/*" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "Sid": "GluePermissions", |
| 111 | + "Effect": "Allow", |
| 112 | + "Action": [ |
| 113 | + "glue:CreateJob", |
| 114 | + "glue:GetJob", |
| 115 | + "glue:StartJobRun", |
| 116 | + "glue:GetJobRun", |
| 117 | + "glue:GetTable", |
| 118 | + "glue:CreateTable", |
| 119 | + "glue:DeleteTable", |
| 120 | + "glue:CreateDatabase", |
| 121 | + "glue:DeleteDatabase" |
| 122 | + ], |
| 123 | + "Resource": "*" |
| 124 | + }, |
| 125 | + { |
| 126 | + "Sid": "AthenaPermissions", |
| 127 | + "Effect": "Allow", |
| 128 | + "Action": [ |
| 129 | + "athena:StartQueryExecution", |
| 130 | + "athena:GetQueryExecution", |
| 131 | + "athena:GetQueryResults" |
| 132 | + ], |
| 133 | + "Resource": "*" |
| 134 | + }, |
| 135 | + { |
| 136 | + "Sid": "CloudwatchPermissions", |
| 137 | + "Effect": "Allow", |
| 138 | + "Action": [ |
| 139 | + "logs:CreateLogGroup", |
| 140 | + "logs:CreateLogStream", |
| 141 | + "logs:PutLogEvents" |
| 142 | + ], |
| 143 | + "Resource": "*" |
| 144 | + }, |
| 145 | + { |
| 146 | + "Sid": "CloudFormationPermissions", |
| 147 | + "Effect": "Allow", |
| 148 | + "Action": [ |
| 149 | + "cloudformation:CreateStack", |
| 150 | + "cloudformation:DeleteStack", |
| 151 | + "cloudformation:DescribeStacks" |
| 152 | + ], |
| 153 | + "Resource": "*" |
| 154 | + }, |
| 155 | + { |
| 156 | + "Sid": "CloudWatchLogsPermissions", |
| 157 | + "Effect": "Allow", |
| 158 | + "Action": [ |
| 159 | + "logs:CreateLogGroup", |
| 160 | + "logs:CreateLogStream", |
| 161 | + "logs:PutLogEvents" |
| 162 | + ], |
| 163 | + "Resource": "*" |
| 164 | + }, |
| 165 | + { |
| 166 | + "Sid": "IAMPassRolePermissions", |
| 167 | + "Effect": "Allow", |
| 168 | + "Action": [ |
| 169 | + "iam:PassRole", |
| 170 | + "iam:GetRole" |
| 171 | + ], |
| 172 | + "Resource": "*" |
| 173 | + } |
| 174 | + ] |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +### Trust Relationships |
| 179 | + |
| 180 | +Your execution role needs trust relationships for: |
| 181 | + |
| 182 | + |
| 183 | +**Service Roles (for PassRole operations):** |
| 184 | +```json |
| 185 | +{ |
| 186 | + "Version": "2012-10-17", |
| 187 | + "Statement": [ |
| 188 | + { |
| 189 | + "Effect": "Allow", |
| 190 | + "Principal": { |
| 191 | + "Service": [ |
| 192 | + "airflow-serverless.amazonaws.com", |
| 193 | + "glue.amazonaws.com" |
| 194 | + ] |
| 195 | + }, |
| 196 | + "Action": "sts:AssumeRole" |
| 197 | + } |
| 198 | + ] |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +## Usage |
| 203 | + |
| 204 | +1. **Enable DAGs** in the Airflow UI |
| 205 | +2. **Configure parameters** as needed for your environment |
| 206 | +3. **Trigger DAGs** manually or via schedule |
| 207 | +4. **Monitor execution** through CloudWatch logs |
| 208 | + |
| 209 | +## Configuration |
| 210 | + |
| 211 | +Most DAGs use parameters that can be customized: |
| 212 | + |
| 213 | +- `role_arn`: IAM role for service operations |
| 214 | +- `s3_bucket`: S3 bucket for data and scripts |
| 215 | +- `region`: AWS region for resources |
| 216 | + |
| 217 | +Update these parameters in the Airflow UI or via environment variables. |
| 218 | + |
| 219 | +## Troubleshooting |
| 220 | + |
| 221 | +### Common Issues |
| 222 | + |
| 223 | +- **IAM Permission Errors**: Ensure your execution role has all required permissions |
| 224 | +- **Resource Not Found**: Verify S3 buckets and objects exist before running DAGs |
| 225 | +- **Timeout Issues**: Adjust timeout values for long-running jobs |
| 226 | + |
| 227 | +### Logs |
| 228 | + |
| 229 | +Check CloudWatch logs for detailed error information: |
| 230 | +- Airflow task logs: `/aws/amazonmwaa/[environment-name]/task` |
| 231 | +- Service-specific logs: `/aws/glue/`, `/aws/athena/`, etc. |
| 232 | + |
| 233 | +## Clean Up |
| 234 | + |
| 235 | +Each DAG includes cleanup tasks where appropriate. For manual cleanup: |
| 236 | + |
| 237 | +```bash |
| 238 | +# Remove uploaded files |
| 239 | +aws s3 rm s3://amzn-s3-demo-bucket/dags/ --recursive |
| 240 | +
|
| 241 | +# Delete IAM role and policies |
| 242 | +aws iam delete-role-policy --role-name mwaa-airflow3-execution-role --policy-name mwaa-airflow3-policy |
| 243 | +aws iam delete-role --role-name mwaa-airflow3-execution-role |
| 244 | +``` |
| 245 | + |
| 246 | +## Security |
| 247 | + |
| 248 | +- Always use least-privilege IAM permissions |
| 249 | +- Sensitive data should be stored in AWS Secrets Manager |
| 250 | +- Network access is controlled through VPC configuration |
| 251 | + |
| 252 | +## Contributing |
| 253 | + |
| 254 | +See [CONTRIBUTING](../../../CONTRIBUTING.md) for guidelines on contributing to this repository. |
| 255 | + |
| 256 | +## License |
| 257 | + |
| 258 | +This library is licensed under the MIT-0 License. See the [LICENSE](../../../LICENSE) file. |
0 commit comments