apache-airflow-providers-amazon 9.8.0__py3-none-any.whl → 9.9.0rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- airflow/providers/amazon/__init__.py +1 -1
- airflow/providers/amazon/aws/executors/aws_lambda/__init__.py +21 -0
- airflow/providers/amazon/aws/executors/aws_lambda/docker/Dockerfile +107 -0
- airflow/providers/amazon/aws/executors/aws_lambda/docker/__init__.py +16 -0
- airflow/providers/amazon/aws/executors/aws_lambda/docker/app.py +129 -0
- airflow/providers/amazon/aws/executors/aws_lambda/lambda_executor.py +479 -0
- airflow/providers/amazon/aws/executors/aws_lambda/utils.py +70 -0
- airflow/providers/amazon/aws/executors/ecs/ecs_executor.py +1 -1
- airflow/providers/amazon/aws/executors/ecs/ecs_executor_config.py +4 -8
- airflow/providers/amazon/aws/hooks/base_aws.py +20 -4
- airflow/providers/amazon/aws/hooks/eks.py +14 -5
- airflow/providers/amazon/aws/hooks/s3.py +101 -34
- airflow/providers/amazon/aws/hooks/sns.py +10 -1
- airflow/providers/amazon/aws/log/cloudwatch_task_handler.py +12 -5
- airflow/providers/amazon/aws/operators/batch.py +1 -2
- airflow/providers/amazon/aws/operators/cloud_formation.py +0 -2
- airflow/providers/amazon/aws/operators/comprehend.py +0 -2
- airflow/providers/amazon/aws/operators/dms.py +0 -2
- airflow/providers/amazon/aws/operators/ecs.py +1 -1
- airflow/providers/amazon/aws/operators/eks.py +13 -0
- airflow/providers/amazon/aws/operators/emr.py +4 -4
- airflow/providers/amazon/aws/operators/glue.py +0 -6
- airflow/providers/amazon/aws/operators/rds.py +0 -4
- airflow/providers/amazon/aws/operators/redshift_cluster.py +90 -63
- airflow/providers/amazon/aws/operators/sns.py +15 -1
- airflow/providers/amazon/aws/sensors/redshift_cluster.py +13 -10
- airflow/providers/amazon/get_provider_info.py +68 -0
- {apache_airflow_providers_amazon-9.8.0.dist-info → apache_airflow_providers_amazon-9.9.0rc1.dist-info}/METADATA +21 -25
- {apache_airflow_providers_amazon-9.8.0.dist-info → apache_airflow_providers_amazon-9.9.0rc1.dist-info}/RECORD +31 -25
- {apache_airflow_providers_amazon-9.8.0.dist-info → apache_airflow_providers_amazon-9.9.0rc1.dist-info}/WHEEL +0 -0
- {apache_airflow_providers_amazon-9.8.0.dist-info → apache_airflow_providers_amazon-9.9.0rc1.dist-info}/entry_points.txt +0 -0
@@ -29,7 +29,7 @@ from airflow import __version__ as airflow_version
|
|
29
29
|
|
30
30
|
__all__ = ["__version__"]
|
31
31
|
|
32
|
-
__version__ = "9.
|
32
|
+
__version__ = "9.9.0"
|
33
33
|
|
34
34
|
if packaging.version.parse(packaging.version.parse(airflow_version).base_version) < packaging.version.parse(
|
35
35
|
"2.10.0"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
2
|
+
# or more contributor license agreements. See the NOTICE file
|
3
|
+
# distributed with this work for additional information
|
4
|
+
# regarding copyright ownership. The ASF licenses this file
|
5
|
+
# to you under the Apache License, Version 2.0 (the
|
6
|
+
# "License"); you may not use this file except in compliance
|
7
|
+
# with the License. You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
from __future__ import annotations
|
18
|
+
|
19
|
+
__all__ = ["AwsLambdaExecutor"]
|
20
|
+
|
21
|
+
from airflow.providers.amazon.aws.executors.aws_lambda.lambda_executor import AwsLambdaExecutor
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
2
|
+
# or more contributor license agreements. See the NOTICE file
|
3
|
+
# distributed with this work for additional information
|
4
|
+
# regarding copyright ownership. The ASF licenses this file
|
5
|
+
# to you under the Apache License, Version 2.0 (the
|
6
|
+
# "License"); you may not use this file except in compliance
|
7
|
+
# with the License. You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
# Use the official AWS Lambda Python base image.
|
19
|
+
# If you wish to use a different Python version, please update the line below
|
20
|
+
FROM public.ecr.aws/lambda/python:3.12
|
21
|
+
|
22
|
+
# hadolint ignore=DL3041
|
23
|
+
RUN dnf -y install unzip \
|
24
|
+
&& dnf clean all \
|
25
|
+
&& rm -rf /var/cache/dnf
|
26
|
+
|
27
|
+
# Install the AWS CLI.
|
28
|
+
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
|
29
|
+
unzip awscliv2.zip \
|
30
|
+
&& ./aws/install \
|
31
|
+
&& rm -rf ./aws awscliv2.zip
|
32
|
+
|
33
|
+
## Install Airflow and dependencies.
|
34
|
+
# The most current version of Airflow is installed by default, along with the amazon and postgres
|
35
|
+
# provider packages.
|
36
|
+
# If you would like to install a specific version, you can use the requirements.txt to change the
|
37
|
+
# version along with installing your dependencies or update this Dockerfile to install a specific
|
38
|
+
# version of Airflow.
|
39
|
+
|
40
|
+
# NOTE: If you change the below line, specifically removing the amazon extra, please ensure boto3 is
|
41
|
+
# installed via another method. Boto3 is required for the Lambda executor to function properly.
|
42
|
+
# hadolint ignore=SC2102
|
43
|
+
RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir apache-airflow[amazon,postgres]
|
44
|
+
# /tmp is the only writable directory in Lambda, so we need to set the AIRFLOW_HOME there.
|
45
|
+
ENV AIRFLOW_HOME=/tmp/airflow
|
46
|
+
# Dags are read-only, so they can be stored in opt (or another path provided).
|
47
|
+
ARG container_dag_path=/opt/airflow/dags
|
48
|
+
ENV AIRFLOW__CORE__DAGS_FOLDER=$container_dag_path
|
49
|
+
RUN mkdir -p /opt/airflow/dags
|
50
|
+
|
51
|
+
# Python dependencies can be installed by providing a requirements.txt.
|
52
|
+
# If the file is in a different location, use the requirements_path build argument to specify
|
53
|
+
# the file path.
|
54
|
+
ARG requirements_path=./requirements.txt
|
55
|
+
ENV REQUIREMENTS_PATH=$requirements_path
|
56
|
+
# Uncomment the two lines below to copy the requirements.txt file to the container, and
|
57
|
+
# install the dependencies.
|
58
|
+
# COPY --chown=airflow:root $REQUIREMENTS_PATH /opt/airflow/requirements.txt
|
59
|
+
# RUN pip install --no-cache-dir -r /opt/airflow/requirements.txt
|
60
|
+
|
61
|
+
## AWS Authentication
|
62
|
+
# The image requires access to AWS services. This Dockerfile supports 2 ways to authenticate with AWS.
|
63
|
+
# The first is using build arguments where you can provide the AWS credentials as arguments
|
64
|
+
# passed when building the image. The other option is to leverage the Lambda execution role. Airflow
|
65
|
+
# will default to using Boto credential strategy which will look for roles from Lambda, this is the
|
66
|
+
# preferred approach. See the Lambda Executor Airflow documentation for more details.
|
67
|
+
|
68
|
+
# If you would like to use an alternative method of authentication, feel free to make the
|
69
|
+
# necessary changes to this file.
|
70
|
+
|
71
|
+
# Uncomment to use these arguments to provide AWS authentication information if not using the Lambda
|
72
|
+
# execution role.
|
73
|
+
#ARG aws_access_key_id
|
74
|
+
#ARG aws_secret_access_key
|
75
|
+
#ARG aws_default_region
|
76
|
+
#ARG aws_session_token
|
77
|
+
|
78
|
+
#ENV AWS_ACCESS_KEY_ID=$aws_access_key_id
|
79
|
+
#ENV AWS_SECRET_ACCESS_KEY=$aws_secret_access_key
|
80
|
+
#ENV AWS_DEFAULT_REGION=$aws_default_region
|
81
|
+
#ENV AWS_SESSION_TOKEN=$aws_session_token
|
82
|
+
|
83
|
+
## Loading DAGs
|
84
|
+
# This Dockerfile supports 2 ways to load DAGs onto the container.
|
85
|
+
# One is to download them from S3 at runtime during the Lambda app invocation. The other
|
86
|
+
# is to copy the dags into the image at build time, this will make task execution
|
87
|
+
# much faster, since the images will already be present but the image will need to be rebuilt
|
88
|
+
# every time the DAGs are updated.
|
89
|
+
# If you would like to use an alternative method of loading DAGs, feel free to make the
|
90
|
+
# necessary changes to this file.
|
91
|
+
|
92
|
+
ARG host_dag_path=./dags
|
93
|
+
ENV HOST_DAG_PATH=$host_dag_path
|
94
|
+
# Uncomment the line below to copy the DAGs from the host to the container.
|
95
|
+
# COPY $HOST_DAG_PATH $AIRFLOW__CORE__DAGS_FOLDER
|
96
|
+
|
97
|
+
# Use these arguments to load DAGs at runtime. If you are using the provided Lambda function (app.py),
|
98
|
+
# it will check for this environment variable and download the DAGs from S3. See the example app in Lambda
|
99
|
+
# Executor documentation for details.
|
100
|
+
ARG s3_uri
|
101
|
+
ENV S3_URI=$s3_uri
|
102
|
+
|
103
|
+
# Copy your Lambda function code into the Docker build directory/context.
|
104
|
+
COPY app.py ${LAMBDA_TASK_ROOT}/
|
105
|
+
|
106
|
+
# Specify the Lambda function handler (update if you provide a different handler with a different name).
|
107
|
+
CMD ["app.lambda_handler"]
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
2
|
+
# or more contributor license agreements. See the NOTICE file
|
3
|
+
# distributed with this work for additional information
|
4
|
+
# regarding copyright ownership. The ASF licenses this file
|
5
|
+
# to you under the Apache License, Version 2.0 (the
|
6
|
+
# "License"); you may not use this file except in compliance
|
7
|
+
# with the License. You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
@@ -0,0 +1,129 @@
|
|
1
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
2
|
+
# or more contributor license agreements. See the NOTICE file
|
3
|
+
# distributed with this work for additional information
|
4
|
+
# regarding copyright ownership. The ASF licenses this file
|
5
|
+
# to you under the Apache License, Version 2.0 (the
|
6
|
+
# "License"); you may not use this file except in compliance
|
7
|
+
# with the License. You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
from __future__ import annotations
|
18
|
+
|
19
|
+
import json
|
20
|
+
import logging
|
21
|
+
import os
|
22
|
+
import subprocess
|
23
|
+
from tempfile import mkdtemp
|
24
|
+
|
25
|
+
import boto3
|
26
|
+
|
27
|
+
"""
|
28
|
+
Example Lambda function to execute an Airflow command or workload. Use or modify this code as needed.
|
29
|
+
"""
|
30
|
+
|
31
|
+
log = logging.getLogger()
|
32
|
+
log.setLevel(logging.INFO)
|
33
|
+
|
34
|
+
|
35
|
+
# Get the S3 URI from the environment variable. Set either on the Lambda function or in the
|
36
|
+
# docker image used for the lambda invocations.
|
37
|
+
S3_URI = os.environ.get("S3_URI", None)
|
38
|
+
# Input and output keys
|
39
|
+
TASK_KEY_KEY = "task_key"
|
40
|
+
COMMAND_KEY = "command"
|
41
|
+
RETURN_CODE_KEY = "return_code"
|
42
|
+
|
43
|
+
|
44
|
+
def lambda_handler(event, context):
|
45
|
+
log.info("Received event: %s", event)
|
46
|
+
log.info("Received context: %s", context)
|
47
|
+
|
48
|
+
command = event.get(COMMAND_KEY)
|
49
|
+
task_key = event.get(TASK_KEY_KEY)
|
50
|
+
|
51
|
+
# Any pre-processing or validation of the command or use of the context can be done here or above.
|
52
|
+
|
53
|
+
# Sync dags from s3 to the local dags directory
|
54
|
+
if S3_URI:
|
55
|
+
fetch_dags_from_s3(S3_URI)
|
56
|
+
# This function must be called, it executes the Airflow command and reports to SQS.
|
57
|
+
run_and_report(command, task_key)
|
58
|
+
|
59
|
+
# Any post-processing or cleanup can be done here.
|
60
|
+
|
61
|
+
|
62
|
+
def run_and_report(command, task_key):
|
63
|
+
"""Execute the provided Airflow command or workload and report the result via SQS."""
|
64
|
+
try:
|
65
|
+
log.info("Starting execution for task: %s", task_key)
|
66
|
+
result = subprocess.run(
|
67
|
+
command, shell=isinstance(command, str), stdout=subprocess.PIPE, stderr=subprocess.STDOUT
|
68
|
+
)
|
69
|
+
return_code = result.returncode
|
70
|
+
log.info("Execution completed for task %s with return code %s", task_key, return_code)
|
71
|
+
log.info("Output:")
|
72
|
+
log.info("%s", result.stdout.decode())
|
73
|
+
except Exception:
|
74
|
+
log.exception("Error executing task %s: ", task_key)
|
75
|
+
return_code = 1 # Non-zero indicates failure to run the task
|
76
|
+
|
77
|
+
queue_url = get_queue_url()
|
78
|
+
message = json.dumps({TASK_KEY_KEY: task_key, RETURN_CODE_KEY: return_code})
|
79
|
+
try:
|
80
|
+
sqs_client = get_sqs_client()
|
81
|
+
sqs_client.send_message(QueueUrl=queue_url, MessageBody=message)
|
82
|
+
log.info("Sent result to SQS %s", message)
|
83
|
+
except Exception:
|
84
|
+
log.exception("Failed to send message to SQS for task %s", task_key)
|
85
|
+
|
86
|
+
|
87
|
+
def get_sqs_client():
|
88
|
+
"""Create an SQS client. Credentials and region are automatically picked up from the environment."""
|
89
|
+
return boto3.client("sqs")
|
90
|
+
|
91
|
+
|
92
|
+
def get_queue_url():
|
93
|
+
"""
|
94
|
+
Get the SQS queue URL from the environment variable.
|
95
|
+
|
96
|
+
Set either on the Lambda function or in the image used for the lambda invocations.
|
97
|
+
"""
|
98
|
+
queue_url = os.environ.get("AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL", os.environ.get("QUEUE_URL", None))
|
99
|
+
if not queue_url:
|
100
|
+
raise RuntimeError(
|
101
|
+
"No Queue URL detected (either AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL or "
|
102
|
+
"QUEUE_URL); Will be unable to send task results. Exiting!"
|
103
|
+
)
|
104
|
+
return queue_url
|
105
|
+
|
106
|
+
|
107
|
+
def fetch_dags_from_s3(s3_uri):
|
108
|
+
"""Fetch DAGs from S3 and sync them to the local dags directory."""
|
109
|
+
log.info("Fetching DAGs from S3 URI: %s", s3_uri)
|
110
|
+
# Use a named temporary directory for the local dags folder, only tmp is writeable in Lambda
|
111
|
+
local_dags_dir = mkdtemp(prefix="airflow_dags_")
|
112
|
+
log.info("Setting AIRFLOW__CORE__DAGS_FOLDER to: %s", local_dags_dir)
|
113
|
+
os.environ["AIRFLOW__CORE__DAGS_FOLDER"] = local_dags_dir
|
114
|
+
|
115
|
+
# S3 URI format s3://bucket-name/path/to/dags/
|
116
|
+
bucket_name = s3_uri.split("/")[2]
|
117
|
+
prefix = "/".join(s3_uri.split("/")[3:])
|
118
|
+
|
119
|
+
s3_resource = boto3.resource("s3")
|
120
|
+
bucket = s3_resource.Bucket(bucket_name)
|
121
|
+
|
122
|
+
for obj in bucket.objects.filter(Prefix=prefix):
|
123
|
+
if obj.key.endswith("/"):
|
124
|
+
# Skip directories
|
125
|
+
continue
|
126
|
+
key = obj.key
|
127
|
+
local_path = os.path.join(local_dags_dir, os.path.basename(key))
|
128
|
+
log.info("Downloading %s to %s", key, local_path)
|
129
|
+
bucket.download_file(key, local_path)
|