Amazon CodeWhisperer with JupyterLab extension for Amazon SageMaker Studio - Part 4
Tags: aws, tutorial, machinelearning, ai
Published: 2023-06-25 14:24:40

## What's new with Amazon CodeWhisperer ?

On 10 May 2023, Senior Principal Technologist at AWS Brian Granger announced extensions to generative AI on Jupyter including:

* Jupyter AI, an open-source project to bring generative AI to Jupyter notebooks

* Introducing new Jupyter extensions to build, train, and deploy ML at scale

* Amazon CodeWhisperer Jupyter extension

For developers, cloud architects, data scientists, data analysts and machine learning engineers in particular this is exciting news allowing JupyterLab users to install Amazon CodeWhisperer to use Python notebooks and also Amazon SageMaker Studio.

You may read all of the Jupyter extensions [here](https://aws.amazon.com/blogs/machine-learning/announcing-new-jupyter-contributions-by-aws-to-democratize-generative-ai-and-scale-ml-workloads/?sc_channel=sm&sc_campaign=Machine_Learning&sc_publisher=LINKEDIN&sc_geo=GLOBAL&sc_outcome=awareness&sc_content=ml_services&trk=b27254ff-c966-4c2a-bb52-d25d5f182ddc&linkId=213815850).

### Learning Objective

You will learn:

* How to install Amazon CodeWhisperer with JupyterLab extension to generate code recommendations within Amazon SageMaker Studio

### What's the benefit of using Amazon CodeWhisperer?

* You may get started and up and running on a machine learning project and increase your developer productivity with auto-completion, provides code suggestions by learning from code snippets and python comments.

* You may also code in other languages such as:

Python, Java, JavaScript, TypeScript, C#, Go, Rust, PHP, Ruby, Kotlin, C, C++, Shell scripting, SQL, and Scala in various IDEs.


![iden](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4ykl3p2z0ioxob6s4x2.jpg)

* There are built-in security scans as you code based on the [AWS Shared Responsibility Model](https://aws.amazon.com/compliance/shared-responsibility-model/).

* Amazon CodeWhisperer is free for individual use.

You may read my previous [blog post](https://dev.to/aws-heroes/amazon-codewhisperer-for-data-science-and-analytics-get-started-with-generative-ai-on-aws-part-2-5e66) if you would like to know how to install Amazon CodeWhisperer for VS Code IDE.

I recently used Amazon CodeWhisperer to kickstart a personal machine learning project which allowed me to code faster.

By using Amazon CodeWhisperer in VS code editor, I was able to complete exploratory data analysis (EDA) in python, I simply typed words into VS code editor and asked a question like a friend using natural language. EDA was completed within 15 minutes and learned about my intent by reading my python comments and making the next suggestion in the data analytics workflow.


![ask](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sajoo0vsrekp383qvb15.jpg)


![hist](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v77d8xugjcd2reo7ausy.jpg)

### Get code recommendations for Amazon SageMaker Studio

Amazon SageMaker Studio launched in 2019 as an IDE using JupyterLab
to allow data science teams to collaborate on the end to end data science workflow which includes build models, train models, fine-tune hyper parameters, debug, deploy, and monitor model drift.

On the 10 May 2023, there were new [announcements](https://aws.amazon.com/blogs/machine-learning/announcing-new-jupyter-contributions-by-aws-to-democratize-generative-ai-and-scale-ml-workloads/?sc_channel=sm&sc_campaign=Machine_Learning&sc_publisher=LINKEDIN&sc_geo=GLOBAL&sc_outcome=awareness&sc_content=ml_services&trk=b27254ff-c966-4c2a-bb52-d25d5f182ddc&linkId=213815850)

You may now receive real-time code recommendations from Amazon CodeWhisperer as you code and build your machine learning pipeline within Amazon SageMaker Studio.

### Pre-requisites

* [Creating your first IAM admin user and User group in your AWS account](https://dev.to/aws-builders/creating-your-first-iam-admin-user-and-user-group-in-your-aws-account-machine-learning-part-1-3cne)

* You may read the following [blog](https://dev.to/aws-heroes/getting-started-with-amazon-sagemaker-jumpstart-image-classification-model-for-natural-disaster-damage-5ghb) to onboard Amazon SageMaker Domain.

* Add the Amazon CodeWhisperer-related permissions to your SageMaker execution role (e.g. Amazon SageMaker FullAccessrole)

```
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "CodeWhispererPermissions",
      "Effect": "Allow",
      "Action": ["codewhisperer:GenerateRecommendations"],
      "Resource": "*"
    }
  ]
}
```

## Tutorial: Add the CodeWhisperer-related permissions to your SageMaker execution role.

For this [tutorial](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-set-up.html) please follow the steps from the Amazon CodeWhisperer Developer Guide.

Step 1: Login to your AWS account as an [Admin User](https://dev.to/aws-builders/creating-your-first-iam-admin-user-and-user-group-in-your-aws-account-machine-learning-part-1-3cne) and choose your preferred AWS region e.g. Asia Pacific Sydney (ap-southeast-2).

Step 2: Navigate to the IAM dashboard.


![IAM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3sp9cxijelg4kh8m2o2.png)

Step 3: Create an IAM policy that contains the following actions to enable Amazon CodeWhisperer for Amazon SageMaker Studio role.

```
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "CodeWhispererPermissions",
      "Effect": "Allow",
      "Action": ["codewhisperer:GenerateRecommendations"],
      "Resource": "*"
    }
  ]
}
```
Navigate to **Policies** on the left-handside menu.


![policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fy6y6hwb81xyit858r01.png)

Step 4: Click **Create policy**.


![Create policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vc4vbmnkovotzlzi8nd.png)

Step 5: Click **json**.


![jsonn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wwa8l16cr4a5jal4fnw.png)

Step 6: Copy the new IAM policy into the json statement and click **Next**.


![copy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fus3wjpef2wmnkeaknjx.png)


Step 7: Provide a policy name e.g. Attach_Amazon_CodeWhisperer_to SageMaker_Studio and click **Create policy**.



![policy ame](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccrjbssh9b21fa3q1yx2.png)

The policy was successfully created.

![view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9b11sfmvkji0wtyump91.png)

You may inspect the policy.


![created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/op5seu5ad2ksym5xalzi.png)


Navigate to IAM and click **Roles**, search for 'Sagemakerfullaccess role' and check if there are any policies already attached.


![sagemakerfull](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5xw2gkscdc4l628p752.png)




Step 8: Navigate to **Policies** and search for policy 'Attach_Amazon_CodeWhisperer_to SageMaker_Studio' and attach the following IAM entity:

> SageMakerfullaccess


![attach polocu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz10n55pn3tiwwu6udm3.png)

Step 9: Navigate to the Amazon SageMaker dashboard and on the left-handside menu click **Getting Started** and on the right select **Create a role**.


![sae](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ketcixhzgn6en7lxev5q.png)


Step 10: Enter the following details such as providing a role name with the pre-fix of SageMaker. e.g. DataScientist, MLEngineer

> DataScientist_SageMaker

You may also provide a short description of the role e.g. create a role to enable Amazon CodeWhisperer recommendations for Amazon SageMaker Studio.

Retain the other default settings and click **Next**.


![role](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrewctvjaem8yatdie50.png)

Step 11: Configure the role by selecting the ML activities.


![configure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6kf2mq56xxg3snwntsl.png)

You may select more activities or de-select the ML activities that you do not require.

Step 12: Scroll down and enter the name of one of your Amazon S3 buckets to store your training and test data. Click **Next**.

> sagemaker-<AWS region>-<AWS account number


![bucket](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4ft4sq47om89cvpezzn.png)


Step 13: In the search bar, start typing the name of the IAM policy that was created in Step 7.

> Attach_Amazon_CodeWhisperer_to SageMaker_Studio


![search policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owuf1jw2vzzmhnubz2jx.png)

Check the box of the policy and click **Next**.


![seelct](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqhn76rvaeiar62b2hdf.png)


Step 14: Inspect and review the role details.


![reviewn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrkgd3zvu8xiayma8xfi.png)

Review the additional policy attached to the SageMaker execution role and click **Submit**.


![submit tole](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtzdgcu4n8d4shvdh1jn.png)

Step 15: The Sagemaker_DataScientist_SageMaker role was successfully created and you may view the role in IAM.


![sagemaker role](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbs495am2fq4hxbobpn7.png)

You can see that this new role has an additional policy attached to enable Amazon CodeWhisperer to be used under the SageMaker execution role for Amazon SageMaker Studio.


![Iiam code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nl4xwobye7pl641u33no.png)

Step 16: Enable Amazon CodeWhisperer within Amazon SageMaker Studio

Select **Studio** on the left-handside menu and click **Open Studio**.


![open](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltvbek5ghrdnlliloshi.png)

Step 17: In Amazon SageMaker Studio, click **Open Launcher**.


![open launchr](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4by32piypovn06gsxusd.png)

Step 18: Under the heading 'Utilities and file', click **System terminal**.


![system terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xs9yqxwh0lz3xnacc2k.png)


Step 19: Copy the code into terminal:

```
conda activate studio
pip install amazon-codewhisperer-jupyterlab-ext
jupyter server extension enable amazon_codewhisperer_jupyterlab_ext
conda deactivate
restart-jupyter-server
```

The installation process will take a few seconds to complete.


![comeplt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmbpxfvi00w47kino97t.png)

Step 20: Navigate to the Launcher homepage and select **Create Notebook**.


![create notebookn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oh1uuxmctvcqtp43kayz.png)

I received the following notification message.

![notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2w64aa4xg7o3qzx9rvf.png)

Step 21: Close the Amazon SageMaker Studio session.

Refresh your browser and navigate to **Studio** and click **Open Studio** and wait for Amazon SageMaker Studio to open.

Under the notebook you will be able to see a tick against CodeWhisperer to confirm that code recommendations are enabled for the code cells.

Step 22: Launch Studio again and you will be taken to JupyterLab which will open the last notebook and start typing Python code into the code cells to generate code recommendations.

![stat tryping](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/za8ll2djeqstgriowtp4.png)

### Reference

* [Amazon CodeWhisperer User Guide](https://docs.aws.amazon.com/codewhisperer/latest/userguide/sagemaker-setup.html)

* [Setting up CodeWhisperer with Amazon SageMaker Studio](https://docs.aws.amazon.com/codewhisperer/latest/userguide/sagemaker-setup.html)

* [Amazon CodeWhisperer Documentation](https://docs.aws.amazon.com/codewhisperer/?icmpid=docs_homepage_devtools)

* [JupterLab extension examples](https://github.com/jupyterlab/extension-examples)

* [Amazon CodeWhisperer features](https://aws.amazon.com/codewhisperer/features/)

* [Announcing new Jupyter contributions by AWS to democratize generative AI and scale ML workloads](Announcing new Jupyter contributions by AWS to democratize generative AI and scale ML workloads)

* [Onboard to Amazon SageMaker Domain](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-studio-onboard.html)

* [Setting up CodeWhisperer with Amazon SageMaker Studio](https://docs.aws.amazon.com/codewhisperer/latest/userguide/sagemaker-setup.html)

* [Amazon CodeWhisperer extension for Jupyter](https://pypi.org/project/amazon-codewhisperer-jupyterlab-ext/)

* [JupyterLab Desktop](https://github.com/jupyterlab/jupyterlab-desktop/blob/master/user-guide.md#uninstalling-jupyterlab-desktop)

* [How JupyterLab Desktop works](https://github.com/jupyterlab/jupyterlab-desktop/blob/master/user-guide.md)

* [Adding and removing IAM](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)

* [Use Amazon SageMaker Launcher] (https://docs.aws.amazon.com/sagemaker/latest/dg/studio-launcher.html)

Until the next lesson, happy learning! 😀

## Build on Generative AI - Using your Python scripts in Sagemaker 

You may be interested to learn more from to change your Python scripts to run on SageMaker Training? 🦅Join guest Giuseppe Angelo Porcelli, along with cohosts Darko Mesaroš & Emily Webber to learn more on [Build on Generative AI](https://www.twitch.tv/aws/video/1838448394)

![emly](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cyz70wd2n8g27s2v9xk.png)

### Last week - AWS re:Inforce 2023

You may be interested to learn more about [Amazon Bedrock](https://www.youtube.com/watch?v=5EDOTtYmkmI), foundation models and Generative AI from AWS re:Inforce announcements last week. You may watch the keynote from AWS CISO CJ Moses, break-out sessions and leadership sessions [here](https://www.youtube.com/@AWSEventsChannel)


![reinforce](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6sp2bn4nwjcwowv7qg2j.png)