You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "vincbeck (via GitHub)" <gi...@apache.org> on 2023/02/21 19:40:22 UTC

[GitHub] [airflow] vincbeck commented on a diff in pull request #28241: Create Lambda create operator and sensor

vincbeck commented on code in PR #28241:
URL: https://github.com/apache/airflow/pull/28241#discussion_r1113495450


##########
airflow/providers/amazon/aws/operators/lambda_function.py:
##########
@@ -20,35 +20,122 @@
 import json
 from typing import TYPE_CHECKING, Sequence
 
+from airflow.compat.functools import cached_property
 from airflow.models import BaseOperator
 from airflow.providers.amazon.aws.hooks.lambda_function import LambdaHook
 
 if TYPE_CHECKING:
     from airflow.utils.context import Context
 
 
-class AwsLambdaInvokeFunctionOperator(BaseOperator):
+class LambdaCreateFunctionOperator(BaseOperator):
     """
-    Invokes an AWS Lambda function.
-    You can invoke a function synchronously (and wait for the response),
-    or asynchronously.
-    To invoke a function asynchronously,
-    set `invocation_type` to `Event`. For more details,
-    review the boto3 Lambda invoke docs.
+    Creates an AWS Lambda function.
+
+    More information regarding parameters of this operator can be found here
+    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.create_function
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:LambdaCreateFunctionOperator`
 
     :param function_name: The name of the AWS Lambda function, version, or alias.
-    :param log_type: Set to Tail to include the execution log in the response. Otherwise, set to "None".
-    :param qualifier: Specify a version or alias to invoke a published version of the function.
-    :param invocation_type: One of RequestResponse / Event / DryRun
-    :param client_context: Up to 3,583 bytes of base64-encoded data about the invoking client
-        to pass to the function in the context object.
-    :param payload: The JSON string that you want to provide to your Lambda function as input.
+    :param runtime: The identifier of the function's runtime. Runtime is required if the deployment package
+        is a .zip file archive.
+    :param role: The Amazon Resource Name (ARN) of the function's execution role.
+    :param handler: The name of the method within your code that Lambda calls to run your function.
+        Handler is required if the deployment package is a .zip file archive.
+    :param code: The code for the function.
+    :param description: A description of the function.
+    :param timeout: The amount of time (in seconds) that Lambda allows a function to run before stopping it.
+    :param config: Optional dictionary for arbitrary parameters to the boto API create_lambda call.
+    :param wait_for_completion: If True, the operator will wait until the function is active.
     :param aws_conn_id: The AWS connection ID to use
+    """
+
+    template_fields: Sequence[str] = (
+        "function_name",
+        "runtime",
+        "role",
+        "handler",
+        "code",
+        "config",
+    )
+    ui_color = "#ff7300"
+
+    def __init__(
+        self,
+        *,
+        function_name: str,
+        runtime: str | None = None,
+        role: str,
+        handler: str | None = None,
+        code: dict,
+        description: str | None = None,
+        timeout: int | None = None,
+        config: dict = {},
+        wait_for_completion: bool = False,
+        aws_conn_id: str = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.function_name = function_name
+        self.runtime = runtime
+        self.role = role
+        self.handler = handler
+        self.code = code
+        self.description = description
+        self.timeout = timeout
+        self.config = config
+        self.wait_for_completion = wait_for_completion
+        self.aws_conn_id = aws_conn_id
+
+    @cached_property
+    def hook(self) -> LambdaHook:
+        return LambdaHook(aws_conn_id=self.aws_conn_id)
+
+    def execute(self, context: Context):
+        self.log.info("Creating AWS Lambda function: %s", self.function_name)
+        response = self.hook.create_lambda(
+            function_name=self.function_name,
+            runtime=self.runtime,
+            role=self.role,
+            handler=self.handler,
+            code=self.code,
+            description=self.description,
+            timeout=self.timeout,
+            **self.config,
+        )
+        self.log.info("Lambda response: %r", response)
+
+        if self.wait_for_completion:
+            self.log.info("Wait for Lambda function to be active")
+            waiter = self.hook.conn.get_waiter("function_active_v2")
+            waiter.wait(
+                FunctionName=self.function_name,
+            )
+
+        return response.get("FunctionArn")
+
+
+class AwsLambdaInvokeFunctionOperator(BaseOperator):

Review Comment:
   You're totally correct. However, this is not related to this PR, this class existed before this PR, the diff just makes it confusing. I actually dont add `AwsLambdaInvokeFunctionOperator` but `LambdaCreateFunctionOperator`.
   
   Though, I agree with you, this should be fixed and we should rename it to have something consistent. I just created #29677, I propose a bit more than renaming `AwsLambdaInvokeFunctionOperator ` to `LambdaInvokeFunctionOperator`. Let me know what you think (feel free to comment on the issue)



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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