How do I add code repositories to my SageMaker AI notebook instance?

2 minute read
0

I want to add code repositories to my Amazon SageMaker AI instance.

Resolution

SageMaker AI supports integration with GitHub code repositories. For HTTPS, use the SageMaker AI built-in Git integration. For SSH repositories or internal Git servers that you access through a virtual private cloud (VPC), use lifecycle configurations.

Use the SageMaker AI Git integration for HTTPS

Add a Git repository to your SageMaker AI account.

When you enter your repository, use the following format:

  • GitHub URL: https://github.com/workspace/repository.git
  • Bitbucket URL: https://bitbucket.org/workspace/repository.git
  • GitLab URL: https://gitlab.com/workspace/repository.git

SageMaker AI clones the repositories to /home/ec2-user/SageMaker/.

Use lifecycle configurations for SSH-based Git integration

Complete the following steps:

  1. Store your SSH private key in AWS Secrets Manager.

  2. Create a lifecycle configuration.

  3. In the Start notebook editor, enter the following script and customize it with your Git server and security policies:

    bash
    #!/bin/bash
    python << EOF
    import boto3
    import base64
    
    
    secret_name = "your_secret_name"
    region_name = "your_region"
    
    
    session = boto3.session.Session()
    client = session.client(service_name='secretsmanager', region_name=region_name)
    get_secret_value_response = client.get_secret_value(SecretId=secret_name)
    
    
    if 'SecretString' in get_secret_value_response:
     secret = get_secret_value_response['SecretString']
     with open('/home/ec2-user/.ssh/id_rsa', 'wt') as tmp:
     tmp.write(secret)
    else:
     decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
     with open('/home/ec2-user/.ssh/id_rsa', 'wt') as tmp:
     tmp.write(decoded_binary_secret)
    EOF
    
    
    echo "Host github.com " >> /home/ec2-user/.ssh/config
    echo " StrictHostKeyChecking no" >> /home/ec2-user/.ssh/config
    echo " IdentityFile /home/ec2-user/.ssh/id_rsa" >> /home/ec2-user/.ssh/config
    
    
    chmod 600 /home/ec2-user/.ssh/config
    chmod 600 /home/ec2-user/.ssh/id_rsa
    chown ec2-user:ec2-user /home/ec2-user/.ssh/config
    chown ec2-user:ec2-user /home/ec2-user/.ssh/id_rsa
    
    
    cd /home/ec2-user/SageMaker
    sudo su ec2-user -c "git clone git@github.com:myAccount/SageMakerNotebook.git"
    
    
    rm /home/ec2-user/.ssh/config
  4. Check that the notebook instance's AWS Identity and Access Management (IAM) role has the following permissions policy to access Secrets Manager:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "secretsmanager:GetSecretValue",
          "Resource": "arn:aws:secretsmanager:region:account-id:secret:secret-name"
        }
      ]
    }

Related information

Create an Amazon SageMaker AI notebook instance

Identity-based policies

How do I troubleshoot issues associated with connecting Git repositories to SageMaker AI notebook instances?

AWS OFFICIAL
AWS OFFICIALUpdated 17 days ago