Пример #1
0
    def _add(self, deployment_pb, bento_pb, bento_path):
        if loader._is_remote_path(bento_path):
            with loader._resolve_remote_bundle_path(bento_path) as local_path:
                return self._add(deployment_pb, bento_pb, local_path)

        deployment_spec = deployment_pb.spec
        sagemaker_config = deployment_spec.sagemaker_operator_config

        raise_if_api_names_not_found_in_bento_service_metadata(
            bento_pb.bento.bento_service_metadata, [sagemaker_config.api_name])

        sagemaker_client = boto3.client('sagemaker', sagemaker_config.region)

        with TempDirectory() as temp_dir:
            sagemaker_project_dir = os.path.join(temp_dir,
                                                 deployment_spec.bento_name)
            _init_sagemaker_project(
                sagemaker_project_dir,
                bento_path,
                bento_pb.bento.bento_service_metadata.env.docker_base_image,
            )
            ecr_image_path = create_and_push_docker_image_to_ecr(
                sagemaker_config.region,
                deployment_spec.bento_name,
                deployment_spec.bento_version,
                sagemaker_project_dir,
            )

        try:
            (
                sagemaker_model_name,
                sagemaker_endpoint_config_name,
                sagemaker_endpoint_name,
            ) = _get_sagemaker_resource_names(deployment_pb)

            _create_sagemaker_model(sagemaker_client, sagemaker_model_name,
                                    ecr_image_path, sagemaker_config)
            _create_sagemaker_endpoint_config(
                sagemaker_client,
                sagemaker_model_name,
                sagemaker_endpoint_config_name,
                sagemaker_config,
            )
            _create_sagemaker_endpoint(
                sagemaker_client,
                sagemaker_endpoint_name,
                sagemaker_endpoint_config_name,
            )
        except AWSServiceError as e:
            delete_sagemaker_deployment_resources_if_exist(deployment_pb)
            raise e

        return ApplyDeploymentResponse(status=Status.OK(),
                                       deployment=deployment_pb)
Пример #2
0
    def _add(self, deployment_pb, bento_pb, bento_path):
        if loader._is_remote_path(bento_path):
            with loader._resolve_remote_bundle_path(bento_path) as local_path:
                return self._add(deployment_pb, bento_pb, local_path)

        deployment_spec = deployment_pb.spec
        lambda_deployment_config = deployment_spec.aws_lambda_operator_config
        bento_service_metadata = bento_pb.bento.bento_service_metadata
        lambda_s3_bucket = generate_aws_compatible_string(
            'btml-{namespace}-{name}-{random_string}'.format(
                namespace=deployment_pb.namespace,
                name=deployment_pb.name,
                random_string=uuid.uuid4().hex[:6].lower(),
            ))

        try:
            py_major, py_minor, _ = bento_service_metadata.env.python_version.split(
                '.')
            if py_major != '3':
                raise BentoMLException(
                    'Python 2 is not supported for Lambda Deployment')
            python_runtime = 'python{}.{}'.format(py_major, py_minor)

            artifact_types = [
                item.artifact_type for item in bento_service_metadata.artifacts
            ]
            if any(i in ['TensorflowSavedModelArtifact', 'KerasModelArtifact']
                   for i in artifact_types) and (py_major, py_minor) != ('3',
                                                                         '6'):
                raise BentoMLException(
                    'For Tensorflow and Keras model, only python3.6 is '
                    'supported for AWS Lambda deployment')

            api_names = ([lambda_deployment_config.api_name]
                         if lambda_deployment_config.api_name else
                         [api.name for api in bento_service_metadata.apis])

            raise_if_api_names_not_found_in_bento_service_metadata(
                bento_service_metadata, api_names)

            create_s3_bucket_if_not_exists(lambda_s3_bucket,
                                           lambda_deployment_config.region)
            deployment_path_prefix = os.path.join(deployment_pb.namespace,
                                                  deployment_pb.name)
            with TempDirectory() as lambda_project_dir:
                logger.debug(
                    'Generating cloudformation template.yaml for lambda project at %s',
                    lambda_project_dir,
                )
                template_file_path = _create_aws_lambda_cloudformation_template_file(
                    project_dir=lambda_project_dir,
                    namespace=deployment_pb.namespace,
                    deployment_name=deployment_pb.name,
                    deployment_path_prefix=deployment_path_prefix,
                    api_names=api_names,
                    bento_service_name=deployment_spec.bento_name,
                    s3_bucket_name=lambda_s3_bucket,
                    py_runtime=python_runtime,
                    memory_size=lambda_deployment_config.memory_size,
                    timeout=lambda_deployment_config.timeout,
                )
                logger.debug('Validating generated template.yaml')
                validate_lambda_template(
                    template_file_path,
                    lambda_deployment_config.region,
                    lambda_project_dir,
                )
                logger.debug(
                    'Initializing lambda project in directory: %s ...',
                    lambda_project_dir,
                )
                init_sam_project(
                    lambda_project_dir,
                    bento_path,
                    deployment_pb.name,
                    deployment_spec.bento_name,
                    api_names,
                    aws_region=lambda_deployment_config.region,
                )
                for api_name in api_names:
                    build_directory = os.path.join(lambda_project_dir,
                                                   '.aws-sam', 'build',
                                                   api_name)
                    logger.debug(
                        'Checking is function "%s" bundle under lambda size '
                        'limit',
                        api_name,
                    )
                    # Since we only use s3 get object in lambda function, and
                    # lambda function pack their own boto3/botocore modules,
                    # we will just delete those modules from function bundle
                    # directory
                    delete_list = ['boto3', 'botocore']
                    for name in delete_list:
                        logger.debug('Remove module "%s" from build directory',
                                     name)
                        shutil.rmtree(os.path.join(build_directory, name))
                    total_build_dir_size = total_file_or_directory_size(
                        build_directory)
                    if total_build_dir_size > LAMBDA_FUNCTION_MAX_LIMIT:
                        raise BentoMLException(
                            'Build function size is over 700MB, max size '
                            'capable for AWS Lambda function')
                    if total_build_dir_size >= LAMBDA_FUNCTION_LIMIT:
                        logger.debug(
                            'Function %s is over lambda size limit, attempting '
                            'reduce it',
                            api_name,
                        )
                        reduce_bundle_size_and_upload_extra_resources_to_s3(
                            build_directory=build_directory,
                            region=lambda_deployment_config.region,
                            s3_bucket=lambda_s3_bucket,
                            deployment_prefix=deployment_path_prefix,
                            function_name=api_name,
                            lambda_project_dir=lambda_project_dir,
                        )
                    else:
                        logger.debug(
                            'Function bundle is within Lambda limit, removing '
                            'download_extra_resources.py file from function bundle'
                        )
                        os.remove(
                            os.path.join(build_directory,
                                         'download_extra_resources.py'))
                logger.info('Packaging AWS Lambda project at %s ...',
                            lambda_project_dir)
                lambda_package(
                    lambda_project_dir,
                    lambda_deployment_config.region,
                    lambda_s3_bucket,
                    deployment_path_prefix,
                )
                logger.info('Deploying lambda project')
                stack_name = generate_aws_compatible_string(
                    deployment_pb.namespace + '-' + deployment_pb.name)
                lambda_deploy(
                    lambda_project_dir,
                    lambda_deployment_config.region,
                    stack_name=stack_name,
                )

            deployment_pb.state.state = DeploymentState.PENDING
            return ApplyDeploymentResponse(status=Status.OK(),
                                           deployment=deployment_pb)
        except BentoMLException as error:
            if lambda_s3_bucket and lambda_deployment_config:
                _cleanup_s3_bucket_if_exist(lambda_s3_bucket,
                                            lambda_deployment_config.region)
            raise error
Пример #3
0
    def _update(self, deployment_pb, current_deployment, bento_pb, bento_path):
        if loader._is_remote_path(bento_path):
            with loader._resolve_remote_bundle_path(bento_path) as local_path:
                return self._update(deployment_pb, current_deployment,
                                    bento_pb, local_path)
        updated_deployment_spec = deployment_pb.spec
        updated_sagemaker_config = updated_deployment_spec.sagemaker_operator_config
        sagemaker_client = boto3.client('sagemaker',
                                        updated_sagemaker_config.region)

        try:
            raise_if_api_names_not_found_in_bento_service_metadata(
                bento_pb.bento.bento_service_metadata,
                [updated_sagemaker_config.api_name],
            )
            describe_latest_deployment_state = self.describe(deployment_pb)
            current_deployment_spec = current_deployment.spec
            current_sagemaker_config = current_deployment_spec.sagemaker_operator_config
            latest_deployment_state = json.loads(
                describe_latest_deployment_state.state.info_json)

            current_ecr_image_tag = latest_deployment_state[
                'ProductionVariants'][0]['DeployedImages'][0]['SpecifiedImage']
            if (updated_deployment_spec.bento_name !=
                    current_deployment_spec.bento_name
                    or updated_deployment_spec.bento_version !=
                    current_deployment_spec.bento_version):
                logger.debug(
                    'BentoService tag is different from current deployment, '
                    'creating new docker image and push to ECR')
                with TempDirectory() as temp_dir:
                    sagemaker_project_dir = os.path.join(
                        temp_dir, updated_deployment_spec.bento_name)
                    _init_sagemaker_project(
                        sagemaker_project_dir,
                        bento_path,
                        bento_pb.bento.bento_service_metadata.env.
                        docker_base_image,
                    )
                    ecr_image_path = create_and_push_docker_image_to_ecr(
                        updated_sagemaker_config.region,
                        updated_deployment_spec.bento_name,
                        updated_deployment_spec.bento_version,
                        sagemaker_project_dir,
                    )
            else:
                logger.debug('Using existing ECR image for Sagemaker model')
                ecr_image_path = current_ecr_image_tag

            (
                updated_sagemaker_model_name,
                updated_sagemaker_endpoint_config_name,
                sagemaker_endpoint_name,
            ) = _get_sagemaker_resource_names(deployment_pb)
            (
                current_sagemaker_model_name,
                current_sagemaker_endpoint_config_name,
                _,
            ) = _get_sagemaker_resource_names(current_deployment)

            if (updated_sagemaker_config.api_name !=
                    current_sagemaker_config.api_name
                    or updated_sagemaker_config.
                    num_of_gunicorn_workers_per_instance !=
                    current_sagemaker_config.
                    num_of_gunicorn_workers_per_instance
                    or ecr_image_path != current_ecr_image_tag):
                logger.debug(
                    'Sagemaker model requires update. Delete current sagemaker model %s'
                    'and creating new model %s',
                    current_sagemaker_model_name,
                    updated_sagemaker_model_name,
                )
                _delete_sagemaker_model_if_exist(sagemaker_client,
                                                 current_sagemaker_model_name)
                _create_sagemaker_model(
                    sagemaker_client,
                    updated_sagemaker_model_name,
                    ecr_image_path,
                    updated_sagemaker_config,
                )
            # When bento service tag is not changed, we need to delete the current
            # endpoint configuration in order to create new one to avoid name collation
            if (current_sagemaker_endpoint_config_name ==
                    updated_sagemaker_endpoint_config_name):
                logger.debug(
                    'Current sagemaker config name %s is same as updated one, '
                    'delete it before create new endpoint config',
                    current_sagemaker_endpoint_config_name,
                )
                _delete_sagemaker_endpoint_config_if_exist(
                    sagemaker_client, current_sagemaker_endpoint_config_name)
            logger.debug(
                'Create new endpoint configuration %s',
                updated_sagemaker_endpoint_config_name,
            )
            _create_sagemaker_endpoint_config(
                sagemaker_client,
                updated_sagemaker_model_name,
                updated_sagemaker_endpoint_config_name,
                updated_sagemaker_config,
            )
            logger.debug(
                'Updating endpoint to new endpoint configuration %s',
                updated_sagemaker_endpoint_config_name,
            )
            _update_sagemaker_endpoint(
                sagemaker_client,
                sagemaker_endpoint_name,
                updated_sagemaker_endpoint_config_name,
            )
            logger.debug(
                'Delete old sagemaker endpoint config %s',
                current_sagemaker_endpoint_config_name,
            )
            _delete_sagemaker_endpoint_config_if_exist(
                sagemaker_client, current_sagemaker_endpoint_config_name)
        except AWSServiceError as e:
            delete_sagemaker_deployment_resources_if_exist(deployment_pb)
            raise e

        return ApplyDeploymentResponse(status=Status.OK(),
                                       deployment=deployment_pb)