Skip to content

Commit

Permalink
.NET (v3): Adding SageMaker workflow example (#5022)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlhagerm authored Jul 10, 2023
1 parent 5b8b612 commit d886402
Show file tree
Hide file tree
Showing 24 changed files with 2,280 additions and 0 deletions.
113 changes: 113 additions & 0 deletions .doc_gen/metadata/sagemaker_metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,89 @@
# zexi 0.4.0
sagemaker_Hello:
title: Hello &SM;
title_abbrev: Hello &SM;
synopsis: get started using &SM;.
category: Hello
languages:
.NET:
versions:
- sdk_version: 3
github: dotnetv3/SageMaker
sdkguide:
excerpts:
- description:
snippet_tags:
- SageMaker.dotnetv3.HelloSageMaker
services:
sagemaker: {ListNotebookInstances}
sagemaker_CreatePipeline:
title: Create or update a pipeline in &SM; using an &AWS; SDK
title_abbrev: Create a pipeline
synopsis: create or update a pipeline in &SM;.
category:
languages:
.NET:
versions:
- sdk_version: 3
github: dotnetv3/SageMaker
sdkguide:
excerpts:
- description:
snippet_tags:
- SageMaker.dotnetv3.CreatePipeline
services:
sagemaker: {CreatePipeline, UpdatePipeline}
sagemaker_ExecutePipeline:
title: Execute a pipeline in &SM; using an &AWS; SDK
title_abbrev: Execute a pipeline
synopsis: start a pipeline execution in &SM;.
category:
languages:
.NET:
versions:
- sdk_version: 3
github: dotnetv3/SageMaker
sdkguide:
excerpts:
- description:
snippet_tags:
- SageMaker.dotnetv3.ExecutePipeline
services:
sagemaker: {StartPipelineExecution}
sagemaker_DeletePipeline:
title: Delete a pipeline in &SM; using an &AWS; SDK
title_abbrev: Delete a pipeline
synopsis: delete a pipeline in &SM;.
category:
languages:
.NET:
versions:
- sdk_version: 3
github: dotnetv3/SageMaker
sdkguide:
excerpts:
- description:
snippet_tags:
- SageMaker.dotnetv3.DeletePipeline
services:
sagemaker: {DeletePipeline}
sagemaker_DescribePipelineExecution:
title: Describe a pipeline execution in &SM; using an &AWS; SDK
title_abbrev: Describe a pipeline execution
synopsis: describe a pipeline execution in &SM;.
category:
languages:
.NET:
versions:
- sdk_version: 3
github: dotnetv3/SageMaker
sdkguide:
excerpts:
- description:
snippet_tags:
- SageMaker.dotnetv3.DescribePipelineExecution
services:
sagemaker: {DescribePipelineExecution}
sagemaker_ListNotebookInstances:
title: List the &SM; notebook instances using an &AWS; SDK
title_abbrev: List notebook instances
Expand Down Expand Up @@ -210,3 +295,31 @@ sagemaker_Scenario_GettingStarted:
- sgm.abapv1.getting_started_with_sgm
services:
sagemaker: {CreateTrainingJob, DescribeTrainingJob, CreateModel, CreateEndpointConfig, CreateEndpoint, DescribeEndpoint, DeleteEndpoint, DeleteEndpointConfig, DeleteModel}
sagemaker_Scenario_Pipelines:
title: Get started with &SM; geospatial jobs in a pipeline using an &AWS; SDK
title_abbrev: Get started with geospatial jobs and pipelines
synopsis_list:
- Set up resources for a pipeline.
- Set up a pipeline that executes a geospatial job.
- Start a pipeline execution.
- Monitor the status of the execution.
- View the output of the pipeline.
- Clean up resources.
category: Scenarios
languages:
.NET:
versions:
- sdk_version: 3
github: dotnetv3/SageMaker
excerpts:
- description: Create a class that wraps &SM; operations.
snippet_tags:
- SageMaker.dotnetv3.SagemakerWrapper
- description: Create a function that handles callbacks from the &SM; pipeline.
snippet_tags:
- SageMaker.dotnetv3.SagemakerLambda
- description: Run an interactive scenario at a command prompt.
snippet_tags:
- SageMaker.dotnetv3.SagemakerPipelineScenario
services:
sagemaker: {CreatePipeline, UpdatePipeline, StartPipelineExecution, DescribePipelineExecution, DeletePipeline}
43 changes: 43 additions & 0 deletions dotnetv3/SageMaker/Actions/HelloSageMaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[SageMaker.dotnetv3.HelloSageMaker]

using Amazon.SageMaker;
using Amazon.SageMaker.Model;

namespace SageMakerActions;

public static class HelloSageMaker
{
static async Task Main(string[] args)
{
var sageMakerClient = new AmazonSageMakerClient();

Console.WriteLine($"Hello Amazon SageMaker! Let's list some of your notebook instances:");
Console.WriteLine();

// You can use await and any of the async methods to get a response.
// Let's get the first five notebook instances.
var response = await sageMakerClient.ListNotebookInstancesAsync(
new ListNotebookInstancesRequest()
{
MaxResults = 5
});

if (!response.NotebookInstances.Any())
{
Console.WriteLine($"No notebook instances found.");
Console.WriteLine("See https://docs.aws.amazon.com/sagemaker/latest/dg/howitworks-create-ws.html to create one.");
}

foreach (var notebookInstance in response.NotebookInstances)
{
Console.WriteLine($"\tInstance: {notebookInstance.NotebookInstanceName}");
Console.WriteLine($"\tArn: {notebookInstance.NotebookInstanceArn}");
Console.WriteLine($"\tCreation Date: {notebookInstance.CreationTime.ToShortDateString()}");
Console.WriteLine();
}
}
}
// snippet-end:[SageMaker.dotnetv3.HelloSageMaker]
20 changes: 20 additions & 0 deletions dotnetv3/SageMaker/Actions/SageMakerActions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.EC2" Version="3.7.136.1" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.2" />
<PackageReference Include="AWSSDK.IdentityManagement" Version="3.7.101" />
<PackageReference Include="AWSSDK.SageMaker" Version="3.7.134.2" />
<PackageReference Include="AWSSDK.SageMakerGeospatial" Version="3.7.102.11" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
</ItemGroup>

</Project>
162 changes: 162 additions & 0 deletions dotnetv3/SageMaker/Actions/SageMakerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[SageMaker.dotnetv3.SagemakerWrapper]

using System.Text.Json;
using Amazon.SageMaker;
using Amazon.SageMaker.Model;
using Amazon.SageMakerGeospatial.Model;
using Amazon.SageMakerGeospatial;

namespace SageMakerActions;

/// <summary>
/// Wrapper class for Amazon SageMaker actions and logic.
/// </summary>
public class SageMakerWrapper
{
private readonly IAmazonSageMaker _amazonSageMaker;
public SageMakerWrapper(IAmazonSageMaker amazonSageMaker)
{
_amazonSageMaker = amazonSageMaker;
}

// snippet-start:[SageMaker.dotnetv3.CreatePipeline]
/// <summary>
/// Create a pipeline from a JSON definition, or update it if the pipeline already exists.
/// </summary>
/// <returns>The Amazon Resource Name (ARN) of the pipeline.</returns>
public async Task<string> SetupPipeline(string pipelineJson, string roleArn, string name, string description, string displayName)
{
try
{
var updateResponse = await _amazonSageMaker.UpdatePipelineAsync(
new UpdatePipelineRequest()
{
PipelineDefinition = pipelineJson,
PipelineDescription = description,
PipelineDisplayName = displayName,
PipelineName = name,
RoleArn = roleArn
});
return updateResponse.PipelineArn;
}
catch (Amazon.SageMaker.Model.ResourceNotFoundException)
{
var createResponse = await _amazonSageMaker.CreatePipelineAsync(
new CreatePipelineRequest()
{
PipelineDefinition = pipelineJson,
PipelineDescription = description,
PipelineDisplayName = displayName,
PipelineName = name,
RoleArn = roleArn
});

return createResponse.PipelineArn;
}
}
// snippet-end:[SageMaker.dotnetv3.CreatePipeline]

// snippet-start:[SageMaker.dotnetv3.ExecutePipeline]
/// <summary>
/// Run a pipeline with input and output file locations.
/// </summary>
/// <param name="queueUrl">The URL for the queue to use for pipeline callbacks.</param>
/// <param name="inputLocationUrl">The input location in Amazon Simple Storage Service (Amazon S3).</param>
/// <param name="outputLocationUrl">The output location in Amazon S3.</param>
/// <param name="pipelineName">The name of the pipeline.</param>
/// <param name="executionRoleArn">The ARN of the role.</param>
/// <returns>The ARN of the pipeline run.</returns>
public async Task<string> ExecutePipeline(
string queueUrl,
string inputLocationUrl,
string outputLocationUrl,
string pipelineName,
string executionRoleArn)
{
var inputConfig = new VectorEnrichmentJobInputConfig()
{
DataSourceConfig = new()
{
S3Data = new VectorEnrichmentJobS3Data()
{
S3Uri = inputLocationUrl
}
},
DocumentType = VectorEnrichmentJobDocumentType.CSV
};

var exportConfig = new ExportVectorEnrichmentJobOutputConfig()
{
S3Data = new VectorEnrichmentJobS3Data()
{
S3Uri = outputLocationUrl
}
};

var jobConfig = new VectorEnrichmentJobConfig()
{
ReverseGeocodingConfig = new ReverseGeocodingConfig()
{
XAttributeName = "Longitude",
YAttributeName = "Latitude"
}
};

var startExecutionResponse = await _amazonSageMaker.StartPipelineExecutionAsync(
new StartPipelineExecutionRequest()
{
PipelineName = pipelineName,
PipelineExecutionDisplayName = pipelineName + "-example-execution",
PipelineParameters = new List<Parameter>()
{
new Parameter() { Name = "parameter_execution_role", Value = executionRoleArn },
new Parameter() { Name = "parameter_queue_url", Value = queueUrl },
new Parameter() { Name = "parameter_vej_input_config", Value = JsonSerializer.Serialize(inputConfig) },
new Parameter() { Name = "parameter_vej_export_config", Value = JsonSerializer.Serialize(exportConfig) },
new Parameter() { Name = "parameter_step_1_vej_config", Value = JsonSerializer.Serialize(jobConfig) }
}
});
return startExecutionResponse.PipelineExecutionArn;
}
// snippet-end:[SageMaker.dotnetv3.ExecutePipeline]

// snippet-start:[SageMaker.dotnetv3.DescribePipelineExecution]
/// <summary>
/// Check the status of a run.
/// </summary>
/// <param name="pipelineExecutionArn">The ARN.</param>
/// <returns>The status of the pipeline.</returns>
public async Task<PipelineExecutionStatus> CheckPipelineExecutionStatus(string pipelineExecutionArn)
{
var describeResponse = await _amazonSageMaker.DescribePipelineExecutionAsync(
new DescribePipelineExecutionRequest()
{
PipelineExecutionArn = pipelineExecutionArn
});

return describeResponse.PipelineExecutionStatus;
}
// snippet-end:[SageMaker.dotnetv3.DescribePipelineExecution]

// snippet-start:[SageMaker.dotnetv3.DeletePipeline]
/// <summary>
/// Delete a SageMaker pipeline by name.
/// </summary>
/// <param name="pipelineName">The name of the pipeline to delete.</param>
/// <returns>The ARN of the pipeline.</returns>
public async Task<string> DeletePipelineByName(string pipelineName)
{
var deleteResponse = await _amazonSageMaker.DeletePipelineAsync(
new DeletePipelineRequest()
{
PipelineName = pipelineName
});

return deleteResponse.PipelineArn;
}
// snippet-end:[SageMaker.dotnetv3.DeletePipeline]
}
// snippet-end:[SageMaker.dotnetv3.SagemakerWrapper]
Binary file added dotnetv3/SageMaker/Images/Pipeline.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d886402

Please sign in to comment.