AWS CodeGuru and Python Getting Started

 

When you write code, whether it’s a full-blown application or a script, you want to ensure that the code is of quality. Whether it’s only you using the code or others, making sure that code follows best practices and security protocols is important.

If you’re using AWS, CodeGuru is the answer. AWS CodeGuru is a tool that provides intelligent recommendations to improve code quality and identifies apps most expensive lines of code. It provides automatic code reviews to automatically check for performance, code quality, and overall cost in any environment.

When AWS CodeGuru first came out, it only supported Java. I created a video for checking Java code, which you can find here. Now, you can use Python with AWS CodeGuru. In this blog post, you’ll learn how to configure CodeGuru to run checks on Python code.

Connecting a Repository

The first thing you’ll need to do is set up CodeGuru to point to a specific code repository. The repository can contain code that is either Java or Python. In this case, you’ll use a repository that contains Python code.

If you don’t have a repository that contains Python code, feel free to fork the following repo, which points to a script to list EC2 instances.

https://github.com/AdminTurnedDevOps/Programming-With-Python-For-DevOps-Engineers-Course/blob/main/Lesson-3-After-The-Basics/EC2-Instances/list_ec2info.py

Open the AWS Management Console and search for codeguru.

Once in CodeGuru, click on Repositories

Inside of the Repositories pane, click Associate repository to link the GitHub repository.

Connect GitHub to AWS, which will ask you for your GitHub username/password, and then choose the Repository location. Once you choose the repository location, click the orange Associate button.

Once the repository is associated, you will see the status change to Associated as shown in the screenshot below.

Creating a Profile Group

Once the GitHub repository is connected in CodeGuru, it’s time to set up a profile. A profile is what’s used to run an Agent on the code you write/run for CodeGuru to retrieve information about the code.

Under the Profiler pane, click Profiling groupsCreate profiling group

Give the profile a name which will identify the profile and choose Other under compute platform. This is to ensure you’re running the code locally or on a server vs in a Lambda Function. Once completed, choose the orange Create button.

Under the profile, you will see two options:

  • JVM
  • Python 3.6-3.9

Choose the option for Python.

Under the Start profile in your application pane, you’ll see two options:

  • Enabling the agent with code
  • Enabling the agent from the command line

Choose the option to enable the agent with code. You’ll see an output similar to the screenshot below which will give you directions for installing the CodeGuru profiler agent via pip and what code you need to put into the Python code.

Below is a screenshot of the code I pointed to in the previous section for you to use with the CodeProfiler code.

Run the code and you’ll now see in the CodeGuru Profile Group that the status has changed to Pending.

Pending means, CodeGuru is doing an analysis of the code that was run.

And that’s it! You’ll now wait for CodeGuru to analyze the code and get the results based on recommendations and best practices.

Leave a Reply

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