def _create_sagemaker_model(sagemaker_client, sagemaker_model_name, ecr_image_path, spec): execution_role_arn = get_arn_role_from_current_aws_user() sagemaker_model_info = { "ModelName": sagemaker_model_name, "PrimaryContainer": { "ContainerHostname": sagemaker_model_name, "Image": ecr_image_path, "Environment": { "API_NAME": spec.api_name, "BENTOML_GUNICORN_TIMEOUT": str(spec.timeout), }, }, "ExecutionRoleArn": execution_role_arn, } # Will set envvar, if user defined gunicorn workers per instance. EnvVar needs # to be string instead of the int. if spec.num_of_gunicorn_workers_per_instance: sagemaker_model_info["PrimaryContainer"]["Environment"][ "BENTOML_GUNICORN_NUM_OF_WORKERS"] = str( spec.num_of_gunicorn_workers_per_instance) try: create_model_response = sagemaker_client.create_model( **sagemaker_model_info) except ClientError as e: raise generate_bentoml_exception_from_aws_client_error( e, "Failed to create sagemaker model") logger.debug("AWS create model response: %s", create_model_response)
def test_generate_bentoml_exception_from_aws_client_error(): error = ClientError( error_response={ 'Error': {'Code': 'ValidationException', 'Message': 'error message'} }, operation_name='failed_operation', ) exception = generate_bentoml_exception_from_aws_client_error(error) assert isinstance(exception, AWSServiceError)
def _update_sagemaker_endpoint(sagemaker_client, endpoint_name, endpoint_config_name): try: logger.debug("Updating sagemaker endpoint %s", endpoint_name) update_endpoint_response = sagemaker_client.update_endpoint( EndpointName=endpoint_name, EndpointConfigName=endpoint_config_name) logger.debug("AWS update endpoint response: %s", str(update_endpoint_response)) except ClientError as e: raise generate_bentoml_exception_from_aws_client_error( e, "Failed to update sagemaker endpoint")
def _create_sagemaker_endpoint_config(sagemaker_client, sagemaker_model_name, endpoint_config_name, sagemaker_config): create_endpoint_config_arguments = { "EndpointConfigName": endpoint_config_name, "ProductionVariants": [{ "VariantName": sagemaker_model_name, "ModelName": sagemaker_model_name, "InitialInstanceCount": sagemaker_config.instance_count, "InstanceType": sagemaker_config.instance_type, }], } if sagemaker_config.data_capture_s3_prefix: logger.debug( "data_capture_s3_prefix %s found, creating data capture config", sagemaker_config.data_capture_s3_prefix, ) if not sagemaker_config.data_capture_sample_percent: logger.debug( "data_capture_sample_percent not found, using default 100%") sagemaker_config.data_capture_sample_percent = 100 create_endpoint_config_arguments["DataCaptureConfig"] = { "EnableCapture": True, "DestinationS3Uri": sagemaker_config.data_capture_s3_prefix, "InitialSamplingPercentage": sagemaker_config.data_capture_sample_percent, "CaptureOptions": [{ "CaptureMode": "Input" }, { "CaptureMode": "Output" }], } logger.debug("Creating Sagemaker endpoint %s configuration", endpoint_config_name) try: create_config_response = sagemaker_client.create_endpoint_config( **create_endpoint_config_arguments) except ClientError as e: raise generate_bentoml_exception_from_aws_client_error( e, "Failed to create sagemaker endpoint config") logger.debug("AWS create endpoint config response: %s", create_config_response)
def _delete_sagemaker_model_if_exist(sagemaker_client, sagemaker_model_name): try: delete_model_response = sagemaker_client.delete_model( ModelName=sagemaker_model_name) logger.debug("AWS delete model response: %s", delete_model_response) except ClientError as e: error_response = e.response.get("Error", {}) error_code = error_response.get("Code", "Unknown") error_message = error_response.get("Message", "Unknown") if (error_code == "ValidationException" and "Could not find model" in error_message): # sagemaker model does not exist return raise generate_bentoml_exception_from_aws_client_error( e, f"Failed to cleanup sagemaker model '{sagemaker_model_name}'") return
def _delete_sagemaker_endpoint_if_exist(sagemaker_client, sagemaker_endpoint_name): try: delete_endpoint_response = sagemaker_client.delete_endpoint( EndpointName=sagemaker_endpoint_name) logger.debug("AWS delete endpoint response: %s", delete_endpoint_response) except ClientError as e: error_response = e.response.get("Error", {}) error_code = error_response.get("Code", "Unknown") error_message = error_response.get("Message", "Unknown") if (error_code == "ValidationException" and "Could not find endpoint" in error_message): # sagemaker endpoint does not exist return raise generate_bentoml_exception_from_aws_client_error( e, f"Failed to delete sagemaker endpoint '{sagemaker_endpoint_name}'")
def _delete_sagemaker_endpoint_config_if_exist(sagemaker_client, sagemaker_endpoint_config_name): try: delete_endpoint_config_response = sagemaker_client.delete_endpoint_config( EndpointConfigName=sagemaker_endpoint_config_name) logger.debug("AWS delete endpoint config response: %s", delete_endpoint_config_response) except ClientError as e: error_response = e.response.get("Error", {}) error_code = error_response.get("Code", "Unknown") error_message = error_response.get("Message", "Unknown") if (error_code == "ValidationException" and "Could not find endpoint configuration" in error_message): # endpoint config does not exist return raise generate_bentoml_exception_from_aws_client_error( e, f"Failed to cleanup sagemaker endpoint config " f"'{sagemaker_endpoint_config_name}' after creation failed", ) return