

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在 Amazon MWAA 上使用 Apache Airflow 配置选项
<a name="configuring-env-variables"></a>

Apache Airflow 配置选项可以作为环境变量附加到 Amazon MWAA 环境中。您可以从建议的下拉列表中进行选择，也可以在 Amazon MWAA 控制台上为 Apache Airflow 版本指定自定义配置选项。本主题介绍可用的 Apache Airflow 配置选项，以及如何使用这些选项来覆盖环境中的 Apache Airflow 配置设置。

**Contents**
+ [先决条件](#configuring-env-variables-prereqs)
+ [工作方式](#configuring-env-variables-how)
+ [使用配置选项加载插件](#configuring-2.0-airflow-override)
+ [配置选项概述](#configuring-env-variables-customizing)
  + [Apache Airflow 配置选项](#configuring-env-variables-airflow-ref)
  + [Apache Airflow 参考](#configuring-env-variables-reference-options)
  + [使用 Amazon MWAA 控制台](#configuring-env-variables-console-add)
+ [配置参考](#configuring-env-variables-reference)
  + [电子邮件配置](#configuring-env-variables-email)
  + [任务配置数](#configuring-env-variables-tasks)
  + [计划程序配置数](#configuring-env-variables-scheduler)
  + [工作线程配置数](#configuring-env-variables-workers)
  + [Web 服务器配置](#configuring-env-variables-webserver)
  + [触发器配置](#configuring-env-variables-webserver)
+ [示例和示例代码](#configuring-env-variables-code)
  + [示例 DAG](#configuring-env-variables-dag)
  + [示例电子邮件通知设置](#configuring-env-variables-email)
+ [接下来做什么？](#configuring-env-variables-next-up)

## 先决条件
<a name="configuring-env-variables-prereqs"></a>

在完成本页上的步骤之前，您需要具备以下条件。
+ **权限** — 您的 AWS 账户 必须已获得管理员授权，访问适用于环境的 [AmazonMWAAFullConsoleAccess](access-policies.md#console-full-access) 访问控制策略。此外，[执行角色](mwaa-create-role.md)必须允许 Amazon MWAA 环境访问环境所使用的 AWS 资源。
+ **访问权限** — 如果您需要访问公共存储库以便直接在 Web 服务器上安装依赖项，则必须将环境配置为具有**公共网络** Web 服务器访问权限。有关更多信息，请参阅[Apache Airflow 访问模式](configuring-networking.md)。
+ **Amazon S3 配置** — 用于存储 DAG 的 [Amazon S3 存储桶](mwaa-s3-bucket.md)、在 `plugins.zip` 中的自定义插件和在 `requirements.txt` 中的 Python 依赖项必须配置为*已阻止公共访问*和*已启用版本控制*。

## 工作方式
<a name="configuring-env-variables-how"></a>

创建环境时，Amazon MWAA 会将您在 Amazon MWAA 控制台的 **Airflow 配置选项**中指定的配置设置作为环境变量附加到环境 AWS Fargate 容器中。如果您在 `airflow.cfg` 中使用同名设置，则您在 Amazon MWAA 控制台上指定的选项将覆盖 `airflow.cfg` 中的值。

虽然默认情况下我们不会在 Amazon MWAA 环境的 Apache Airflow UI 中公开 `airflow.cfg`，但您可以直接在 Amazon MWAA 控制台上更改 Apache Airflow 配置选项，然后通过设置 `webserver.expose_config` 来公开配置。

## 使用配置选项加载插件
<a name="configuring-2.0-airflow-override"></a>

默认情况下，在 Apache Airflow v2 和更高版本中，使用 `core.lazy_load_plugins : True` 设置将插件配置为“延迟”加载。如果您使用自定义插件，则必须添加 `core.lazy_load_plugins : False` 为 Apache Airflow 配置选项，以便在每个 Airflow 流程开始时加载插件，从而覆盖默认设置。

## 配置选项概述
<a name="configuring-env-variables-customizing"></a>

当您在 Amazon MWAA 控制台上添加配置时，Amazon MWAA 会将该配置作为环境变量写入。
+ **列出的选项**。您可以在下拉列表中从适用于 Apache Airflow 的版本中选择一项配置设置。例如 `dag_concurrency` : `16`。配置设置将环境的 Fargate 容器转化为 `AIRFLOW__CORE__DAG_CONCURRENCY : 16`
+ **自定义选项**。您还可以指定未在下拉列表中列出的 Apache Airflow 版本的 Airflow 配置选项。例如 `foo.user` : `YOUR_USER_NAME`。配置设置将环境的 Fargate 容器转化为 `AIRFLOW__FOO__USER : YOUR_USER_NAME`

### Apache Airflow 配置选项
<a name="configuring-env-variables-airflow-ref"></a>

下图显示了您可以在 Amazon MWAA 控制台上自定义 **Apache Airflow 配置选项**的位置。

![此图显示了您可以在 Amazon MWAA 控制台上自定义 Apache Airflow 配置选项的位置。](http://docs.aws.amazon.com/zh_cn/mwaa/latest/userguide/images/mwaa-console-airflow-config.png)


### Apache Airflow 参考
<a name="configuring-env-variables-reference-options"></a>

有关 Apache Airflow 支持的配置选项列表，请参阅*《Apache Airflow 参考指南》*中的[配置参考](https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html)。要访问您在 Amazon MWAA 上运行的 Apache Airflow 版本的选项，请从下拉列表中选择版本。

### 使用 Amazon MWAA 控制台
<a name="configuring-env-variables-console-add"></a>

以下过程将指导您完成将 Airflow 配置选项添加到环境中的步骤。

1. 在 Amazon MWAA 控制台上打开[环境页面](https://console.aws.amazon.com/mwaa/home#/environments)。

1. 选择环境。

1. 选择**编辑**。

1. 选择**下一步**。

1. 在 **Airflow 配置选项**窗格中选择**添加自定义**配置。

1. 从下拉列表中选择配置并输入值，或者输入自定义配置并输入值。

1. 为每个您想要添加的配置选择**添加自定义配置选项**。

1. 选择**保存**。

## 配置参考
<a name="configuring-env-variables-reference"></a>

下一节包含 Amazon MWAA 控制台下拉列表中可用的 Apache Airflow 配置列表。

### 电子邮件配置
<a name="configuring-env-variables-email"></a>

以下列表显示了 Amazon MWAA 上可用于 Apache Airflow v2 和 v3 的 Airflow 电子邮件通知配置选项。

我们建议对 SMTP 流量使用端口 587。默认情况下，AWS 会阻止所有 Amazon EC2 实例的端口 25 上的出站 SMTP 流量。要在端口 25 上发送出站流量，可[请求移除此限制](https://aws.amazon.com/premiumsupport/knowledge-center/ec2-port-25-throttle/)。


| Airflow 配置选项 | 描述 | 示例值 | 
| --- | --- | --- | 
| email.email\_backend | Apache Airflow 实用工具用于在 [email\_backend](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#email-backend) 中发送电子邮件通知。 | airflow.utils.email.send\_email\_smtp | 
| smtp.smtp\_host | 在 [smtp\_host](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#smtp-host) 中用作电子邮件地址的出站服务器的名称。 | localhost | 
| smtp.smtp\_starttls | 在 [smtp\_startttls](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#smtp-starttls) 中，传输层安全性协议 (TLS) 用于加密互联网上的电子邮件。 | False | 
| smtp.smtp\_sll | 安全套接字层（SSL）用于连接 [smtp\_ssl](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#smtp-ssl) 中的服务器和电子邮件客户端。 | True | 
| smtp.smtp\_port | 在 [smtp\_port](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#smtp-port) 中为服务器指定的传输控制协议（TCP）端口。 | 587 | 
| smtp.smtp\_mail\_from | [smtp\_mail\_from](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#smtp-mail-from) 中的出站电子邮件地址。 | myemail@domain.com | 

### 任务配置数
<a name="configuring-env-variables-tasks"></a>

以下列表显示了 Amazon MWAA 上可用于 Apache Airflow v2 和 v3 的 Airflow 任务下拉列表中可用的配置。


| Airflow 配置选项 | 描述 | 示例值 | 
| --- | --- | --- | 
| core.default\_task\_ret | 在 [default\_task\_retries](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#default-task-retries) 中重试 Apache Airflow 任务的次数。 | 3 | 
| core.parallelism | 可以在整个环境中并行运行的最大任务实例数（[并行](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#parallelism)）。 | 40 | 

### 计划程序配置数
<a name="configuring-env-variables-scheduler"></a>

以下列表显示了 Amazon MWAA 上可用于 Apache Airflow v2 和 v3 的下拉列表中可用的 Apache Airflow 计划程序配置。


| Airflow 配置选项 | 描述 | 示例值 | 
| --- | --- | --- | 
| scheduler.catchup\_by\_default | 告诉计划程序创建 DAG 运行以“赶上”在 [catchup\_by\_default](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#catchup-by-default) 中的特定时间间隔。 | False | 
| scheduler.scheduler\_zombie\_task\_task\_ 在 Apache Airflow v3 中不可用。  | 告诉计划程序是否将任务实例标记为失败并在 [scheduler\_zombie\_task\_treshold ](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#scheduler-zombie-task-threshold)中重新安排任务。 | 300 | 

### 工作线程配置数
<a name="configuring-env-variables-workers"></a>

以下列表显示了 Amazon MWAA 上可用于 Apache Airflow v2 和 v3 的下拉列表中可用的 Airflow 工作线程配置。


| Airflow 配置选项 | 描述 | 示例值 | 
| --- | --- | --- | 
| celery.worker\_autoscale | 在 [worker\_autoscale](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#worker-autoscale) 中使用 [Celery Executor](https://airflow.apache.org/docs/apache-airflow/2.0.2/executor/celery.html) 在任何工作线程上同时运行的最大和最小任务数。值必须按以下顺序以逗号分隔：`max_concurrency,min_concurrency`。 | 16,12 | 

### Web 服务器配置
<a name="configuring-env-variables-webserver"></a>

以下列表显示了 Amazon MWAA 上可用于 Apache Airflow v2 和 v3 的下拉列表中可用的 Apache Airflow Web 服务器配置。


| Airflow 配置选项 | 描述 | 示例值 | 
| --- | --- | --- | 
| 网络服务器.default\_ui\_timezone 在 Apache Airflow v3 中不可用。  | 在 [default\_ui\_timezone](https://airflow.apache.org/docs/apache-airflow/2.0.2/configurations-ref.html#default-ui-timezone) 中的默认 Apache Airflow UI 的日期时间设置。 设置 `default_ui_timezone` 选项不会更改 DAG 计划运行的时区。要更改 DAG 的时区，您可以使用自定义插件。有关更多信息，请参阅[在 Amazon MWAA 上更改 DAG 的时区](samples-plugins-timezone.md)。  | America/New\_York | 

### 触发器配置
<a name="configuring-env-variables-webserver"></a>

以下列表显示了在 Amazon MWAA 上可用于 Apache Airflow v2 和 v3 的 Apache Airflow [触发器](https://airflow.apache.org/docs/apache-airflow/stable/authoring-and-scheduling/deferring.html)配置。


| Airflow 配置选项 | 描述 | 示例值 | 
| --- | --- | --- | 
| mwaa.triggerer\_enabled | 用于在 Amazon MWAA 上激活和停用触发器。默认情况下，该值设置为 `True`。如果设置为 `False`，Amazon MWAA 将不会在计划程序上启动任何触发器进程。 | True | 
| triggerer.default\_capacity（在 v2 中）<br />trigger.capacity（在 v3 中） | 定义每个触发器可以并行运行的触发器数量。在 Amazon MWAA 上，此容量是按每个触发器和每个计划程序设置的，因为这两个组件并排运行。对于 small、medium、large、xlarge 和 2xlarge实例，每个调度器的默认值分别设置为 `60`、`125`、`250`、`500` 和 `1000`。 | 125 | 

## 示例和示例代码
<a name="configuring-env-variables-code"></a>

### 示例 DAG
<a name="configuring-env-variables-dag"></a>

您可以使用以下 DAG 来打印 `email_backend` Apache Airflow 配置选项。要运行以响应 Amazon MWAA 事件，请将代码复制到 Amazon S3 存储桶上环境的 DAG 文件夹。

```
from airflow.decorators import dag
				from datetime import datetime
				
				def print_var(**kwargs):
				email_backend = kwargs['conf'].get(section='email', key='email_backend')
				print("email_backend")
				return email_backend
				
				@dag(
				dag_id="print_env_variable_example",
				schedule_interval=None,     
				start_date=datetime({{yyyy}}, {{m}}, {{d}}),
				catchup=False,
				)
				def print_variable_dag():
				email_backend_test = PythonOperator(
				task_id="email_backend_test",
				python_callable=print_var,
				provide_context=True
				)
				
				print_variable_test = print_variable_dag()
```

### 示例电子邮件通知设置
<a name="configuring-env-variables-email"></a>

以下 Apache Airflow 配置选项可用于使用应用程序密码的 Gmail.com 电子邮件帐户。有关更多信息，请参阅*《Gmail 帮助参考指南》*中的[使用应用程序密码登录](https://support.google.com/mail/answer/185833?hl=en-GB)。

![此图显示了如何使用 MWAA 控制台上的 Apache Airflow 配置选项配置 gmail.com 电子邮件帐户。](http://docs.aws.amazon.com/zh_cn/mwaa/latest/userguide/images/mwaa-console-config-email-configuration.png)


## 接下来做什么？
<a name="configuring-env-variables-next-up"></a>
+ 要了解如何将 DAG 文件夹上传到 Amazon S3 存储桶，请参阅 [添加或更新 DAG](configuring-dag-folder.md)。