Source code for airflow.providers.amazon.aws.example_dags.example_sagemaker_endpoint
# 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.importjsonimportosfromdatetimeimportdatetimeimportboto3fromairflowimportDAGfromairflow.decoratorsimporttaskfromairflow.providers.amazon.aws.operators.s3importS3CreateObjectOperatorfromairflow.providers.amazon.aws.operators.sagemakerimport(SageMakerDeleteModelOperator,SageMakerEndpointConfigOperator,SageMakerEndpointOperator,SageMakerModelOperator,SageMakerTrainingOperator,)fromairflow.providers.amazon.aws.sensors.sagemakerimportSageMakerEndpointSensor# Project name will be used in naming the S3 buckets and various tasks.# The dataset used in this example is identifying varieties of the Iris flower.
# 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
# The URI of an Amazon-provided docker image for handling KNN model training. This is a public ECR# repo cited in public SageMaker documentation, so the account number does not need to be redacted.# For more info see: https://docs.aws.amazon.com/sagemaker/latest/dg/ecr-us-west-2.html#knn-us-west-2.title
)train_model=SageMakerTrainingOperator(task_id='train_model',config=TRAINING_CONFIG,do_xcom_push=False,)create_model=SageMakerModelOperator(task_id='create_model',config=MODEL_CONFIG,do_xcom_push=False,)# [START howto_operator_sagemaker_endpoint_config]configure_endpoint=SageMakerEndpointConfigOperator(task_id='configure_endpoint',config=ENDPOINT_CONFIG_CONFIG,do_xcom_push=False,)# [END howto_operator_sagemaker_endpoint_config]# [START howto_operator_sagemaker_endpoint]deploy_endpoint=SageMakerEndpointOperator(task_id='deploy_endpoint',config=DEPLOY_ENDPOINT_CONFIG,# Waits by default, <setting as False to demonstrate the Sensor below.wait_for_completion=False,do_xcom_push=False,)# [END howto_operator_sagemaker_endpoint]# [START howto_sensor_sagemaker_endpoint]await_endpoint=SageMakerEndpointSensor(task_id='await_endpoint',endpoint_name=ENDPOINT_NAME,do_xcom_push=False,)# [END howto_sensor_sagemaker_endpoint]# Trigger rule set to "all_done" so clean up will run regardless of success on other tasks.delete_model=SageMakerDeleteModelOperator(task_id='delete_model',config={'ModelName':MODEL_NAME},trigger_rule='all_done',)(upload_data>>train_model>>create_model>>configure_endpoint>>deploy_endpoint>>await_endpoint>>call_endpoint()>>cleanup()>>delete_model)