Skip to content

Commit

Permalink
feat(stepfunctions-tasks): support for SageMaker APIs: CreateEndpoint…
Browse files Browse the repository at this point in the history
…, CreateEndpointConfig, CreateModel, and UpdateEndpoint (#10187)

**Implementation**

Update package `@aws-cdk/aws-stepfunctions-tasks` to include support for SageMaker **CreateEndpoint**, **CreateEndpointConfig**, **CreateModel**, **UpdateEndpoint**    API as per documentation here: https://docs.aws.amazon.com/step-functions/latest/dg/connect-sagemaker.html

Includes support for the following Amazon SageMaker API calls:
* `CreateEndpoint`
* `CreateEndpointConfig`
* `CreateModel`
* `UpdateEndpoint`

Closes #6572
  • Loading branch information
Stacy-D authored Sep 29, 2020
1 parent a8d7282 commit 84738ee
Show file tree
Hide file tree
Showing 16 changed files with 2,070 additions and 10 deletions.
57 changes: 57 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aw
- [SageMaker](#sagemaker)
- [Create Training Job](#create-training-job)
- [Create Transform Job](#create-transform-job)
- [Create Endpoint](#create-endpoint)
- [Create Endpoint Config](#create-endpoint-config)
- [Create Model](#create-model)
- [Update Endpoint](#update-endpoint)
- [SNS](#sns)
- [Step Functions](#step-functions)
- [Start Execution](#start-execution)
Expand Down Expand Up @@ -774,6 +778,59 @@ new sfn.SagemakerTransformTask(this, 'Batch Inference', {

```

### Create Endpoint

You can call the [`CreateEndpoint`](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpoint.html) API from a `Task` state.

```ts
new sfn.SageMakerCreateEndpoint(this, 'SagemakerEndpoint', {
endpointName: sfn.JsonPath.stringAt('$.EndpointName'),
endpointConfigName: sfn.JsonPath.stringAt('$.EndpointConfigName'),
});
```

### Create Endpoint Config

You can call the [`CreateEndpointConfig`](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html) API from a `Task` state.

```ts
new sfn.SageMakerCreateEndpointConfig(this, 'SagemakerEndpointConfig', {
endpointConfigName: 'MyEndpointConfig',
productionVariants: [{
initialInstanceCount: 2,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.M5, ec2.InstanceSize.XLARGE),
modelName: 'MyModel',
variantName: 'awesome-variant',
}],
});
```

### Create Model

You can call the [`CreateModel`](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API from a `Task` state.

```ts
new sfn.SageMakerCreateModel(this, 'Sagemaker', {
modelName: 'MyModel',
primaryContainer: new tasks.ContainerDefinition({
image: tasks.DockerImage.fromJsonExpression(sfn.JsonPath.stringAt('$.Model.imageName')),
mode: tasks.Mode.SINGLE_MODEL,
modelS3Location: tasks.S3Location.fromJsonExpression('$.TrainingJob.ModelArtifacts.S3ModelArtifacts'),
}),
});
```

### Update Endpoint

You can call the [`UpdateEndpoint`](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API from a `Task` state.

```ts
new sfn.SageMakerUpdateEndpoint(this, 'SagemakerEndpoint', {
endpointName: sfn.JsonPath.stringAt('$.Endpoint.Name'),
endpointConfigName: sfn.JsonPath.stringAt('$.Endpoint.EndpointConfig'),
});
```

## SNS

Step Functions supports [Amazon SNS](https://docs.aws.amazon.com/step-functions/latest/dg/connect-sns.html) through the service integration pattern.
Expand Down
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export * from './ecs/run-task';
export * from './sagemaker/base-types';
export * from './sagemaker/create-training-job';
export * from './sagemaker/create-transform-job';
export * from './sagemaker/create-endpoint';
export * from './sagemaker/create-endpoint-config';
export * from './sagemaker/create-model';
export * from './sagemaker/update-endpoint';
export * from './start-execution';
export * from './stepfunctions/start-execution';
export * from './stepfunctions/invoke-activity';
Expand Down
212 changes: 212 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions-tasks/lib/sagemaker/base-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,218 @@ export interface TransformResources {
readonly volumeEncryptionKey?: kms.IKey;
}

/**
* Properties to define a ContainerDefinition
*
* @see https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ContainerDefinition.html
* @experimental
*/
export interface ContainerDefinitionOptions {
/**
* The Amazon EC2 Container Registry (Amazon ECR) path where inference code is stored.
*
* @default - None
*/
readonly image?: DockerImage;
/**
* The environment variables to set in the Docker container
*
* @default - No variables
*/
readonly environmentVariables?: sfn.TaskInput;
/**
* The name or Amazon Resource Name (ARN) of the model package to use to create the model.
*
* @default - None
*/
readonly modelPackageName?: string;
/**
* Defines how many models the container hosts
*
* @default - Mode.SINGLE_MODEL
*/
readonly mode?: Mode;
/**
* This parameter is ignored for models that contain only a PrimaryContainer.
* When a ContainerDefinition is part of an inference pipeline,
* the value of the parameter uniquely identifies the container for the purposes of logging and metrics.
*
* @default - None
*/
readonly containerHostName?: string;
/**
* The S3 path where the model artifacts, which result from model training, are stored.
* This path must point to a single gzip compressed tar archive (.tar.gz suffix).
* The S3 path is required for Amazon SageMaker built-in algorithms, but not if you use your own algorithms.
*
* @default - None
*/
readonly modelS3Location?: S3Location;
}

/**
* Describes the container, as part of model definition.
*
* @see https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ContainerDefinition.html
* @experimental
*/
export class ContainerDefinition implements IContainerDefinition {

constructor(private readonly options: ContainerDefinitionOptions) {}

/**
* Called when the ContainerDefinition type configured on Sagemaker Task
*/
public bind(task: ISageMakerTask): ContainerDefinitionConfig {
return {
parameters: {
ContainerHostname: this.options.containerHostName,
Image: this.options.image?.bind(task).imageUri,
Mode: this.options.mode,
ModelDataUrl: this.options.modelS3Location?.bind(task, { forReading: true }).uri,
ModelPackageName: this.options.modelPackageName,
Environment: this.options.environmentVariables?.value,
},
};
}
}

/**
* Configuration of the container used to host the model
*
* @see https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ContainerDefinition.html
* @experimental
*/
export interface IContainerDefinition {
/**
* Called when the ContainerDefinition is used by a SageMaker task.
*/
bind(task: ISageMakerTask): ContainerDefinitionConfig;
}

/**
* Configuration options for the ContainerDefinition
*/
export interface ContainerDefinitionConfig {
/**
* Additional parameters to pass to the base task
*
* @default - No additional parameters passed
*/
readonly parameters?: { [key: string]: any };
}

/**
* Specifies how many models the container hosts
*
* @experimental
*/
export enum Mode {
/**
* Container hosts a single model
*/
SINGLE_MODEL = 'SingleModel',
/**
* Container hosts multiple models
*
* @see https://docs.aws.amazon.com/sagemaker/latest/dg/multi-model-endpoints.html
*/
MULTI_MODEL = 'MultiModel',
}

/**
* Identifies a model that you want to host and the resources to deploy for hosting it.
*
* @see https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ProductionVariant.html
* @experimental
*/
export interface ProductionVariant {
/**
* The size of the Elastic Inference (EI) instance to use for the production variant.
*
* @default - None
*/
readonly acceleratorType?: AcceleratorType;
/**
* Number of instances to launch initially.
*
* @default - 1
*/
readonly initialInstanceCount?: number;
/**
* Determines initial traffic distribution among all of the models that you specify in the endpoint configuration.
*
* @default - 1.0
*/
readonly initialVariantWeight?: number;
/**
* The ML compute instance type
*/
readonly instanceType: ec2.InstanceType;
/**
* The name of the production variant.
*/
readonly variantName: string;
/**
* The name of the model that you want to host. This is the name that you specified when creating the model.
*/
readonly modelName: string;
}

/**
* The generation of Elastic Inference (EI) instance
*
* @see https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html
* @experimental
*/
export class AcceleratorClass {
/**
* Elastic Inference accelerator 1st generation
*/
public static readonly EIA1 = AcceleratorClass.of('eia1');
/**
* Elastic Inference accelerator 2nd generation
*/
public static readonly EIA2 = AcceleratorClass.of('eia2');
/**
* Custom AcceleratorType
* @param version - Elastic Inference accelerator generation
*/
public static of(version: string) { return new AcceleratorClass(version); }
/**
* @param version - Elastic Inference accelerator generation
*/
private constructor(public readonly version: string) { }
}

/**
* The size of the Elastic Inference (EI) instance to use for the production variant.
* EI instances provide on-demand GPU computing for inference
*
* @see https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html
* @experimental
*/
export class AcceleratorType {
/**
* AcceleratorType
*
* This class takes a combination of a class and size.
*/
public static of(acceleratorClass: AcceleratorClass, instanceSize: ec2.InstanceSize) {
return new AcceleratorType(`ml.${acceleratorClass}.${instanceSize}`);
}

constructor(private readonly instanceTypeIdentifier: string) {
}

/**
* Return the accelerator type as a dotted string
*/
public toString(): string {
return this.instanceTypeIdentifier;
}
}

/**
* Specifies the number of records to include in a mini-batch for an HTTP inference request.
*
Expand Down
Loading

0 comments on commit 84738ee

Please sign in to comment.