SageMaker * CloudFormation

  1. Create a new CloudFormation template file.
  2. Add the following resources to the template file:

Replace <my-kms-key-id> with the ID of your pre-configured AWS KMS key.

To deploy this template using the AWS CLI, you can use the create-stack command:

aws cloudformation create-stack --stack-name MySageMakerStack --template-body file://sagemaker-stack.yaml


AWS CLI:

aws cloudformation deploy
–template-file notebook-instance.yaml
–stack-name notebook-instance-stack
–parameters ParameterKey=InstanceType,ParameterValue=ml.t2.medium ParameterKey=KmsKeyId,ParameterValue=<my-kms-key-id>

Boto3:

Python

import boto3

client = boto3.client('cloudformation')

template = '''
AWSTemplateFormatVersion: 2010-09-09

Parameters:
  InstanceType:
    Type: String
    Default: ml.t2.medium
  KmsKeyId:
    Type: String

Resources:
  NotebookInstance:
    Type: AWS::SageMaker::NotebookInstance
    Properties:
      InstanceType: !Ref InstanceType
      VolumeSizeInGB: 50
      KmsKeyId: !Ref KmsKeyId

Outputs:
  NotebookInstanceUrl:
    Value: !Ref NotebookInstance
    Export:
      Name: NotebookInstanceUrl
'''

response = client.create_stack(
    StackName='notebook-instance-stack',
    TemplateBody=template,
    Parameters=[
        {
            'ParameterKey': 'InstanceType',
            'ParameterValue': 'ml.t2.medium'
        },
        {
            'ParameterKey': 'KmsKeyId',
            'ParameterValue': '<my-kms-key-id>'
        }
    ]
)

stack_id = response['StackId']

client.wait_for_stack_status_complete(
    StackName=stack_id
)

output = client.describe_stacks(
    StackName=stack_id
)['Stacks'][0]['Outputs'][0]

notebook_instance_url = output['OutputValue']

print(notebook_instance_url)

Use code with caution. Learn morecontent_copy

This code will create a new CloudFormation stack and launch a notebook instance using the pre-configured AWS KMS key. You can then access the notebook instance using the URL in the NotebookInstanceUrl output.

To create a portfolio in the AWS Service Catalog and upload samples to share with scientists’ IAM roles:

  1. Open the AWS Service Catalog console.
  2. Click Create new portfolio.
  3. Enter a name and description for the portfolio.
  4. Click Create portfolio.

Once the portfolio is created, you can upload samples to share with scientists’ IAM roles by following these steps:

  1. Open the AWS Service Catalog console.
  2. Select the portfolio you created in the previous step.
  3. Click Products.
  4. Click the Import product button.
  5. Select the CloudFormation template product type.
  6. Click Next.
  7. Upload your CloudFormation template file.
  8. Click Next.
  9. Configure the product settings.
  10. Click Create product.

Once the product is created, you can upload samples to share with scientists’ IAM roles by following these steps:

  1. Open the AWS Service Catalog console.
  2. Select the product you created in the previous step.
  3. Click Versions.
  4. Click Create new version.
  5. Upload your sample files.
  6. Click Create version.

You can then assign the product to scientists’ IAM roles so that they can access and use the samples.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top