You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/07/07 16:11:24 UTC

[GitHub] [airflow] gregbrowndev commented on issue #9707: DockerOperator AWS ECR Login Unauthorised

gregbrowndev commented on issue #9707:
URL: https://github.com/apache/airflow/issues/9707#issuecomment-654968688


   Apologies, I misunderstood how the Docker Registry connection worked. 
   
   For anyone else running into this, here is a simple DAG which updates a 'docker_ecr' connection token, based on [Brian Campbell's code here](https://www.lucidchart.com/techblog/2019/03/22/using-apache-airflows-docker-operator-with-amazons-container-repository/).
   
   ```py
   import base64
   import subprocess
   
   import boto3
   from airflow import DAG
   from airflow.contrib.hooks.aws_hook import AwsHook
   from airflow.operators.python_operator import PythonOperator
   from airflow.utils.dates import days_ago
   
   
   def update_credentials():
       """Retrieves ECR access token and updates Airflow docker_ecr connection"""
       aws_hook = AwsHook("aws_credentials")
       credentials = aws_hook.get_credentials()
   
       ecr = boto3.client(
           'ecr',
           region_name='eu-west-2',
           aws_access_key_id=credentials.access_key,
           aws_secret_access_key=credentials.secret_key
       )
       response = ecr.get_authorization_token()
   
       username, password = base64.b64decode(
           response['authorizationData'][0]['authorizationToken']
       ).decode('utf-8').split(':')
   
       registry_url = response['authorizationData'][0]['proxyEndpoint']
   
       # Delete existing docker connection
       airflow_del_cmd = 'airflow connections -d --conn_id docker_ecr'
       process = subprocess.Popen(airflow_del_cmd.split(), stdout=subprocess.PIPE)
       output, error = process.communicate()
   
       # Add docker connection with updated credentials
       airflow_add_cmd = 'airflow connections -a --conn_id docker_ecr --conn_type docker --conn_host {} --conn_login {} --conn_password {}'.format(
           registry_url, username, password)
       process = subprocess.Popen(airflow_add_cmd.split(), stdout=subprocess.PIPE)
       output, error = process.communicate()
   
   
   default_args = {
       "owner": "ito",
       "description": "Fetches and stores ECR credentials to allow Docker daemon to pull images",
       "depend_on_past": False,
       "start_date": days_ago(1),
       "email_on_failure": False,
       "email_on_retry": False,
   }
   
   with DAG(
           "Update_ECR_Credentials",
           default_args=default_args,
           schedule_interval="@hourly",
           catchup=False,
   ) as dag:
       update_credentials_task = PythonOperator(
           task_id="update_credentials", python_callable=update_credentials
       )
   
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org