# Amazon SageMaker Construct Library

> Version 2.278.0-alpha.0

> **Separate package**
>
> `@aws-cdk/aws-sagemaker-alpha` is not part of `aws-cdk-lib` — install it separately:
>
> ```sh
> npm install @aws-cdk/aws-sagemaker-alpha
> pip install aws-cdk.aws-sagemaker-alpha
> ```
>
> This module is [**experimental**](https://docs.aws.amazon.com/cdk/api/stability.html) (alpha): its API may change in any release, without a major version bump.
---

![cdk-constructs: Experimental](https://img.shields.io/badge/cdk--constructs-experimental-important.svg?style=for-the-badge)

> The APIs of higher level constructs in this module are experimental and under active development.
> They are subject to non-backward compatible changes or removal in any future version. These are
> not subject to the [Semantic Versioning](https://semver.org/) model and breaking changes will be
> announced in the release notes. This means that while you may use them, you may need to update
> your source code when upgrading to a newer version of this package.

---

Amazon SageMaker provides every developer and data scientist with the ability to build, train, and
deploy machine learning models quickly. Amazon SageMaker is a fully-managed service that covers the
entire machine learning workflow to label and prepare your data, choose an algorithm, train the
model, tune and optimize it for deployment, make predictions, and take action. Your models get to
production faster with much less effort and lower cost.

## Model

To create a machine learning model with Amazon Sagemaker, use the [`Model`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.Model.html) construct. This construct
includes properties that can be configured to define model components, including the model inference
code as a Docker image and an optional set of separate model data artifacts. See the [AWS
documentation](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-marketplace-develop.html)
to learn more about SageMaker models.

### Single Container Model

In the event that a single container is sufficient for your inference use-case, you can define a
single-container model:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';
import * as path from 'path';

const image = sagemaker.ContainerImage.fromAsset(path.join('path', 'to', 'Dockerfile', 'directory'));
const modelData = sagemaker.ModelData.fromAsset(path.join('path', 'to', 'artifact', 'file.tar.gz'));

const model = new sagemaker.Model(this, 'PrimaryContainerModel', {
  containers: [
    {
      image: image,
      modelData: modelData,
    }
  ]
});
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker
import path as path

image = sagemaker.ContainerImage.from_asset(path.join("path", "to", "Dockerfile", "directory"))
model_data = sagemaker.ModelData.from_asset(path.join("path", "to", "artifact", "file.tar.gz"))

model = sagemaker.Model(self, "PrimaryContainerModel",
    containers=[sagemaker.ContainerDefinition(
        image=image,
        model_data=model_data
    )
    ]
)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;
import path.*;

ContainerImage image = ContainerImage.fromAsset(join("path", "to", "Dockerfile", "directory"));
ModelData modelData = ModelData.fromAsset(join("path", "to", "artifact", "file.tar.gz"));

Model model = Model.Builder.create(this, "PrimaryContainerModel")
        .containers(List.of(ContainerDefinition.builder()
                .image(image)
                .modelData(modelData)
                .build()))
        .build();
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;
using Path;

var image = ContainerImage.FromAsset(Join("path", "to", "Dockerfile", "directory"));
var modelData = ModelData.FromAsset(Join("path", "to", "artifact", "file.tar.gz"));

var model = new Model(this, "PrimaryContainerModel", new ModelProps {
    Containers = new [] { new ContainerDefinition {
        Image = image,
        ModelData = modelData
    } }
});
```

Golang:

```go
import "github.com/aws/aws-cdk-go/awscdksagemakeralpha"
import "github.com/aws-samples/dummy/path"

image := sagemaker.ContainerImage_FromAsset(path.join(jsii.String("path"), jsii.String("to"), jsii.String("Dockerfile"), jsii.String("directory")))
modelData := sagemaker.ModelData_FromAsset(path.join(jsii.String("path"), jsii.String("to"), jsii.String("artifact"), jsii.String("file.tar.gz")))

model := sagemaker.NewModel(this, jsii.String("PrimaryContainerModel"), &ModelProps{
	Containers: []ContainerDefinition{
		&ContainerDefinition{
			Image: image,
			ModelData: modelData,
		},
	},
})
```

### Inference Pipeline Model

An inference pipeline is an Amazon SageMaker model that is composed of a linear sequence of multiple
containers that process requests for inferences on data. See the [AWS
documentation](https://docs.aws.amazon.com/sagemaker/latest/dg/inference-pipelines.html) to learn
more about SageMaker inference pipelines. To define an inference pipeline, you can provide
additional containers for your model:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const image1: sagemaker.ContainerImage;
declare const modelData1: sagemaker.ModelData;
declare const image2: sagemaker.ContainerImage;
declare const modelData2: sagemaker.ModelData;
declare const image3: sagemaker.ContainerImage;
declare const modelData3: sagemaker.ModelData;

const model = new sagemaker.Model(this, 'InferencePipelineModel', {
  containers: [
    { image: image1, modelData: modelData1 },
    { image: image2, modelData: modelData2 },
    { image: image3, modelData: modelData3 }
  ],
});
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

# image1: sagemaker.ContainerImage
# model_data1: sagemaker.ModelData
# image2: sagemaker.ContainerImage
# model_data2: sagemaker.ModelData
# image3: sagemaker.ContainerImage
# model_data3: sagemaker.ModelData

model = sagemaker.Model(self, "InferencePipelineModel",
    containers=[sagemaker.ContainerDefinition(image=image1, model_data=model_data1), sagemaker.ContainerDefinition(image=image2, model_data=model_data2), sagemaker.ContainerDefinition(image=image3, model_data=model_data3)
    ]
)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

ContainerImage image1;
ModelData modelData1;
ContainerImage image2;
ModelData modelData2;
ContainerImage image3;
ModelData modelData3;

Model model = Model.Builder.create(this, "InferencePipelineModel")
        .containers(List.of(ContainerDefinition.builder().image(image1).modelData(modelData1).build(), ContainerDefinition.builder().image(image2).modelData(modelData2).build(), ContainerDefinition.builder().image(image3).modelData(modelData3).build()))
        .build();
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

ContainerImage image1;
ModelData modelData1;
ContainerImage image2;
ModelData modelData2;
ContainerImage image3;
ModelData modelData3;

var model = new Model(this, "InferencePipelineModel", new ModelProps {
    Containers = new [] { new ContainerDefinition { Image = image1, ModelData = modelData1 }, new ContainerDefinition { Image = image2, ModelData = modelData2 }, new ContainerDefinition { Image = image3, ModelData = modelData3 } }
});
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

var image1 ContainerImage
var modelData1 ModelData
var image2 ContainerImage
var modelData2 ModelData
var image3 ContainerImage
var modelData3 ModelData

model := sagemaker.NewModel(this, jsii.String("InferencePipelineModel"), &ModelProps{
	Containers: []ContainerDefinition{
		&ContainerDefinition{
			Image: image1,
			ModelData: modelData1,
		},
		&ContainerDefinition{
			Image: image2,
			ModelData: modelData2,
		},
		&ContainerDefinition{
			Image: image3,
			ModelData: modelData3,
		},
	},
})
```

### Model Properties

#### Network Isolation

If you enable [network isolation](https://docs.aws.amazon.com/sagemaker/latest/dg/mkt-algo-model-internet-free.html), the containers can't make any outbound network calls, even to other AWS services such as Amazon S3. Additionally, no AWS credentials are made available to the container runtime environment.

To enable network isolation, set the `networkIsolation` property to `true`:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const image: sagemaker.ContainerImage;
declare const modelData: sagemaker.ModelData;

const model = new sagemaker.Model(this, 'ContainerModel', {
  containers: [
    {
      image,
      modelData,
    }
  ],
  networkIsolation: true,
});
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

# image: sagemaker.ContainerImage
# model_data: sagemaker.ModelData

model = sagemaker.Model(self, "ContainerModel",
    containers=[sagemaker.ContainerDefinition(
        image=image,
        model_data=model_data
    )
    ],
    network_isolation=True
)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

ContainerImage image;
ModelData modelData;

Model model = Model.Builder.create(this, "ContainerModel")
        .containers(List.of(ContainerDefinition.builder()
                .image(image)
                .modelData(modelData)
                .build()))
        .networkIsolation(true)
        .build();
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

ContainerImage image;
ModelData modelData;

var model = new Model(this, "ContainerModel", new ModelProps {
    Containers = new [] { new ContainerDefinition {
        Image = image,
        ModelData = modelData
    } },
    NetworkIsolation = true
});
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

var image ContainerImage
var modelData ModelData

model := sagemaker.NewModel(this, jsii.String("ContainerModel"), &ModelProps{
	Containers: []ContainerDefinition{
		&ContainerDefinition{
			Image: *Image,
			ModelData: *ModelData,
		},
	},
	NetworkIsolation: jsii.Boolean(true),
})
```

### Container Images

Inference code can be stored in the Amazon EC2 Container Registry (Amazon ECR), which is specified
via [`ContainerDefinition`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ContainerDefinition.html)'s `image` property which accepts a class that extends the [`ContainerImage`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ContainerImage.html)
abstract base class.

#### Asset Image

Reference a local directory containing a Dockerfile:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';
import * as path from 'path';

const image = sagemaker.ContainerImage.fromAsset(path.join('path', 'to', 'Dockerfile', 'directory'));
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker
import path as path

image = sagemaker.ContainerImage.from_asset(path.join("path", "to", "Dockerfile", "directory"))
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;
import path.*;

ContainerImage image = ContainerImage.fromAsset(join("path", "to", "Dockerfile", "directory"));
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;
using Path;

var image = ContainerImage.FromAsset(Join("path", "to", "Dockerfile", "directory"));
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"
import path "github.com/aws-samples/dummy/path"

image := sagemaker.ContainerImage_FromAsset(path.join(jsii.String("path"), jsii.String("to"), jsii.String("Dockerfile"), jsii.String("directory")))
```

#### ECR Image

Reference an image available within ECR:

TypeScript:

```ts
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

const repository = ecr.Repository.fromRepositoryName(this, 'Repository', 'repo');
const image = sagemaker.ContainerImage.fromEcrRepository(repository, 'tag');
```

Python:

```python
import aws_cdk.aws_ecr as ecr
import aws_cdk.aws_sagemaker_alpha as sagemaker

repository = ecr.Repository.from_repository_name(self, "Repository", "repo")
image = sagemaker.ContainerImage.from_ecr_repository(repository, "tag")
```

Java:

```java
import software.amazon.awscdk.services.ecr.*;
import software.amazon.awscdk.services.sagemaker.alpha.*;

IRepository repository = Repository.fromRepositoryName(this, "Repository", "repo");
ContainerImage image = ContainerImage.fromEcrRepository(repository, "tag");
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.ECR;
using Amazon.CDK.AWS.Sagemaker.Alpha;

var repository = Repository.FromRepositoryName(this, "Repository", "repo");
var image = ContainerImage.FromEcrRepository(repository, "tag");
```

Golang:

```go
import ecr "github.com/aws/aws-cdk-go/awscdk"
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

repository := ecr.Repository_FromRepositoryName(this, jsii.String("Repository"), jsii.String("repo"))
image := sagemaker.ContainerImage_FromEcrRepository(repository, jsii.String("tag"))
```

#### DLC Image

Reference a deep learning container image:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

const repositoryName = 'huggingface-pytorch-training';
const tag = '1.13.1-transformers4.26.0-gpu-py39-cu117-ubuntu20.04';

const image = sagemaker.ContainerImage.fromDlc(repositoryName, tag);
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

repository_name = "huggingface-pytorch-training"
tag = "1.13.1-transformers4.26.0-gpu-py39-cu117-ubuntu20.04"

image = sagemaker.ContainerImage.from_dlc(repository_name, tag)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

String repositoryName = "huggingface-pytorch-training";
String tag = "1.13.1-transformers4.26.0-gpu-py39-cu117-ubuntu20.04";

ContainerImage image = ContainerImage.fromDlc(repositoryName, tag);
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

var repositoryName = "huggingface-pytorch-training";
var tag = "1.13.1-transformers4.26.0-gpu-py39-cu117-ubuntu20.04";

var image = ContainerImage.FromDlc(repositoryName, tag);
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

repositoryName := "huggingface-pytorch-training"
tag := "1.13.1-transformers4.26.0-gpu-py39-cu117-ubuntu20.04"

image := sagemaker.ContainerImage_FromDlc(repositoryName, tag)
```

### Model Artifacts

If you choose to decouple your model artifacts from your inference code (as is natural given
different rates of change between inference code and model artifacts), the artifacts can be
specified via the `modelData` property which accepts a class that extends the [`ModelData`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ModelData.html) abstract
base class. The default is to have no model artifacts associated with a model.

#### Asset Model Data

Reference local model data:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';
import * as path from 'path';

const modelData = sagemaker.ModelData.fromAsset(path.join('path', 'to', 'artifact', 'file.tar.gz'));
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker
import path as path

model_data = sagemaker.ModelData.from_asset(path.join("path", "to", "artifact", "file.tar.gz"))
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;
import path.*;

ModelData modelData = ModelData.fromAsset(join("path", "to", "artifact", "file.tar.gz"));
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;
using Path;

var modelData = ModelData.FromAsset(Join("path", "to", "artifact", "file.tar.gz"));
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"
import path "github.com/aws-samples/dummy/path"

modelData := sagemaker.ModelData_FromAsset(path.join(jsii.String("path"), jsii.String("to"), jsii.String("artifact"), jsii.String("file.tar.gz")))
```

#### S3 Model Data

Reference an S3 bucket and object key as the artifacts for a model:

TypeScript:

```ts
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

const bucket = new s3.Bucket(this, 'MyBucket');
const modelData = sagemaker.ModelData.fromBucket(bucket, 'path/to/artifact/file.tar.gz');
```

Python:

```python
import aws_cdk.aws_s3 as s3
import aws_cdk.aws_sagemaker_alpha as sagemaker

bucket = s3.Bucket(self, "MyBucket")
model_data = sagemaker.ModelData.from_bucket(bucket, "path/to/artifact/file.tar.gz")
```

Java:

```java
import software.amazon.awscdk.services.s3.*;
import software.amazon.awscdk.services.sagemaker.alpha.*;

Bucket bucket = new Bucket(this, "MyBucket");
ModelData modelData = ModelData.fromBucket(bucket, "path/to/artifact/file.tar.gz");
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.S3;
using Amazon.CDK.AWS.Sagemaker.Alpha;

var bucket = new Bucket(this, "MyBucket");
var modelData = ModelData.FromBucket(bucket, "path/to/artifact/file.tar.gz");
```

Golang:

```go
import s3 "github.com/aws/aws-cdk-go/awscdk"
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

bucket := s3.NewBucket(this, jsii.String("MyBucket"))
modelData := sagemaker.ModelData_FromBucket(bucket, jsii.String("path/to/artifact/file.tar.gz"))
```

## Model Hosting

Amazon SageMaker provides model hosting services for model deployment. Amazon SageMaker provides an
HTTPS endpoint where your machine learning model is available to provide inferences.

### Endpoint Configuration

By using the [`EndpointConfig`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.EndpointConfig.html) construct, you can define a set of endpoint configuration which can be
used to provision one or more endpoints. In this configuration, you identify one or more models to
deploy and the resources that you want Amazon SageMaker to provision. You define one or more
production variants, each of which identifies a model. Each production variant also describes the
resources that you want Amazon SageMaker to provision. If you are hosting multiple models, you also
assign a variant weight to specify how much traffic you want to allocate to each model. For example,
suppose that you want to host two models, A and B, and you assign traffic weight 2 for model A and 1
for model B. Amazon SageMaker distributes two-thirds of the traffic to Model A, and one-third to
model B:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const modelA: sagemaker.Model;
declare const modelB: sagemaker.Model;

const endpointConfig = new sagemaker.EndpointConfig(this, 'EndpointConfig', {
  instanceProductionVariants: [
    {
      model: modelA,
      variantName: 'modelA',
      initialVariantWeight: 2.0,
    },
    {
      model: modelB,
      variantName: 'variantB',
      initialVariantWeight: 1.0,
    },
  ]
});
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

# model_a: sagemaker.Model
# model_b: sagemaker.Model

endpoint_config = sagemaker.EndpointConfig(self, "EndpointConfig",
    instance_production_variants=[sagemaker.InstanceProductionVariantProps(
        model=model_a,
        variant_name="modelA",
        initial_variant_weight=2
    ), sagemaker.InstanceProductionVariantProps(
        model=model_b,
        variant_name="variantB",
        initial_variant_weight=1
    )
    ]
)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

Model modelA;
Model modelB;

EndpointConfig endpointConfig = EndpointConfig.Builder.create(this, "EndpointConfig")
        .instanceProductionVariants(List.of(InstanceProductionVariantProps.builder()
                .model(modelA)
                .variantName("modelA")
                .initialVariantWeight(2)
                .build(), InstanceProductionVariantProps.builder()
                .model(modelB)
                .variantName("variantB")
                .initialVariantWeight(1)
                .build()))
        .build();
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

Model modelA;
Model modelB;

var endpointConfig = new EndpointConfig(this, "EndpointConfig", new EndpointConfigProps {
    InstanceProductionVariants = new [] { new InstanceProductionVariantProps {
        Model = modelA,
        VariantName = "modelA",
        InitialVariantWeight = 2
    }, new InstanceProductionVariantProps {
        Model = modelB,
        VariantName = "variantB",
        InitialVariantWeight = 1
    } }
});
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

var modelA Model
var modelB Model

endpointConfig := sagemaker.NewEndpointConfig(this, jsii.String("EndpointConfig"), &EndpointConfigProps{
	InstanceProductionVariants: []InstanceProductionVariantProps{
		&InstanceProductionVariantProps{
			Model: modelA,
			VariantName: jsii.String("modelA"),
			InitialVariantWeight: jsii.Number(2),
		},
		&InstanceProductionVariantProps{
			Model: modelB,
			VariantName: jsii.String("variantB"),
			InitialVariantWeight: jsii.Number(1),
		},
	},
})
```

#### Container Startup Health Check Timeout

You can specify a timeout value for your inference container to pass health check by configuring
the `containerStartupHealthCheckTimeout` property. This is useful when your model takes longer
to initialize and you want to avoid premature health check failures:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const model: sagemaker.Model;

const endpointConfig = new sagemaker.EndpointConfig(this, 'EndpointConfig', {
  instanceProductionVariants: [
    {
      model: model,
      variantName: 'my-variant',
      containerStartupHealthCheckTimeout: cdk.Duration.minutes(5), // 5 minutes timeout
    },
  ]
});
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

# model: sagemaker.Model

endpoint_config = sagemaker.EndpointConfig(self, "EndpointConfig",
    instance_production_variants=[sagemaker.InstanceProductionVariantProps(
        model=model,
        variant_name="my-variant",
        container_startup_health_check_timeout=cdk.Duration.minutes(5)
    )
    ]
)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

Model model;

EndpointConfig endpointConfig = EndpointConfig.Builder.create(this, "EndpointConfig")
        .instanceProductionVariants(List.of(InstanceProductionVariantProps.builder()
                .model(model)
                .variantName("my-variant")
                .containerStartupHealthCheckTimeout(Duration.minutes(5))
                .build()))
        .build();
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

Model model;

var endpointConfig = new EndpointConfig(this, "EndpointConfig", new EndpointConfigProps {
    InstanceProductionVariants = new [] { new InstanceProductionVariantProps {
        Model = model,
        VariantName = "my-variant",
        ContainerStartupHealthCheckTimeout = Duration.Minutes(5)
    } }
});
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

var model Model

endpointConfig := sagemaker.NewEndpointConfig(this, jsii.String("EndpointConfig"), &EndpointConfigProps{
	InstanceProductionVariants: []InstanceProductionVariantProps{
		&InstanceProductionVariantProps{
			Model: model,
			VariantName: jsii.String("my-variant"),
			ContainerStartupHealthCheckTimeout: cdk.Duration_Minutes(jsii.Number(5)),
		},
	},
})
```

The timeout value must be between 60 seconds and 1 hour (3600 seconds). If not specified,
Amazon SageMaker uses the default timeout behavior.

### Serverless Inference

Amazon SageMaker Serverless Inference is a purpose-built inference option that makes it easy for you to deploy and scale ML models. Serverless endpoints automatically launch compute resources and scale them in and out depending on traffic, eliminating the need to choose instance types or manage scaling policies. For more information, see [SageMaker Serverless Inference](https://docs.aws.amazon.com/sagemaker/latest/dg/serverless-endpoints.html).

To create a serverless endpoint configuration, use the `serverlessProductionVariant` property:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const model: sagemaker.Model;

const endpointConfig = new sagemaker.EndpointConfig(this, 'ServerlessEndpointConfig', {
  serverlessProductionVariant: {
    model: model,
    variantName: 'serverlessVariant',
    maxConcurrency: 10,
    memorySizeInMB: 2048,
    provisionedConcurrency: 5, // optional
  },
});
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

# model: sagemaker.Model

endpoint_config = sagemaker.EndpointConfig(self, "ServerlessEndpointConfig",
    serverless_production_variant=sagemaker.ServerlessProductionVariantProps(
        model=model,
        variant_name="serverlessVariant",
        max_concurrency=10,
        memory_size_in_mb=2048,
        provisioned_concurrency=5
    )
)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

Model model;

EndpointConfig endpointConfig = EndpointConfig.Builder.create(this, "ServerlessEndpointConfig")
        .serverlessProductionVariant(ServerlessProductionVariantProps.builder()
                .model(model)
                .variantName("serverlessVariant")
                .maxConcurrency(10)
                .memorySizeInMB(2048)
                .provisionedConcurrency(5)
                .build())
        .build();
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

Model model;

var endpointConfig = new EndpointConfig(this, "ServerlessEndpointConfig", new EndpointConfigProps {
    ServerlessProductionVariant = new ServerlessProductionVariantProps {
        Model = model,
        VariantName = "serverlessVariant",
        MaxConcurrency = 10,
        MemorySizeInMB = 2048,
        ProvisionedConcurrency = 5
    }
});
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

var model Model

endpointConfig := sagemaker.NewEndpointConfig(this, jsii.String("ServerlessEndpointConfig"), &EndpointConfigProps{
	ServerlessProductionVariant: &ServerlessProductionVariantProps{
		Model: model,
		VariantName: jsii.String("serverlessVariant"),
		MaxConcurrency: jsii.Number(10),
		MemorySizeInMB: jsii.Number(2048),
		ProvisionedConcurrency: jsii.Number(5),
	},
})
```

Serverless inference is ideal for workloads with intermittent or unpredictable traffic patterns. You can configure:

- `maxConcurrency`: Maximum concurrent invocations (1-200)
- `memorySizeInMB`: Memory allocation in 1GB increments (1024, 2048, 3072, 4096, 5120, or 6144 MB)
- `provisionedConcurrency`: Optional pre-warmed capacity to reduce cold starts

**Note**: Provisioned concurrency incurs charges even when the endpoint is not processing requests. Use it only when you need to minimize cold start latency.

You cannot mix serverless and instance-based variants in the same endpoint configuration.

### Endpoint

When you create an endpoint from an [`EndpointConfig`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.EndpointConfig.html), Amazon SageMaker launches the ML compute
instances and deploys the model or models as specified in the configuration. To get inferences from
the model, client applications send requests to the Amazon SageMaker Runtime HTTPS endpoint. For
more information about the API, see the
[InvokeEndpoint](https://docs.aws.amazon.com/sagemaker/latest/dg/API_runtime_InvokeEndpoint.html)
API. Defining an endpoint requires at minimum the associated endpoint configuration:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const endpointConfig: sagemaker.EndpointConfig;

const endpoint = new sagemaker.Endpoint(this, 'Endpoint', { endpointConfig });
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

# endpoint_config: sagemaker.EndpointConfig

endpoint = sagemaker.Endpoint(self, "Endpoint", endpoint_config=endpoint_config)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

EndpointConfig endpointConfig;

Endpoint endpoint = Endpoint.Builder.create(this, "Endpoint").endpointConfig(endpointConfig).build();
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

EndpointConfig endpointConfig;

var endpoint = new Endpoint(this, "Endpoint", new EndpointProps { EndpointConfig = endpointConfig });
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

var endpointConfig EndpointConfig

endpoint := sagemaker.NewEndpoint(this, jsii.String("Endpoint"), &EndpointProps{
	EndpointConfig: EndpointConfig,
})
```

### AutoScaling

To enable autoscaling on the production variant, use the `autoScaleInstanceCount` method:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const model: sagemaker.Model;

const variantName = 'my-variant';
const endpointConfig = new sagemaker.EndpointConfig(this, 'EndpointConfig', {
  instanceProductionVariants: [
    {
      model: model,
      variantName: variantName,
    },
  ]
});

const endpoint = new sagemaker.Endpoint(this, 'Endpoint', { endpointConfig });
const productionVariant = endpoint.findInstanceProductionVariant(variantName);
const instanceCount = productionVariant.autoScaleInstanceCount({
  maxCapacity: 3
});
instanceCount.scaleOnInvocations('LimitRPS', {
  maxRequestsPerSecond: 30,
});
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

# model: sagemaker.Model

variant_name = "my-variant"
endpoint_config = sagemaker.EndpointConfig(self, "EndpointConfig",
    instance_production_variants=[sagemaker.InstanceProductionVariantProps(
        model=model,
        variant_name=variant_name
    )
    ]
)

endpoint = sagemaker.Endpoint(self, "Endpoint", endpoint_config=endpoint_config)
production_variant = endpoint.find_instance_production_variant(variant_name)
instance_count = production_variant.auto_scale_instance_count(
    max_capacity=3
)
instance_count.scale_on_invocations("LimitRPS",
    max_requests_per_second=30
)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

Model model;

String variantName = "my-variant";
EndpointConfig endpointConfig = EndpointConfig.Builder.create(this, "EndpointConfig")
        .instanceProductionVariants(List.of(InstanceProductionVariantProps.builder()
                .model(model)
                .variantName(variantName)
                .build()))
        .build();

Endpoint endpoint = Endpoint.Builder.create(this, "Endpoint").endpointConfig(endpointConfig).build();
IEndpointInstanceProductionVariant productionVariant = endpoint.findInstanceProductionVariant(variantName);
ScalableInstanceCount instanceCount = productionVariant.autoScaleInstanceCount(EnableScalingProps.builder()
        .maxCapacity(3)
        .build());
instanceCount.scaleOnInvocations("LimitRPS", InvocationsScalingProps.builder()
        .maxRequestsPerSecond(30)
        .build());
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

Model model;

var variantName = "my-variant";
var endpointConfig = new EndpointConfig(this, "EndpointConfig", new EndpointConfigProps {
    InstanceProductionVariants = new [] { new InstanceProductionVariantProps {
        Model = model,
        VariantName = variantName
    } }
});

var endpoint = new Endpoint(this, "Endpoint", new EndpointProps { EndpointConfig = endpointConfig });
var productionVariant = endpoint.FindInstanceProductionVariant(variantName);
var instanceCount = productionVariant.AutoScaleInstanceCount(new EnableScalingProps {
    MaxCapacity = 3
});
instanceCount.ScaleOnInvocations("LimitRPS", new InvocationsScalingProps {
    MaxRequestsPerSecond = 30
});
```

Golang:

```go
import "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

var model Model

variantName := "my-variant"
endpointConfig := sagemaker.NewEndpointConfig(this, jsii.String("EndpointConfig"), &EndpointConfigProps{
	InstanceProductionVariants: []InstanceProductionVariantProps{
		&InstanceProductionVariantProps{
			Model: model,
			VariantName: variantName,
		},
	},
})

endpoint := sagemaker.NewEndpoint(this, jsii.String("Endpoint"), &EndpointProps{
	EndpointConfig: EndpointConfig,
})
productionVariant := endpoint.FindInstanceProductionVariant(variantName)
instanceCount := productionVariant.AutoScaleInstanceCount(&EnableScalingProps{
	MaxCapacity: jsii.Number(3),
})
instanceCount.ScaleOnInvocations(jsii.String("LimitRPS"), &InvocationsScalingProps{
	MaxRequestsPerSecond: jsii.Number(30),
})
```

For load testing guidance on determining the maximum requests per second per instance, please see
this [documentation](https://docs.aws.amazon.com/sagemaker/latest/dg/endpoint-scaling-loadtest.html).

### Metrics

To monitor CloudWatch metrics for a production variant, use one or more of the metric convenience
methods:

TypeScript:

```ts
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const endpointConfig: sagemaker.EndpointConfig;

const endpoint = new sagemaker.Endpoint(this, 'Endpoint', { endpointConfig });
const productionVariant = endpoint.findInstanceProductionVariant('my-variant');
productionVariant.metricModelLatency().createAlarm(this, 'ModelLatencyAlarm', {
  threshold: 100000,
  evaluationPeriods: 3,
});
```

Python:

```python
import aws_cdk.aws_sagemaker_alpha as sagemaker

# endpoint_config: sagemaker.EndpointConfig

endpoint = sagemaker.Endpoint(self, "Endpoint", endpoint_config=endpoint_config)
production_variant = endpoint.find_instance_production_variant("my-variant")
production_variant.metric_model_latency().create_alarm(self, "ModelLatencyAlarm",
    threshold=100000,
    evaluation_periods=3
)
```

Java:

```java
import software.amazon.awscdk.services.sagemaker.alpha.*;

EndpointConfig endpointConfig;

Endpoint endpoint = Endpoint.Builder.create(this, "Endpoint").endpointConfig(endpointConfig).build();
IEndpointInstanceProductionVariant productionVariant = endpoint.findInstanceProductionVariant("my-variant");
productionVariant.metricModelLatency().createAlarm(this, "ModelLatencyAlarm", CreateAlarmOptions.builder()
        .threshold(100000)
        .evaluationPeriods(3)
        .build());
```

C# / .NET:

```csharp
using Amazon.CDK.AWS.Sagemaker.Alpha;

EndpointConfig endpointConfig;

var endpoint = new Endpoint(this, "Endpoint", new EndpointProps { EndpointConfig = endpointConfig });
var productionVariant = endpoint.FindInstanceProductionVariant("my-variant");
productionVariant.MetricModelLatency().CreateAlarm(this, "ModelLatencyAlarm", new CreateAlarmOptions {
    Threshold = 100000,
    EvaluationPeriods = 3
});
```

Golang:

```go
import sagemaker "github.com/aws/aws-cdk-go/awscdksagemakeralpha"

var endpointConfig EndpointConfig

endpoint := sagemaker.NewEndpoint(this, jsii.String("Endpoint"), &EndpointProps{
	EndpointConfig: EndpointConfig,
})
productionVariant := endpoint.FindInstanceProductionVariant(jsii.String("my-variant"))
productionVariant.MetricModelLatency().CreateAlarm(this, jsii.String("ModelLatencyAlarm"), &CreateAlarmOptions{
	Threshold: jsii.Number(100000),
	EvaluationPeriods: jsii.Number(3),
})
```

## Types

### Constructs (L2)

- [`Endpoint`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.Endpoint.html) Construct (L2) Defines a SageMaker endpoint.
- [`EndpointConfig`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.EndpointConfig.html) Construct (L2) Defines a SageMaker EndpointConfig.
- [`Model`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.Model.html) Construct (L2) Defines a SageMaker Model.
- [`Pipeline`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.Pipeline.html) Construct (L2) A SageMaker Pipeline.

### Classes

- [`AcceleratorType`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.AcceleratorType.html) Class Supported Elastic Inference (EI) instance types for SageMaker instance-based production variants.
- [`ContainerImage`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ContainerImage.html) Class Constructs for types of container images.
- [`InstanceType`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.InstanceType.html) Class Supported instance types for SageMaker instance-based production variants.
- [`ModelData`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ModelData.html) Class Model data represents the source of model artifacts, which will ultimately be loaded from an S3 location.

### Structs

- [`ContainerDefinition`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ContainerDefinition.html) Struct Describes the container, as part of model definition.
- [`ContainerImageConfig`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ContainerImageConfig.html) Struct The configuration for creating a container image.
- [`EndpointAttributes`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.EndpointAttributes.html) Struct Represents an Endpoint resource defined outside this stack.
- [`EndpointConfigProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.EndpointConfigProps.html) Struct Construction properties for a SageMaker EndpointConfig.
- [`EndpointProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.EndpointProps.html) Struct Construction properties for a SageMaker Endpoint.
- [`InstanceProductionVariantProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.InstanceProductionVariantProps.html) Struct Construction properties for an instance production variant.
- [`InvocationsScalingProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.InvocationsScalingProps.html) Struct Properties for enabling SageMaker Endpoint utilization tracking.
- [`ModelAttributes`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ModelAttributes.html) Struct Represents a Model resource defined outside this stack.
- [`ModelDataConfig`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ModelDataConfig.html) Struct The configuration needed to reference model artifacts.
- [`ModelProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ModelProps.html) Struct Construction properties for a SageMaker Model.
- [`PipelineAttributes`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.PipelineAttributes.html) Struct Attributes for importing a SageMaker Pipeline.
- [`PipelineProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.PipelineProps.html) Struct Properties for defining a SageMaker Pipeline.
- [`ScalableInstanceCountProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ScalableInstanceCountProps.html) Struct The properties of a scalable attribute representing task count.
- [`ServerlessProductionVariantProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ServerlessProductionVariantProps.html) Struct Construction properties for a serverless production variant.

### Interfaces

- [`IEndpoint`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.IEndpoint.html) Interface The Interface for a SageMaker Endpoint resource.
- [`IEndpointConfig`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.IEndpointConfig.html) Interface The interface for a SageMaker EndpointConfig resource.
- [`IEndpointInstanceProductionVariant`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.IEndpointInstanceProductionVariant.html) Interface Represents an instance production variant that has been associated with an endpoint.
- [`IModel`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.IModel.html) Interface Interface that defines a Model resource.

### Enums

- [`InvocationHttpResponseCode`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.InvocationHttpResponseCode.html) Enum HTTP response codes for Endpoint invocations.

### Constructs

- [`ScalableInstanceCount`](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-sagemaker-alpha.ScalableInstanceCount.html) Construct A scalable sagemaker endpoint attribute.
