pre-configured airflow "Connections"
Is there a way to create docker image with pre-configured airflow "Connections"?
The airflow CLI allows adding connections, see https://airflow.incubator.apache.org/cli.html
You could add your custom connection statements (airflow connections -a...) either to the entrypoint.sh and rebuild the image, or you can inherit an image FROM puckel/docker-airflow and add a RUN statement in there.
Sorry if I misunderstood your question
@mendhak thanks for advice, with CLI we can pass only conn_uri param, but I want to create Connection based on conn_type and some extras maybe you know the proper way to build conn_uri?
I decided to insert connections directly in databse
Hey @ToGoBananas can you share sample statements you used to insert connections directly?
With the CLI I've added variables and connections like this:
sudo docker-compose exec webserver airflow variables -i variables.json
sudo docker-compose exec webserver airflow connections -a --conn_id examplessh --conn_uri ssh://user:pass@serverip --conn_extra '{"key_file":"/usr/local/airflow/id_rsa", "no_host_key_check":true}'
sudo docker-compose exec webserver airflow connections -a --conn_id examples3 --conn_uri s3://my-test-bucket --conn_extra '{"aws_access_key_id":"AAAAAAA", "aws_secret_access_key":"bbbbbbb", "bucket_name":"mybucket"}'
@mendhak do you know how to add a connection for google cloud with CLI directly?
Based on the types listed here and the code here it seems like everything needs to go into extras but the UI shows it in a different form through some behind-the-scenes magic.
I think you should try
sudo docker-compose exec webserver airflow connections -a --conn_id googletest --conn_uri google_cloud_platform://dummyvalue --conn_extra '{ 'key_path':'....','scope':'....' }'
Main thing is getting the schema right (google_cloud_platform://) and after that just a matter of putting in the rest of the values for GCP into extras
You may have to experiment with this a few times.
@mendhak Thank you the instruction. I will give it a try!
@mendhak I have inspected the source code. It seems that airflow connections has a bug about the feature to parse --conn_uri. I have reported that to airflow community. Thank you for your help.
https://issues.apache.org/jira/browse/AIRFLOW-1330
update for anyone coming across this thread:
Add to entrypoint.sh after initdb:
airflow connections --add --conn_id=gcp --conn_type=google_cloud_platform --conn_extra='{ "extra__google_cloud_platform__key_path":"/usr/local/airflow/secrets/gcp_key.json", "extra__google_cloud_platform__project": "gcp-project", "extra__google_cloud_platform__scope": "https://www.googleapis.com/auth/cloud-platform"}'
You can also add connections from python using the airflow libraries. I run this from entrypoint.sh:
https://gist.github.com/jnalley/80cbfbf2619c848e4d1d1efeec0c836c
@jnalley Can you please share your entrypoint.sh file as well.
I am following the same approach but not sure how do I execute setup_connection.py from entrypoint.sh file.
Thank you!
@aicdp1010 I added the entrypoint.sh I was using here:
https://gist.github.com/jnalley/3831d3104cb008adceb86b996bd499c0
My Dockerfile also has:
COPY scripts /usr/local/airflow/
So, I just put all the .py scripts under the scripts directory (along with entrypoint.sh).
In 2022 to add a connection to the local docker registry
airflow connections add 'docker_default' \
--conn-type 'docker' \
--conn-login 'root' \
--conn-host '127.0.0.1' \
--conn-port '5000'