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_model - add inference_execution_config #20066

Merged
merged 4 commits into from
Jul 28, 2021
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
3 changes: 3 additions & 0 deletions .changelog/20066.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_sagemaker_model: Add `inference_execution_config`.
```
227 changes: 130 additions & 97 deletions aws/resource_aws_sagemaker_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,8 @@ func resourceAwsSagemakerModel() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

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

"primary_container": {
"container": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -49,36 +39,19 @@ func resourceAwsSagemakerModel() *schema.Resource {
ForceNew: true,
ValidateFunc: validateSagemakerName,
},

"image": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSagemakerImage,
},

"mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: sagemaker.ContainerModeSingleModel,
ValidateFunc: validation.StringInSlice(sagemaker.ContainerMode_Values(), false),
},

"model_data_url": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateSagemakerModelDataUrl,
},

"environment": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
ValidateFunc: validateSagemakerEnvironment,
Elem: &schema.Schema{Type: schema.TypeString},
},
"image": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSagemakerImage,
},
"image_config": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -94,46 +67,59 @@ func resourceAwsSagemakerModel() *schema.Resource {
},
},
},
"mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: sagemaker.ContainerModeSingleModel,
ValidateFunc: validation.StringInSlice(sagemaker.ContainerMode_Values(), false),
},
"model_data_url": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateSagemakerModelDataUrl,
},
},
},
},

"vpc_config": {
Type: schema.TypeList,
"enable_network_isolation": {
Type: schema.TypeBool,
Optional: true,
MaxItems: 1,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnets": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"security_group_ids": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},

"execution_role_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},

"enable_network_isolation": {
Type: schema.TypeBool,
"inference_execution_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mode": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(sagemaker.InferenceExecutionMode_Values(), false),
},
},
},
},

"container": {
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validateSagemakerName,
},
"primary_container": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -143,36 +129,19 @@ func resourceAwsSagemakerModel() *schema.Resource {
ForceNew: true,
ValidateFunc: validateSagemakerName,
},

"image": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSagemakerImage,
},

"mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: sagemaker.ContainerModeSingleModel,
ValidateFunc: validation.StringInSlice(sagemaker.ContainerMode_Values(), false),
},

"model_data_url": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateSagemakerModelDataUrl,
},

"environment": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
ValidateFunc: validateSagemakerEnvironment,
Elem: &schema.Schema{Type: schema.TypeString},
},
"image": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSagemakerImage,
},
"image_config": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -188,12 +157,46 @@ func resourceAwsSagemakerModel() *schema.Resource {
},
},
},
"mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: sagemaker.ContainerModeSingleModel,
ValidateFunc: validation.StringInSlice(sagemaker.ContainerMode_Values(), false),
},
"model_data_url": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateSagemakerModelDataUrl,
},
},
},
},

"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
"vpc_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnets": {
Type: schema.TypeSet,
Required: true,
MaxItems: 16,
Elem: &schema.Schema{Type: schema.TypeString},
},
"security_group_ids": {
Type: schema.TypeSet,
Required: true,
MaxItems: 5,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},

CustomizeDiff: SetTagsDiff,
Expand Down Expand Up @@ -240,6 +243,10 @@ func resourceAwsSagemakerModelCreate(d *schema.ResourceData, meta interface{}) e
createOpts.EnableNetworkIsolation = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("inference_execution_config"); ok {
createOpts.InferenceExecutionConfig = expandSagemakerModelInferenceExecutionConfig(v.([]interface{}))
}

log.Printf("[DEBUG] Sagemaker model create config: %#v", *createOpts)
_, err := retryOnAwsCode("ValidationException", func() (interface{}, error) {
return conn.CreateModel(createOpts)
Expand Down Expand Up @@ -285,29 +292,29 @@ func resourceAwsSagemakerModelRead(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("error reading Sagemaker model %s: %w", d.Id(), err)
}

if err := d.Set("arn", model.ModelArn); err != nil {
return fmt.Errorf("unable to set arn for sagemaker model %q: %+v", d.Id(), err)
}
if err := d.Set("name", model.ModelName); err != nil {
return err
}
if err := d.Set("execution_role_arn", model.ExecutionRoleArn); err != nil {
return err
}
if err := d.Set("enable_network_isolation", model.EnableNetworkIsolation); err != nil {
return err
}
arn := aws.StringValue(model.ModelArn)
d.Set("arn", arn)
d.Set("name", model.ModelName)
d.Set("execution_role_arn", model.ExecutionRoleArn)
d.Set("enable_network_isolation", model.EnableNetworkIsolation)

if err := d.Set("primary_container", flattenContainer(model.PrimaryContainer)); err != nil {
return err
return fmt.Errorf("error setting primary_container: %w", err)
}

if err := d.Set("container", flattenContainers(model.Containers)); err != nil {
return err
return fmt.Errorf("error setting container: %w", err)
}

if err := d.Set("vpc_config", flattenSageMakerVpcConfigResponse(model.VpcConfig)); err != nil {
return fmt.Errorf("error setting vpc_config: %w", err)
}

tags, err := keyvaluetags.SagemakerListTags(conn, aws.StringValue(model.ModelArn))
if err := d.Set("inference_execution_config", flattenSagemakerModelInferenceExecutionConfig(model.InferenceExecutionConfig)); err != nil {
return fmt.Errorf("error setting inference_execution_config: %w", err)
}

tags, err := keyvaluetags.SagemakerListTags(conn, arn)
if err != nil {
return fmt.Errorf("error listing tags for Sagemaker Model (%s): %w", d.Id(), err)
}
Expand Down Expand Up @@ -480,3 +487,29 @@ func flattenContainers(containers []*sagemaker.ContainerDefinition) []interface{
}
return fContainers
}

func expandSagemakerModelInferenceExecutionConfig(l []interface{}) *sagemaker.InferenceExecutionConfig {
if len(l) == 0 {
return nil
}

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

config := &sagemaker.InferenceExecutionConfig{
Mode: aws.String(m["mode"].(string)),
}

return config
}

func flattenSagemakerModelInferenceExecutionConfig(config *sagemaker.InferenceExecutionConfig) []interface{} {
if config == nil {
return []interface{}{}
}

cfg := make(map[string]interface{})

cfg["mode"] = aws.StringValue(config.Mode)

return []interface{}{cfg}
}
Loading