Cloud Composer 3 | Cloud Composer 2 | Cloud Composer 1
本頁面說明如何為 Cloud Composer 環境設定 SMTP 服務。
事前準備
如果 Cloud Composer 環境已設定私人 IP,請確認該環境可連線至外部電子郵件服務的 API 端點 (例如
https://api.sendgrid.com
或您偏好的 SMTP 伺服器)。
設定 SendGrid 電子郵件服務
如要接收通知,請設定環境變數,透過 SendGrid 電子郵件服務傳送電子郵件。
註冊 SendGrid
如果您尚未註冊 SendGrid,請在 Google Cloud 控制台註冊,然後依序按一下「Manage On Provider」(管理供應商) 前往 SendGrid 網域,然後按一下「Settings」(設定) 擷取使用者名稱並建立 API 金鑰。 Google Cloud 開發人員一開始可享有每月 12,000 封免費電子郵件的配額。
設定變數
SendGrid 會使用 API 金鑰和寄件者電子郵件地址傳送郵件。您可以透過下列任一選項提供這項資訊:
在 Secret Manager 中儲存值
如要在 Secret Manager 中儲存值,請按照下列步驟操作:
為環境設定 Secret Manager。請務必為密鑰後端設定權限和 Airflow 設定選項。
覆寫下列 Airflow 設定選項:
區段 鍵 值 email
email_conn_id
sendgrid_default
email
email_backend
airflow.providers.sendgrid.utils.emailer.send_email
為名為
airflow-connections-sendgrid_default
的 SendGrid 連線建立密鑰。將密鑰的值設為連線 URI。例如:sendgrid://<username>:<sendgrid_api_key>@smtp.sendgrid.net:587
如要進一步瞭解如何在 Secret Manager 中儲存連線,請參閱「管理 Airflow 連線」。
設定 SendGrid 的電子郵件地址。您無法透過密鑰設定電子郵件地址。請改為覆寫下列 Airflow 設定選項:
區段 鍵 值 email
from_email
寄件者電子郵件地址,例如 noreply@example.com
。
在 Airflow 中儲存值
覆寫下列 Airflow 設定選項:
區段 鍵 值 email
email_conn_id
sendgrid_default
email
email_backend
airflow.providers.sendgrid.utils.emailer.send_email
在 Airflow 中,設定名為
sendgrid_default
的連線。在連線 URI 中指定 Sendgrid API 金鑰。例如:gcloud composer environments run ENVIRONMENT_NAME \ --location LOCATION \ connections add -- \ --conn-uri "sendgrid://USERNAME:SENDGRID_API_KEY@smtp.sendgrid.net:587" \ sendgrid_default
更改下列內容:
ENVIRONMENT_NAME
:環境名稱。LOCATION
:環境所在的地區。USERNAME
:SendGrid 使用者名稱。SENDGRID_API_KEY
:SendGrid API 金鑰。
設定 SendGrid 的電子郵件地址。您無法透過密鑰設定電子郵件地址。請改為覆寫下列 Airflow 設定選項:
區段 鍵 值 email
from_email
寄件者電子郵件地址,例如 noreply@example.com
。
測試 SendGrid 設定
按照下列步驟測試 SendGrid 設定:
- 建立使用
EmailOperator
的測試 DAG。例如:
- 將 DAG 上傳至環境,並檢查工作是否成功。
- 使用您的 SendGrid 憑證登入 SendGrid。
- 在 SendGrid UI 中,前往「Activity」頁面。
- 在清單中搜尋電子郵件。您會看到 SendGrid 處理並發送了電子郵件。
如果電子郵件未處理及送達,請執行下列操作:
- 檢查 SendGrid 設定。
- 確認您已啟用 Secret Manager 後端。請確認您已授予額外權限,並設定 Airflow 設定選項的覆寫值。
- 檢查電子郵件用戶端的垃圾郵件篩選器。
設定第三方 SMTP 服務
如要透過第三方 SMTP 服務傳送電子郵件,請覆寫 email_backend
Airflow 設定選項,並設定其他 SMTP 相關參數。
如要設定第三方 SMTP 服務,請覆寫下列 Airflow 設定選項:
區段 | 鍵 | 值 |
---|---|---|
email |
email_backend |
airflow.utils.email.send_email_smtp |
smtp |
smtp_host |
SMTP 伺服器的主機名稱。 |
smtp |
smtp_user |
SMTP 伺服器的使用者名稱。 |
smtp |
smtp_port |
SMTP 伺服器的通訊埠。無法使用通訊埠 25。您可以使用其他通訊埠,例如標準 SMTP 通訊埠 465 和 587。 |
smtp |
smtp_password |
系統不支援透過 smtp_password 設定密碼。如要設定 SMTP 密碼,請按照「設定 SMTP 密碼」一文中的操作說明進行。 |
smtp |
smtp_mail_from |
寄件者電子郵件地址,例如 noreply-composer@ 。 |
smtp |
smtp_starttls |
為了提高安全性,請設為 True 。 |
smtp |
smtp_ssl |
為了提高安全性,請設為 True 。 |
為第三方 SMTP 服務設定 SMTP 密碼
在 Airflow 設定檔中以純文字形式儲存 SMTP 密碼,是一種不良的安全做法。因此 Cloud Composer 不支援這種方法。您可以改用其他兩種方法設定 SMTP 密碼。
使用指令擷取 SMTP 密碼
您無法在 Cloud Composer 1 中使用指令擷取 SMTP 密碼。在 Airflow UI 中,透過指令提供密碼會觸發錯誤,導致無法存取「管理」>「設定」。
使用儲存在 Secret Manager 中的密鑰來擷取 SMTP 密碼
您可以將 Secret Manager 設定為 Airflow 密鑰後端。
為 Composer 環境設定 Secret Manager 後,您就可以在 Secret Manager 中儲存 SMTP 密碼:
建立新的密鑰:
echo -n "SMTP_PASSWORD" | gcloud beta secrets create \ airflow-config-smtp-password \ --data-file=- \ --replication-policy=automatic
將
SMTP_PASSWORD
替換為您的 SMTP 密碼。設定 Airflow 以便從 Secret Manager 取得 SMTP 密碼。如要這樣做,請覆寫下列 Airflow 設定選項:
區段 鍵 值 smtp
smtp_password_secret
smtp-password