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_space - support app lifecycle management #39800

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/39800.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_sagemaker_space: Add `space_settings.code_editor_app_settings.app_lifecycle_management`, `space_settings.jupyter_lab_app_settings.app_lifecycle_management` Arguments
```
1 change: 1 addition & 0 deletions internal/service/sagemaker/sagemaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func TestAccSageMaker_serial(t *testing.T) {
"kernelGatewayAppSettings_imageConfig": testAccSpace_kernelGatewayAppSettings_imageconfig,
"jupyterServerAppSettings": testAccSpace_jupyterServerAppSettings,
"jupyterLabAppSettings": testAccSpace_jupyterLabAppSettings,
"jupyterLabAppSettingsLifecycle": testAccSpace_jupyterLabAppSettingsAppLifecycle,
"codeEditorAppSettings": testAccSpace_codeEditorAppSettings,
"storageSettings": testAccSpace_storageSettings,
"customFileSystem": testAccSpace_customFileSystem,
Expand Down
122 changes: 122 additions & 0 deletions internal/service/sagemaker/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ func resourceSpace() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"app_lifecycle_management": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"idle_settings": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"idle_timeout_in_minutes": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(60, 525600),
},
},
},
},
},
},
},
"default_resource_spec": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -163,6 +186,29 @@ func resourceSpace() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"app_lifecycle_management": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"idle_settings": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"idle_timeout_in_minutes": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(60, 525600),
},
},
},
},
},
},
},
"code_repository": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -687,6 +733,10 @@ func expandSpaceCodeEditorAppSettings(l []interface{}) *awstypes.SpaceCodeEditor

config := &awstypes.SpaceCodeEditorAppSettings{}

if v, ok := m["app_lifecycle_management"].([]interface{}); ok && len(v) > 0 {
config.AppLifecycleManagement = expandSpaceAppLifecycleManagement(v)
}

if v, ok := m["default_resource_spec"].([]interface{}); ok && len(v) > 0 {
config.DefaultResourceSpec = expandResourceSpec(v)
}
Expand All @@ -701,6 +751,10 @@ func flattenSpaceCodeEditorAppSettings(config *awstypes.SpaceCodeEditorAppSettin

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

if config.AppLifecycleManagement != nil {
m["app_lifecycle_management"] = flattenSpaceAppLifecycleManagement(config.AppLifecycleManagement)
}

if config.DefaultResourceSpec != nil {
m["default_resource_spec"] = flattenResourceSpec(config.DefaultResourceSpec)
}
Expand All @@ -717,6 +771,10 @@ func expandSpaceJupyterLabAppSettings(l []interface{}) *awstypes.SpaceJupyterLab

config := &awstypes.SpaceJupyterLabAppSettings{}

if v, ok := m["app_lifecycle_management"].([]interface{}); ok && len(v) > 0 {
config.AppLifecycleManagement = expandSpaceAppLifecycleManagement(v)
}

if v, ok := m["code_repository"].(*schema.Set); ok && v.Len() > 0 {
config.CodeRepositories = expandCodeRepositories(v.List())
}
Expand All @@ -735,6 +793,10 @@ func flattenSpaceJupyterLabAppSettings(config *awstypes.SpaceJupyterLabAppSettin

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

if config.AppLifecycleManagement != nil {
m["app_lifecycle_management"] = flattenSpaceAppLifecycleManagement(config.AppLifecycleManagement)
}

if config.CodeRepositories != nil {
m["code_repository"] = flattenCodeRepositories(config.CodeRepositories)
}
Expand Down Expand Up @@ -951,3 +1013,63 @@ func flattenSpaceSharingSettings(config *awstypes.SpaceSharingSettings) []map[st

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

func expandSpaceAppLifecycleManagement(l []interface{}) *awstypes.SpaceAppLifecycleManagement {
if len(l) == 0 || l[0] == nil {
return nil
}

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

config := &awstypes.SpaceAppLifecycleManagement{}

if v, ok := m["idle_settings"].([]interface{}); ok && len(v) > 0 {
config.IdleSettings = expandSpaceIdleSettings(v)
}

return config
}

func expandSpaceIdleSettings(l []interface{}) *awstypes.SpaceIdleSettings {
if len(l) == 0 || l[0] == nil {
return nil
}

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

config := &awstypes.SpaceIdleSettings{}

if v, ok := m["idle_timeout_in_minutes"].(int); ok {
config.IdleTimeoutInMinutes = aws.Int32(int32(v))
}

return config
}

func flattenSpaceAppLifecycleManagement(config *awstypes.SpaceAppLifecycleManagement) []map[string]interface{} {
if config == nil {
return []map[string]interface{}{}
}

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

if config.IdleSettings != nil {
m["idle_settings"] = flattenSpaceIdleSettings(config.IdleSettings)
}

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

func flattenSpaceIdleSettings(config *awstypes.SpaceIdleSettings) []map[string]interface{} {
if config == nil {
return []map[string]interface{}{}
}

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

if config.IdleTimeoutInMinutes != nil {
m["idle_timeout_in_minutes"] = aws.ToInt32(config.IdleTimeoutInMinutes)
}

return []map[string]interface{}{m}
}
75 changes: 75 additions & 0 deletions internal/service/sagemaker/space_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,45 @@ func testAccSpace_jupyterLabAppSettings(t *testing.T) {
})
}

func testAccSpace_jupyterLabAppSettingsAppLifecycle(t *testing.T) {
ctx := acctest.Context(t)
var domain sagemaker.DescribeSpaceOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_sagemaker_space.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.SageMakerServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckSpaceDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccSpaceConfig_jupyterLabAppSettingsLifecycle(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSpaceExists(ctx, resourceName, &domain),
resource.TestCheckResourceAttr(resourceName, "space_settings.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "space_settings.0.app_type", "JupyterLab"),
resource.TestCheckResourceAttr(resourceName, "space_settings.0.jupyter_lab_app_settings.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "space_settings.0.jupyter_lab_app_settings.0.default_resource_spec.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "space_settings.0.jupyter_lab_app_settings.0.default_resource_spec.0.instance_type", "ml.t3.micro"),
resource.TestCheckResourceAttr(resourceName, "space_settings.0.jupyter_lab_app_settings.0.app_lifecycle_management.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "space_settings.0.jupyter_lab_app_settings.0.app_lifecycle_management.0.idle_settings.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "space_settings.0.jupyter_lab_app_settings.0.app_lifecycle_management.0.idle_settings.0.idle_timeout_in_minutes", "60"),
resource.TestCheckResourceAttr(resourceName, "space_sharing_settings.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "space_sharing_settings.0.sharing_type", "Private"),
resource.TestCheckResourceAttr(resourceName, "ownership_settings.#", acctest.Ct1),
resource.TestCheckResourceAttrPair(resourceName, "ownership_settings.0.owner_user_profile_name", "aws_sagemaker_user_profile.test", "user_profile_name"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccSpace_jupyterServerAppSettings(t *testing.T) {
ctx := acctest.Context(t)
var domain sagemaker.DescribeSpaceOutput
Expand Down Expand Up @@ -618,6 +657,42 @@ resource "aws_sagemaker_space" "test" {
`, rName))
}

func testAccSpaceConfig_jupyterLabAppSettingsLifecycle(rName string) string {
return acctest.ConfigCompose(testAccDomainConfig_jupyterLabAppSettingsAppLifecycle(rName), fmt.Sprintf(`
resource "aws_sagemaker_user_profile" "test" {
domain_id = aws_sagemaker_domain.test.id
user_profile_name = "%[1]s-2"
}

resource "aws_sagemaker_space" "test" {
domain_id = aws_sagemaker_domain.test.id
space_name = %[1]q

space_sharing_settings {
sharing_type = "Private"
}

ownership_settings {
owner_user_profile_name = aws_sagemaker_user_profile.test.user_profile_name
}

space_settings {
app_type = "JupyterLab"
jupyter_lab_app_settings {
app_lifecycle_management {
idle_settings {
idle_timeout_in_minutes = 60
}
}
default_resource_spec {
instance_type = "ml.t3.micro"
}
}
}
}
`, rName))
}

func testAccSpaceConfig_jupyterServerAppSettings(rName string) string {
return acctest.ConfigCompose(testAccSpaceConfig_base(rName), fmt.Sprintf(`
resource "aws_sagemaker_space" "test" {
Expand Down
15 changes: 15 additions & 0 deletions website/docs/r/sagemaker_space.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ The `space_sharing_settings` block supports the following argument:

The `code_editor_app_settings` block supports the following argument:

* `app_lifecycle_management` - (Optional) Settings that are used to configure and manage the lifecycle of JupyterLab applications in a space. See [`app_lifecycle_management` Block](#app_lifecycle_management-block) below.
* `code_repository` - (Optional) A list of Git repositories that SageMaker automatically displays to users for cloning in the JupyterServer application. See [`code_repository` Block](#code_repository-block) below.
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
* `default_resource_spec` - (Required) The default instance type and the Amazon Resource Name (ARN) of the SageMaker image created on the instance. See [`default_resource_spec` Block](#default_resource_spec-block) below.

### `custom_file_system` Block
Expand All @@ -73,6 +75,7 @@ The `custom_file_system` block supports the following argument:

The `jupyter_lab_app_settings` block supports the following arguments:

* `app_lifecycle_management` - (Optional) Settings that are used to configure and manage the lifecycle of JupyterLab applications in a space. See [`app_lifecycle_management` Block](#app_lifecycle_management-block) below.
* `code_repository` - (Optional) A list of Git repositories that SageMaker automatically displays to users for cloning in the JupyterServer application. See [`code_repository` Block](#code_repository-block) below.
* `default_resource_spec` - (Required) The default instance type and the Amazon Resource Name (ARN) of the SageMaker image created on the instance. See [`default_resource_spec` Block](#default_resource_spec-block) below.

Expand Down Expand Up @@ -134,6 +137,18 @@ The `ebs_storage_settings` block supports the following argument:

* `ebs_volume_size_in_gb` - (Required) The size of an EBS storage volume for a space.

### `app_lifecycle_management` Block

The `app_lifecycle_management` block supports the following argument:

* `idle_settings` - (Optional) Settings related to idle shutdown of Studio applications. See [`idle_settings` Block](#idle_settings-block) below.

### `idle_settings` Block

The `idle_settings` block supports the following argument:

* `idle_timeout_in_minutes` - (Optional) The time that SageMaker waits after the application becomes idle before shutting it down. Valid values are between `60` and `525600`.

## Attribute Reference

This resource exports the following attributes in addition to the arguments above:
Expand Down
Loading