|
| 1 | +# Copyright 2017-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Models that can be deployed to serverless compute.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import time |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +import boto3 |
| 20 | +import botocore |
| 21 | + |
| 22 | +from sagemaker.model import ModelBase |
| 23 | + |
| 24 | +from .predictor import LambdaPredictor |
| 25 | + |
| 26 | + |
| 27 | +class LambdaModel(ModelBase): |
| 28 | + """A model that can be deployed to Lambda.""" |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, image_uri: str, role: str, client: Optional[botocore.client.BaseClient] = None |
| 32 | + ) -> None: |
| 33 | + """Initialize instance attributes. |
| 34 | +
|
| 35 | + Arguments: |
| 36 | + image_uri: URI of a container image in the Amazon ECR registry. The image |
| 37 | + should contain a handler that performs inference. |
| 38 | + role: The Amazon Resource Name (ARN) of the IAM role that Lambda will assume |
| 39 | + when it performs inference |
| 40 | + client: The Lambda client used to interact with Lambda. |
| 41 | + """ |
| 42 | + self._client = client or boto3.client("lambda") |
| 43 | + self._image_uri = image_uri |
| 44 | + self._role = role |
| 45 | + |
| 46 | + def deploy( |
| 47 | + self, function_name: str, timeout: int, memory_size: int, wait: bool = True |
| 48 | + ) -> LambdaPredictor: |
| 49 | + """Create a Lambda function using the image specified in the constructor. |
| 50 | +
|
| 51 | + Arguments: |
| 52 | + function_name: The name of the function. |
| 53 | + timeout: The number of seconds that the function can run for before being terminated. |
| 54 | + memory_size: The amount of memory in MB that the function has access to. |
| 55 | + wait: If true, wait until the deployment completes (default: True). |
| 56 | +
|
| 57 | + Returns: |
| 58 | + A LambdaPredictor instance that performs inference using the specified image. |
| 59 | + """ |
| 60 | + response = self._client.create_function( |
| 61 | + FunctionName=function_name, |
| 62 | + PackageType="Image", |
| 63 | + Role=self._role, |
| 64 | + Code={ |
| 65 | + "ImageUri": self._image_uri, |
| 66 | + }, |
| 67 | + Timeout=timeout, |
| 68 | + MemorySize=memory_size, |
| 69 | + ) |
| 70 | + |
| 71 | + if not wait: |
| 72 | + return LambdaPredictor(function_name, client=self._client) |
| 73 | + |
| 74 | + # Poll function state. |
| 75 | + polling_interval = 5 |
| 76 | + while response["State"] == "Pending": |
| 77 | + time.sleep(polling_interval) |
| 78 | + response = self._client.get_function_configuration(FunctionName=function_name) |
| 79 | + |
| 80 | + if response["State"] != "Active": |
| 81 | + raise RuntimeError("Failed to deploy model to Lambda: %s" % response["StateReason"]) |
| 82 | + |
| 83 | + return LambdaPredictor(function_name, client=self._client) |
| 84 | + |
| 85 | + def delete_model(self) -> None: |
| 86 | + """Destroy resources associated with this model. |
| 87 | +
|
| 88 | + This method does not delete the image specified in the constructor. As |
| 89 | + a result, this method is a no-op. |
| 90 | + """ |
0 commit comments