翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Apache Airflow 接続 AWS Secrets Manager に でシークレットキーを使用する
次の例では、 AWS Secrets Manager を呼び出して、Amazon Managed Workflows for Apache Airflow で Apache Airflow 接続のシークレットキーを取得します。AWS Secrets Manager シークレットを使用した Apache Airflow 接続の設定 でのステップを完了していることが前提となります。
バージョン
このページのコード例は、Python 3.10 の Apache Airflow v2 と Python 3.11 の Apache Airflow v3 で使用できます。 https://peps.python.org/pep-0619/
前提条件
このページのサンプルコードを使用するには、以下が必要です。
-
「」に記載されているように、Apache Airflow 設定オプションとしての Secrets Manager バックエンドAWS Secrets Manager シークレットを使用した Apache Airflow 接続の設定。
-
にリストされている Secrets Manager の Apache Airflow 接続文字列AWS Secrets Manager シークレットを使用した Apache Airflow 接続の設定。
アクセス許可
-
に記載されている Secrets Manager のアクセス許可AWS Secrets Manager シークレットを使用した Apache Airflow 接続の設定。
要件
Apache Airflow v2 以降でこのコード例を使用するには、追加の依存関係は必要ありません。aws-mwaa-docker-images
コードサンプル
以下の手順では、Secrets Manager を呼び出してシークレットを取得する DAG コードを作成する方法について説明します。
-
コマンドプロンプトで、DAG コードが保存されているディレクトリに移動します。例:
cd dags
-
以下のコードサンプルの内容をコピーし、ローカルに
secrets-manager.py
として保存します。""" Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from airflow import DAG, settings, secrets from airflow.operators.python import PythonOperator from airflow.utils.dates import days_ago from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook from datetime import timedelta import os ### The steps to create this secret key can be found at: https://docs.aws.amazon.com/mwaa/latest/userguide/connections-secrets-manager.html sm_secretId_name = 'airflow/connections/myconn' default_args = { 'owner': 'airflow', 'start_date': days_ago(1), 'depends_on_past': False } ### Gets the secret myconn from Secrets Manager def read_from_aws_sm_fn(**kwargs): ### set up Secrets Manager hook = AwsBaseHook(client_type='secretsmanager') client = hook.get_client_type(region_name='us-east-1') response = client.get_secret_value(SecretId=sm_secretId_name) myConnSecretString = response["SecretString"] return myConnSecretString ### 'os.path.basename(__file__).replace(".py", "")' uses the file name secrets-manager.py for a DAG ID of secrets-manager with DAG( dag_id=os.path.basename(__file__).replace(".py", ""), default_args=default_args, dagrun_timeout=timedelta(hours=2), start_date=days_ago(1), schedule_interval=None ) as dag: write_all_to_aws_sm = PythonOperator( task_id="read_from_aws_sm", python_callable=read_from_aws_sm_fn, provide_context=True )
次のステップ
-
この例の DAG コードを Amazon S3 バケットの
dags
フォルダにアップロードする方法については、DAG の追加と更新 を参照してください。