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

feature(sagemaker notebook): Add support for DefaultCodeRepository #13772

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
14 changes: 14 additions & 0 deletions aws/resource_aws_sagemaker_notebook_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ func resourceAwsSagemakerNotebookInstance() *schema.Resource {
}, false),
},

"default_code_repository": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"tags": tagsSchema(),
},
}
Expand All @@ -106,6 +112,10 @@ func resourceAwsSagemakerNotebookInstanceCreate(d *schema.ResourceData, meta int
createOpts.DirectInternetAccess = aws.String(v.(string))
}

if v, ok := d.GetOk("default_code_repository"); ok {
createOpts.DefaultCodeRepository = aws.String(v.(string))
}

if s, ok := d.GetOk("subnet_id"); ok {
createOpts.SubnetId = aws.String(s.(string))
}
Expand Down Expand Up @@ -199,6 +209,10 @@ func resourceAwsSagemakerNotebookInstanceRead(d *schema.ResourceData, meta inter
return fmt.Errorf("error setting direct_internet_access for sagemaker notebook instance (%s): %s", d.Id(), err)
}

if err := d.Set("default_code_repository", notebookInstance.DefaultCodeRepository); err != nil {
return fmt.Errorf("error setting default_code_repository for sagemaker notebook instance (%s): %s", d.Id(), err)
}

tags, err := keyvaluetags.SagemakerListTags(conn, aws.StringValue(notebookInstance.NotebookInstanceArn))

if err != nil {
Expand Down
86 changes: 86 additions & 0 deletions aws/resource_aws_sagemaker_notebook_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,42 @@ func testAccCheckAWSSagemakerNotebookDirectInternetAccess(notebook *sagemaker.De
}
}

func TestAccAWSSagemakerNotebookInstance_default_code_repository(t *testing.T) {
var notebook sagemaker.DescribeNotebookInstanceOutput
notebookName := resource.PrefixedUniqueId(sagemakerTestAccSagemakerNotebookInstanceResourceNamePrefix)
var resourceName = "aws_sagemaker_notebook_instance.foo"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSagemakerNotebookInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSagemakerNotebookInstanceConfigDefaultCodeRepository(notebookName, "https://github.com/terraform-providers/terraform-provider-aws.git"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSagemakerNotebookInstanceExists(resourceName, &notebook),
testAccCheckAWSSagemakerNotebookDefaultCodeRepository(&notebook, "https://github.com/terraform-providers/terraform-provider-aws.git"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAWSSagemakerNotebookDefaultCodeRepository(notebook *sagemaker.DescribeNotebookInstanceOutput, expected string) resource.TestCheckFunc {
return func(s *terraform.State) error {
defaultCodeRepository := notebook.DefaultCodeRepository
if *defaultCodeRepository != expected {
return fmt.Errorf("default_code_repository setting is incorrect: %s", *notebook.DefaultCodeRepository)
}

return nil
}
}

func testAccCheckAWSSagemakerNotebookInstanceTags(notebook *sagemaker.DescribeNotebookInstanceOutput, key string, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sagemakerconn
Expand Down Expand Up @@ -593,3 +629,53 @@ resource "aws_subnet" "sagemaker" {
}
`, notebookName, directInternetAccess)
}

func testAccAWSSagemakerNotebookInstanceConfigDefaultCodeRepository(notebookName string, defaultCodeRepository string) string {
return fmt.Sprintf(`
resource "aws_sagemaker_notebook_instance" "foo" {
name = %[1]q
role_arn = aws_iam_role.foo.arn
instance_type = "ml.t2.medium"
security_groups = [aws_security_group.test.id]
subnet_id = aws_subnet.sagemaker.id
default_code_repository = %[2]q
}

resource "aws_iam_role" "foo" {
name = %[1]q
path = "/"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}

data "aws_iam_policy_document" "assume_role" {
statement {
actions = [ "sts:AssumeRole" ]
principals {
type = "Service"
identifiers = [ "sagemaker.amazonaws.com" ]
}
}
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "tf-acc-test-sagemaker-notebook-instance-default-code-repository"
}
}

resource "aws_security_group" "test" {
vpc_id = aws_vpc.test.id
}

resource "aws_subnet" "sagemaker" {
vpc_id = aws_vpc.test.id
cidr_block = "10.0.0.0/24"

tags = {
Name = "tf-acc-test-sagemaker-notebook-instance-default-code-repository"
}
}
`, notebookName, defaultCodeRepository)
}
1 change: 1 addition & 0 deletions website/docs/r/sagemaker_notebook_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The following arguments are supported:
* `kms_key_id` - (Optional) The AWS Key Management Service (AWS KMS) key that Amazon SageMaker uses to encrypt the model artifacts at rest using Amazon S3 server-side encryption.
* `lifecycle_config_name` - (Optional) The name of a lifecycle configuration to associate with the notebook instance.
* `direct_internet_access` - (Optional) Set to `Disabled` to disable internet access to notebook. Requires `security_groups` and `subnet_id` to be set. Supported values: `Enabled` (Default) or `Disabled`. If set to `Disabled`, the notebook instance will be able to access resources only in your VPC, and will not be able to connect to Amazon SageMaker training and endpoint services unless your configure a NAT Gateway in your VPC.
* `default_code_repository` - (Optional) The Git repository associated with the notebook instance as its default code repository
* `tags` - (Optional) A map of tags to assign to the resource.

## Attributes Reference
Expand Down