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

F/Add model_package_name for aws_sagemaker_model #31755

Merged
3 changes: 3 additions & 0 deletions .changelog/31755.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_sagemaker_model: Add model_package_name argument
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
```
32 changes: 27 additions & 5 deletions internal/service/sagemaker/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func ResourceModel() *schema.Resource {
},
"image": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: validImage,
},
Expand Down Expand Up @@ -106,6 +106,12 @@ func ResourceModel() *schema.Resource {
ForceNew: true,
ValidateFunc: validModelDataURL,
},
"model_package_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
},
},
},
Expand Down Expand Up @@ -164,7 +170,7 @@ func ResourceModel() *schema.Resource {
},
"image": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: validImage,
},
Expand Down Expand Up @@ -211,6 +217,12 @@ func ResourceModel() *schema.Resource {
ForceNew: true,
ValidateFunc: validModelDataURL,
},
"model_package_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
},
},
},
Expand Down Expand Up @@ -404,8 +416,10 @@ func resourceModelDelete(ctx context.Context, d *schema.ResourceData, meta inter
}

func expandContainer(m map[string]interface{}) *sagemaker.ContainerDefinition {
container := sagemaker.ContainerDefinition{
Image: aws.String(m["image"].(string)),
container := sagemaker.ContainerDefinition{}

if v, ok := m["image"]; ok && v.(string) != "" {
container.Image = aws.String(v.(string))
}

if v, ok := m["mode"]; ok && v.(string) != "" {
Expand All @@ -418,6 +432,9 @@ func expandContainer(m map[string]interface{}) *sagemaker.ContainerDefinition {
if v, ok := m["model_data_url"]; ok && v.(string) != "" {
container.ModelDataUrl = aws.String(v.(string))
}
if v, ok := m["model_package_name"]; ok && v.(string) != "" {
container.ModelPackageName = aws.String(v.(string))
}
if v, ok := m["environment"].(map[string]interface{}); ok && len(v) > 0 {
container.Environment = flex.ExpandStringMap(v)
}
Expand Down Expand Up @@ -478,7 +495,9 @@ func flattenContainer(container *sagemaker.ContainerDefinition) []interface{} {

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

cfg["image"] = aws.StringValue(container.Image)
if container.Image != nil {
cfg["image"] = aws.StringValue(container.Image)
}

if container.Mode != nil {
cfg["mode"] = aws.StringValue(container.Mode)
Expand All @@ -490,6 +509,9 @@ func flattenContainer(container *sagemaker.ContainerDefinition) []interface{} {
if container.ModelDataUrl != nil {
cfg["model_data_url"] = aws.StringValue(container.ModelDataUrl)
}
if container.ModelPackageName != nil {
cfg["model_package_name"] = aws.StringValue(container.ModelPackageName)
}
if container.Environment != nil {
cfg["environment"] = aws.StringValueMap(container.Environment)
}
Expand Down
68 changes: 68 additions & 0 deletions internal/service/sagemaker/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ func TestAccSageMakerModel_primaryContainerModeSingle(t *testing.T) {
})
}

func TestAccSageMakerModel_primaryContainerModelPackageName(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_sagemaker_model.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, sagemaker.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckModelDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccModelConfig_primaryContainerPackageName(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckModelExists(ctx, resourceName),
resource.TestCheckResourceAttrSet(resourceName, "primary_container.0.model_package_name"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccSageMakerModel_containers(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -618,6 +645,47 @@ resource "aws_s3_object" "test" {
`, rName))
}

func testAccModelConfig_primaryContainerPackageName(rName string) string {
return acctest.ConfigCompose(testAccModelConfigBase(rName), fmt.Sprintf(`
data "aws_region" "current" {}
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved

locals {
region_account_map = {
us-east-1 = "865070037744"
us-east-2 = "057799348421"
us-west-1 = "382657785993"
us-west-2 = "594846645681"
ca-central-1 = "470592106596"
eu-central-1 = "446921602837"
eu-west-1 = "985815980388"
eu-west-2 = "856760150666"
eu-west-3 = "843114510376"
eu-north-1 = "136758871317"
ap-southeast-1 = "192199979996"
ap-southeast-2 = "666831318237"
ap-northeast-2 = "745090734665"
ap-northeast-1 = "977537786026"
ap-south-1 = "077584701553"
sa-east-1 = "270155090741"
}
account = region_account_map[data.aws_region.current]
model_package_name = format(
"arn:aws:sagemaker:%%s:%%s:model-package/gpt-2-1584040650-de7f6ab78d68d7fdf5f4f39a559d05ac",
data.aws_region.current,
account
)
}

resource "aws_sagemaker_model" "test" {
name = %[1]q
execution_role_arn = aws_iam_role.test.arn
primary_container {
model_package_name = local.model_package_name
}
}
`, rName))
}

func testAccModelConfig_primaryContainerHostname(rName string) string {
return acctest.ConfigCompose(testAccModelConfigBase(rName), fmt.Sprintf(`
resource "aws_sagemaker_model" "test" {
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/sagemaker_model.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ The following arguments are supported:

The `primary_container` and `container` block both support:

* `image` - (Required) The registry path where the inference code image is stored in Amazon ECR.
* `image` - (Optional) The registry path where the inference code image is stored in Amazon ECR.
* `mode` - (Optional) The container hosts value `SingleModel/MultiModel`. The default value is `SingleModel`.
* `model_data_url` - (Optional) The URL for the S3 location where model artifacts are stored.
* `model_package_name` - (Optional) The Amazon Resource Name (ARN) of the model package to use to create the model.
* `container_hostname` - (Optional) The DNS host name for the container.
* `environment` - (Optional) Environment variables for the Docker container.
A list of key value pairs.
Expand Down