Unlocking the Power of CodeWhisperer in Your SageMaker Notebook: A Step-by-Step Guide.
Published: 2023-09-02 14:10:35

In the world of data science and machine learning, having a seamless and efficient development environment is crucial. Amazon SageMaker has been a go-to platform for many data professionals, providing a collaborative space to build, train, and deploy machine learning models. But what if you could supercharge your SageMaker Notebook experience even further? That’s where CodeWhisperer comes into play.

In this article, we’ll walk you through the process of configuring CodeWhisperer in your SageMaker Notebook Instance, step by step. let’s dive right in.

## **Step 1: Customize Your Notebook Instance with a Script**

To make sure CodeWhisperer is good to go every time you fire up your SageMaker Notebook, just follow these straightforward steps:

**# Create a Lifecycle Configuration Script**

* Go to the SageMaker console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

* On the left, click “Lifecycle configurations.”

* Now, switch to the “Notebook Instance” tab.

* Choose Create configuration

* Give it a name (only letters, numbers, and hyphens, no spaces, please).

* To make sure this script runs when you create and start a notebook, choose “Start notebook.”

* Now, paste this script in the “Start notebook editor”:

```
    #!/bin/bash
    
    set -e
    
    # OVERVIEW
    # This script installs a single Jupyter Notebook server extension package in SageMaker Notebook Instance
    
    sudo -u ec2-user -i <<'EOF'
    
    # PARAMETERS
    PIP_PACKAGE_NAME=amazon-codewhisperer-jupyterlab-ext
    EXTENSION_NAME=amazon_codewhisperer_jupyterlab_ext
    
    source /home/ec2-user/anaconda3/bin/activate JupyterSystemEnv
    
    pip install $PIP_PACKAGE_NAME
    jupyter server extension enable $EXTENSION_NAME --py --sys-prefix
    
    source /home/ec2-user/anaconda3/bin/deactivate
    
    EOF
    
    # The restart command is dependent on the current running Amazon Linux and JupyterLab
    CURR_VERSION=$(cat /etc/os-release)
    if [[ $CURR_VERSION == *$"http://aws.amazon.com/amazon-linux-ami/"* ]]; then
        sudo initctl restart jupyter-server --no-wait
    else
        sudo systemctl --no-block restart jupyter-server.service
    fi
```
* Click “Create configuration.”

**# Update the Notebook Instance to use a Lifecycle Configuration Script**

* Open the SageMaker console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

* Navigate to “Notebook instances.”

* Select the notebook instance you want to update by clicking its name.

* If the notebook status isn’t “Stopped,” click “Stop” to halt the notebook instance.

* Click “Edit” to open the Edit notebook instance page.

* Under “Additional configuration,” select the lifecycle configuration created in step 1.1.

* Click “Update Notebook Instance.”

## Step 2: Updating the Notebook Instance Execution Role to Allow CodeWhisperer Usage

* Open the SageMaker console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

* Navigate to “Notebook instances.”

* Select your notebook instance.

* Scroll down to the “Permissions and encryption” section.

* Grab the Notebook Instance Execution Role.

* Update the role with the following policy:
```
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "CodeWhispererPermissions",
                "Effect": "Allow",
                "Action": [
                    "codewhisperer:GenerateRecommendations"
                ],
                "Resource": "*"
            }
        ]
    }
```
* Once you’ve updated the policy, Start Jupyter Labs and the CodeWhisperer extension will be enabled.

## Conclusion

With these simple steps, you can enhance your SageMaker Notebook Instance with CodeWhisperer, unlocking a wealth of features and functionalities for your data science and machine learning projects. So, why wait? Give it a try and experience the difference yourself. Happy coding!

Connect with me on [Linkedin](https://www.linkedin.com/in/vmarpadge/)