You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/07/19 02:06:57 UTC

[GitHub] [airflow] josh-fell commented on a diff in pull request #25134: Sagemaker System Tests - Part 3 of 3 - example_sagemaker_endpoint.py (AIP-47)

josh-fell commented on code in PR #25134:
URL: https://github.com/apache/airflow/pull/25134#discussion_r923995797


##########
tests/system/providers/amazon/aws/example_sagemaker_endpoint.py:
##########
@@ -0,0 +1,292 @@
+# 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.
+import json
+from datetime import datetime
+
+import boto3
+
+from airflow import DAG
+from airflow.decorators import task
+from airflow.models.baseoperator import chain
+from airflow.operators.python import get_current_context
+from airflow.providers.amazon.aws.operators.s3 import (
+    S3CreateBucketOperator,
+    S3CreateObjectOperator,
+    S3DeleteBucketOperator,
+)
+from airflow.providers.amazon.aws.operators.sagemaker import (
+    SageMakerDeleteModelOperator,
+    SageMakerEndpointConfigOperator,
+    SageMakerEndpointOperator,
+    SageMakerModelOperator,
+    SageMakerTrainingOperator,
+)
+from airflow.providers.amazon.aws.sensors.sagemaker import SageMakerEndpointSensor
+from airflow.utils.trigger_rule import TriggerRule
+from tests.system.providers.amazon.aws.utils import ENV_ID_KEY, SystemTestContextBuilder, purge_logs
+
+DAG_ID = 'example_sagemaker_endpoint'
+
+# Externally fetched variables:
+ROLE_ARN_KEY = 'ROLE_ARN'
+# The URI of a Docker image for handling KNN model training.
+# To find the URI of a free Amazon-provided image that can be used, substitute your
+# desired region in the following link and find the URI under "Registry Path".
+# https://docs.aws.amazon.com/sagemaker/latest/dg/ecr-us-east-1.html#knn-us-east-1.title
+# This URI should be in the format of {12-digits}.dkr.ecr.{region}.amazonaws.com/knn
+KNN_IMAGE_URI_KEY = 'KNN_IMAGE_URI'
+
+sys_test_context_task = (
+    SystemTestContextBuilder().add_variable(KNN_IMAGE_URI_KEY).add_variable(ROLE_ARN_KEY).build()
+)
+
+# For an example of how to obtain the following train and test data, please see
+# https://github.com/apache/airflow/blob/main/airflow/providers/amazon/aws/example_dags/example_sagemaker.py
+TRAIN_DATA = '0,4.9,2.5,4.5,1.7\n1,7.0,3.2,4.7,1.4\n0,7.3,2.9,6.3,1.8\n2,5.1,3.5,1.4,0.2\n'
+SAMPLE_TEST_DATA = '6.4,3.2,4.5,1.5'
+
+
+@task
+def call_endpoint(endpoint_name):
+    response = (
+        boto3.Session()
+        .client('sagemaker-runtime')
+        .invoke_endpoint(
+            EndpointName=endpoint_name,
+            ContentType='text/csv',
+            Body=SAMPLE_TEST_DATA,
+        )
+    )
+
+    return json.loads(response["Body"].read().decode())['predictions']
+
+
+@task(trigger_rule=TriggerRule.ALL_DONE)
+def delete_endpoint_config(endpoint_config_job_name):
+    boto3.client('sagemaker').delete_endpoint_config(EndpointConfigName=endpoint_config_job_name)
+
+
+@task(trigger_rule=TriggerRule.ALL_DONE)
+def delete_endpoint(endpoint_name):
+    boto3.client('sagemaker').delete_endpoint(EndpointName=endpoint_name)
+
+
+@task(trigger_rule=TriggerRule.ALL_DONE)
+def delete_logs(env_id, endpoint_name):
+    generated_logs = [
+        # Format: ('log group name', 'log stream prefix')
+        ('/aws/sagemaker/TrainingJobs', env_id),
+        (f'/aws/sagemaker/Endpoints/{endpoint_name}', env_id),
+    ]
+
+    purge_logs(generated_logs)
+
+
+@task
+def set_up(env_id, knn_image_uri, role_arn):

Review Comment:
   ```suggestion
   def set_up(env_id, knn_image_uri, role_arn,  ti=None):
   ```
   Then you don't have to use `get_current_context()` to get the `TaskInstance` context object since it's passed to tasks behind the scenes and you can remove line 173. The `get_current_context()` function [should ideally be used to access context objects deep in the function stack](https://airflow.apache.org/docs/apache-airflow/stable/tutorial_taskflow_api.html#accessing-context-variables-in-decorated-tasks) so you don't have to pass them around.



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