简体   繁体   English

在 DAG 中使用 boto3 时,Apache airflow 无法找到 AWS 凭证

[英]Apache airflow unable to locate AWS credentials when using boto3 inside a DAG

Running an instance of Airflow on ECS Fargate.在 ECS Fargate 上运行 Airflow 的实例。 The problem is I cannot run the code to call an existing Glue Job within the DAG.问题是我无法运行代码来调用 DAG 中的现有 Glue 作业。 Below is the DAG script.下面是 DAG 脚本。

import boto3
import os
import logging
import time
import sys
import botocore
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.operators.bash_operator import BashOperator
from airflow.providers.amazon.aws.sensors.s3_key import S3KeySensor

default_args = {
    'owner': 'test',
    'start_date': datetime(2021, 1, 4),
    'depends_on_past': False,
    'provide_context': True
}

dag = DAG('run_glue_job',
          description='Executes Glue job.',
          schedule_interval=None,
          catchup=False,
          default_args=default_args)

def task_1(**kwargs):
    print('recieved trigger')

    glue = boto3.client('glue', 'us-east-1')
    response = glue.start_job_run(JobName='airflow-dev-job')
    print(response['JobRunId'])

    return response['JobRunId']

def task_2(**kwargs):
    print('send email message')

    return 'passed' 

t1 = PythonOperator(
    task_id = 'execute_glue_job',
    dag = dag,
    python_callable = task_2
)

t2 = PythonOperator(
    task_id = 'send_email_notification',
    dag = dag,
    python_callable = task_3
)

t1 >> t2

I am returning a credential error.我正在返回凭据错误。

File "/usr/local/airflow/.local/lib/python3.7/site-packages/botocore/auth.py", line 357, in add_auth
    raise NoCredentialsError
botocore.exceptions.NoCredentialsError: Unable to locate credentials

I tried setting the task_role_arn in the aws_default under the connections;我尝试在连接下的 aws_default 中设置 task_role_arn; however, I am still not able to get around the issue.但是,我仍然无法解决这个问题。

I was able to solve the problem by adding the correct task role in the task definition in ECS.我能够通过在 ECS 的任务定义中添加正确的任务角色来解决问题。

Make sure the task role assigned has all policies attached for services that you are trying to access/run via Airflow.确保分配的任务角色为您尝试通过 Airflow 访问/运行的服务附加了所有策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM