Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/sagemaker_endpoint_configuration - add serverless_config argument #25218

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/25218.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_sagemaker_endpoint_configuration: Add `serverless_config` argument
```

```release-note:enhancement
resource/aws_sagemaker_endpoint_configuration: Make `production_variants.initial_instance_count` and `production_variants.instance_type` arguments optional
```
220 changes: 146 additions & 74 deletions internal/service/sagemaker/endpoint_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,75 +106,6 @@ func ResourceEndpointConfiguration() *schema.Resource {
},
},
},

"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validName,
},

"production_variants": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"variant_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"model_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"initial_instance_count": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(1),
},

"instance_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(sagemaker.ProductionVariantInstanceType_Values(), false),
},

"initial_variant_weight": {
Type: schema.TypeFloat,
Optional: true,
ForceNew: true,
ValidateFunc: validation.FloatAtLeast(0),
Default: 1,
},

"accelerator_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(sagemaker.ProductionVariantAcceleratorType_Values(), false),
},
},
},
},

"kms_key_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},

"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),

"data_capture_config": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -270,6 +201,89 @@ func ResourceEndpointConfiguration() *schema.Resource {
},
},
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validName,
},
"kms_key_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
"production_variants": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 10,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"accelerator_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(sagemaker.ProductionVariantAcceleratorType_Values(), false),
},
"initial_instance_count": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(1),
},
"initial_variant_weight": {
Type: schema.TypeFloat,
Optional: true,
ForceNew: true,
ValidateFunc: validation.FloatAtLeast(0),
Default: 1,
},
"instance_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(sagemaker.ProductionVariantInstanceType_Values(), false),
},
"model_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"serverless_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_concurrency": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 200),
},
"memory_size_in_mb": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntInSlice([]int{1024, 2048, 3072, 4096, 5120, 6144}),
},
},
},
},
"variant_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand Down Expand Up @@ -412,9 +426,15 @@ func expandProductionVariants(configured []interface{}) []*sagemaker.ProductionV
data := lRaw.(map[string]interface{})

l := &sagemaker.ProductionVariant{
InstanceType: aws.String(data["instance_type"].(string)),
ModelName: aws.String(data["model_name"].(string)),
InitialInstanceCount: aws.Int64(int64(data["initial_instance_count"].(int))),
ModelName: aws.String(data["model_name"].(string)),
}

if v, ok := data["initial_instance_count"].(int); ok && v > 0 {
l.InitialInstanceCount = aws.Int64(int64(v))
}

if v, ok := data["instance_type"].(string); ok && v != "" {
l.InstanceType = aws.String(v)
}

if v, ok := data["variant_name"]; ok {
Expand All @@ -431,6 +451,10 @@ func expandProductionVariants(configured []interface{}) []*sagemaker.ProductionV
l.AcceleratorType = aws.String(v)
}

if v, ok := data["serverless_config"].([]interface{}); ok && len(v) > 0 {
l.ServerlessConfig = expandServerlessConfig(v)
}

containers = append(containers, l)
}

Expand All @@ -443,13 +467,23 @@ func flattenProductionVariants(list []*sagemaker.ProductionVariant) []map[string
for _, i := range list {
l := map[string]interface{}{
"accelerator_type": aws.StringValue(i.AcceleratorType),
"initial_instance_count": aws.Int64Value(i.InitialInstanceCount),
"initial_variant_weight": aws.Float64Value(i.InitialVariantWeight),
"instance_type": aws.StringValue(i.InstanceType),
"model_name": aws.StringValue(i.ModelName),
"variant_name": aws.StringValue(i.VariantName),
}

if i.InitialInstanceCount != nil {
l["initial_instance_count"] = aws.Int64Value(i.InitialInstanceCount)
}

if i.InstanceType != nil {
l["instance_type"] = aws.StringValue(i.InstanceType)
}

if i.ServerlessConfig != nil {
l["serverless_config"] = flattenServerlessConfig(i.ServerlessConfig)
}

result = append(result, l)
}
return result
Expand Down Expand Up @@ -646,6 +680,26 @@ func expandEndpointConfigNotificationConfig(configured []interface{}) *sagemaker
return c
}

func expandServerlessConfig(configured []interface{}) *sagemaker.ProductionVariantServerlessConfig {
if len(configured) == 0 {
return nil
}

m := configured[0].(map[string]interface{})

c := &sagemaker.ProductionVariantServerlessConfig{}

if v, ok := m["max_concurrency"].(int); ok {
c.MaxConcurrency = aws.Int64(int64(v))
}

if v, ok := m["memory_size_in_mb"].(int); ok {
c.MemorySizeInMB = aws.Int64(int64(v))
}

return c
}

func flattenEndpointConfigAsyncInferenceConfig(config *sagemaker.AsyncInferenceConfig) []map[string]interface{} {
if config == nil {
return []map[string]interface{}{}
Expand Down Expand Up @@ -715,3 +769,21 @@ func flattenEndpointConfigNotificationConfig(config *sagemaker.AsyncInferenceNot

return []map[string]interface{}{cfg}
}

func flattenServerlessConfig(config *sagemaker.ProductionVariantServerlessConfig) []map[string]interface{} {
if config == nil {
return []map[string]interface{}{}
}

cfg := map[string]interface{}{}

if config.MaxConcurrency != nil {
cfg["max_concurrency"] = aws.Int64Value(config.MaxConcurrency)
}

if config.MemorySizeInMB != nil {
cfg["memory_size_in_mb"] = aws.Int64Value(config.MemorySizeInMB)
}

return []map[string]interface{}{cfg}
}
49 changes: 48 additions & 1 deletion internal/service/sagemaker/endpoint_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAccSageMakerEndpointConfiguration_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "production_variants.0.initial_instance_count", "2"),
resource.TestCheckResourceAttr(resourceName, "production_variants.0.instance_type", "ml.t2.medium"),
resource.TestCheckResourceAttr(resourceName, "production_variants.0.initial_variant_weight", "1"),
resource.TestCheckResourceAttr(resourceName, "production_variants.0.code_dump_config.#", "0"),
resource.TestCheckResourceAttr(resourceName, "production_variants.0.serverless_config.#", "0"),
resource.TestCheckResourceAttr(resourceName, "data_capture_config.#", "0"),
resource.TestCheckResourceAttr(resourceName, "async_inference_config.#", "0"),
),
Expand All @@ -49,6 +49,35 @@ func TestAccSageMakerEndpointConfiguration_basic(t *testing.T) {
})
}

func TestAccSageMakerEndpointConfiguration_ProductionVariants_serverless(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_sagemaker_endpoint_configuration.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, sagemaker.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckEndpointConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccEndpointConfigurationConfig_serverless(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckEndpointConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "production_variants.#", "1"),
resource.TestCheckResourceAttr(resourceName, "production_variants.0.serverless_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "production_variants.0.serverless_config.0.max_concurrency", "1"),
resource.TestCheckResourceAttr(resourceName, "production_variants.0.serverless_config.0.memory_size_in_mb", "1024"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccSageMakerEndpointConfiguration_ProductionVariants_initialVariantWeight(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_sagemaker_endpoint_configuration.test"
Expand Down Expand Up @@ -753,3 +782,21 @@ resource "aws_sagemaker_endpoint_configuration" "test" {
}
`, rName)
}

func testAccEndpointConfigurationConfig_serverless(rName string) string {
return testAccEndpointConfigurationConfig_Base(rName) + fmt.Sprintf(`
resource "aws_sagemaker_endpoint_configuration" "test" {
name = %q

production_variants {
variant_name = "variant-1"
model_name = aws_sagemaker_model.test.name

serverless_config {
max_concurrency = 1
memory_size_in_mb = 1024
}
}
}
`, rName)
}
Loading