john-jac commented on code in PR #66046:
URL: https://github.com/apache/airflow/pull/66046#discussion_r3161942636


##########
providers/amazon/tests/system/amazon/aws/example_mwaa_serverless.py:
##########
@@ -0,0 +1,182 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from datetime import datetime
+
+from airflow.providers.amazon.aws.operators.mwaa_serverless import (
+    MwaaServerlessStartWorkflowRunOperator,
+)
+from airflow.providers.common.compat.sdk import DAG, chain
+
+from system.amazon.aws.utils import ENV_ID_KEY, SystemTestContextBuilder
+from tests_common.test_utils.version_compat import AIRFLOW_V_3_0_PLUS
+
+if AIRFLOW_V_3_0_PLUS:
+    from airflow.sdk import TriggerRule, task
+else:
+    from airflow.decorators import task  # type: ignore[attr-defined,no-redef]
+    from airflow.utils.trigger_rule import TriggerRule  # type: 
ignore[no-redef,attr-defined]
+
+DAG_ID = "example_mwaa_serverless"
+
+# Valid MWAA Serverless YAML: tasks as mapping, FQN operators, flat parameters.
+# This workflow runs an S3KeySensor that checks for the workflow YAML file 
itself.
+WORKFLOW_YAML = """\
+systest_mwaa_serverless:
+  schedule: null
+  description: "System test: S3 key sensor on workflow definition"
+  tasks:
+    check_file:
+      task_id: check_file
+      operator: airflow.providers.amazon.aws.sensors.s3.S3KeySensor
+      bucket_name: {bucket}
+      bucket_key: workflow.yaml
+"""
+
+sys_test_context_task = SystemTestContextBuilder().build()
+
+with DAG(
+    dag_id=DAG_ID,
+    schedule=None,
+    start_date=datetime(2024, 1, 1),
+    catchup=False,
+) as dag:
+    test_context = sys_test_context_task()
+    env_id = test_context[ENV_ID_KEY]
+    bucket_name = f"{env_id}-mwaa-sl"
+
+    @task
+    def create_bucket_and_workflow(bucket: str) -> str:
+        """Create S3 bucket with workflow YAML, IAM role, and MWAA Serverless 
workflow."""
+        import json
+        import time
+
+        import boto3
+
+        s3 = boto3.client("s3")
+        s3.create_bucket(Bucket=bucket)
+        s3.put_object(
+            Bucket=bucket,
+            Key="workflow.yaml",
+            Body=WORKFLOW_YAML.format(bucket=bucket).encode(),
+        )
+
+        iam = boto3.client("iam")
+        role_name = f"{bucket}-role"
+        trust = json.dumps(
+            {
+                "Version": "2012-10-17",
+                "Statement": [
+                    {
+                        "Effect": "Allow",
+                        "Principal": {"Service": 
"airflow-serverless.amazonaws.com"},
+                        "Action": "sts:AssumeRole",
+                    }
+                ],
+            }
+        )
+        try:
+            role_arn = iam.create_role(RoleName=role_name, 
AssumeRolePolicyDocument=trust)[
+                "Role"
+            ]["Arn"]
+        except iam.exceptions.EntityAlreadyExistsException:
+            role_arn = iam.get_role(RoleName=role_name)["Role"]["Arn"]
+        iam.attach_role_policy(
+            RoleName=role_name, 
PolicyArn="arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
+        )
+        iam.attach_role_policy(
+            RoleName=role_name, 
PolicyArn="arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"
+        )
+        time.sleep(10)  # Wait for IAM role propagation
+
+        mwaa = boto3.client("mwaa-serverless")
+        resp = mwaa.create_workflow(
+            Name=bucket,
+            DefinitionS3Location={"Bucket": bucket, "ObjectKey": 
"workflow.yaml"},
+            RoleArn=role_arn,
+        )
+        return resp["WorkflowArn"]
+
+    workflow_arn = create_bucket_and_workflow(bucket=bucket_name)
+
+    # [START howto_operator_mwaa_serverless_start_workflow_run]
+    start_workflow = MwaaServerlessStartWorkflowRunOperator(
+        task_id="start_workflow",
+        workflow_arn=workflow_arn,
+    )
+    # [END howto_operator_mwaa_serverless_start_workflow_run]
+
+    @task(trigger_rule=TriggerRule.ALL_DONE)
+    def stop_and_cleanup(workflow_arn: str, run_id: str, bucket: str):
+        """Stop the run, delete the workflow, clean up IAM role and S3 
bucket."""
+        import time
+
+        import boto3
+
+        mwaa = boto3.client("mwaa-serverless")
+        try:
+            mwaa.stop_workflow_run(WorkflowArn=workflow_arn, RunId=run_id)
+            time.sleep(10)  # Wait for run to stop before deleting workflow
+        except Exception:
+            pass
+        try:
+            mwaa.delete_workflow(WorkflowArn=workflow_arn)
+        except Exception:
+            pass

Review Comment:
   Great feedback @vincbeck !  Working on that now.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to