Last active
August 22, 2022 14:41
-
-
Save aspain/b36d291d6b6829aa1b353b6118a50496 to your computer and use it in GitHub Desktop.
Airflow Connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Accessing a connection named "mys3bucket" from either a secrets backend or the Airflow backend: | |
# If using a secrets backend with connections prefix configured as: | |
# AIRFLOW__SECRETS__BACKEND_KWARGS={"connections_prefix":"airflow/connections"...}, this connection would be saved in | |
# param store as /airflow/connections/mys3bucket: | |
get_tagging = S3GetBucketTaggingOperator( | |
task_id='s3_get_bucket_tagging', | |
bucket_name='adam-cs-astrocloud', | |
aws_conn_id='mys3bucket' | |
) | |
# Accessing variables saved either in a secrets backend or directly in the Airflow backend | |
# If using a secrets backend with variables prefix configured as: | |
# AIRFLOW__SECRETS__BACKEND_KWARGS={"variables_prefix":"airflow/variables"...}, | |
# these variables would be saved as "airflow/variables/secretawsaccesskeyid" and "airflow/variables/secretawssecretaccesskey": | |
def lambda_boto3(): | |
lambda_region = "us-east-1" | |
lambda_name = "adam_aws_lambda" | |
lambda_client = boto3.client( | |
"lambda", | |
region_name=lambda_region, | |
aws_access_key_id=Variable.get("secretawsaccesskeyid"), | |
aws_secret_access_key=Variable.get("secretawssecretaccesskey"), | |
) | |
response = lambda_client.invoke( | |
FunctionName=lambda_name, InvocationType="Event", Payload=lambda_payload | |
) | |
print("Response--->", response) | |
# Accessing a connection named "mylambdaconnid" from either a secrets backend or the Airflow backend: | |
# If using a secrets backend with connections prefix configured as: | |
# AIRFLOW__SECRETS__BACKEND_KWARGS={"connections_prefix":"airflow/connections"...}, this connection would be saved in | |
# param store as /airflow/connections/mylambdaconnid: | |
def lamba_hook_func(): | |
hook = LambdaHook(aws_conn_id="mylambdaconnid") | |
response = hook.invoke_lambda( | |
function_name="adam_aws_lambda", | |
log_type="None", | |
qualifier="$LATEST", | |
invocation_type="RequestResponse", | |
payload=lambda_payload, | |
) | |
print("Response--->", response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment