Visual Studio Code Aws



This post will describe the end-to-end process of setting up VS Code environment for Python and developing & deploying AWS Lambda functions using that VS Code setup.

Step 1: Get an access key and secret key for your IAM user Step 2: Install AWS Toolkit for Visual Studio and connect to CodeCommit Clone a CodeCommit repository from Visual Studio Create a CodeCommit repository from Visual Studio Working with CodeCommit repositories. Click install on the extension with title 'AWS Amplify API'. In a JavaScript, TypeScript, or JSX file, start typing an AWS Amplify API command that has a corresponding snippet and choose the appropriate snippet by either clicking it or pressing 'enter' or 'tab' when the correct snippet is highlighted in the dropdown menu. The AWS Toolkit for Visual Studio Code provides support for AWS Lambda functions. Using the Toolkit for VS Code, you can author code for Lambda functions that are part of serverless applications. In addition, you can invoke Lambda functions either locally or on AWS. Lambda is a fully managed compute service that runs your code in response to events generated by custom code or from various AWS. 🔥🔥 Best Online Code Training: 🔥🔥 BEST Web Hosting: Screen.

Visual Studio Code is the most popular IDE in the world right now according to Stack Overflow’s 2019 survey while Python is the most wanted and second most loved programming language and has risen the ranks of programming fame probably more quickly than any other language. So it’s quite an inclination to have these 2 entities setup side by side to complement each other.

After you finish this tutorial, you will be able to:

  • Setup Visual Studio Code environment for Python
  • Write Python code in Visual Studio Code
  • Run and debug Python Code in Visual Studio Code
  • Create virtual environment for Python
  • Connect your Visual Studio code environment to your AWS account
  • Create new AWS Lambda functions locally on Visual Studio Code.
  • Deploying your serverless applications using AWS SAM to your account using Visual Studio Code.

The tutorial is broken down into two parts: 1) Setup Visual Studio Environment for Python and 2) Connecting and deploying to AWS from Visual Studio Code. This is the first part.

Install Python

First install Python on your machine. You can find it here from Python’s official website.

Python’s package manager, pip, comes bundled with the installation. To make sure you have pip, you can run the following command on your terminal:

Python environment on Visual Studio Code

Now we will setup Visual Studio Environment so we can develop in Python.

Install Visual Studio Code from their official website.

After installation, open Visual Studio Code and click on the Extension button on the left side bar.

In the search bar, type ‘Python’ and search. A list will appear and most probably on the top, you will see the Python extension by Microsoft:

Install it and reload if prompted.

your first Python workspace

Open your command terminal and enter the following commands to create a new workspace:

python-ignite is simply a name of your choosing.

The above command will simply open the current working directory in Visual Studio Code.

Select Python Interpreter

Now we will explicitly choose our Python interpreter, as there could be multiple Python interpreters installed on your machine:

Open Command Palette:

  • Windows and Linux: Ctrl+Shift+P
  • MacOS: Shift+Command+P

Type and select Python: Select Interpreter and select an interpreter from the list, depending on how many Python installations you have. This value is going to be stored in the following file: .vscode > settings.json. You can change your interpreter any time.

Create new file and paste the following code inside it:

running Python Code

Python code can now be run in one of the following ways:

  • Click the ‘Run Code’ button
  • Right click in the file and select ‘Run Python File in Terminal’
Api
  • In order to run a specific selected portion of the code, select the code, right click and select ‘Run Selection/Line in Python Terminal’.
  • Open the Command Pallette Shift-Command-P and enter Python: Start REPL
  • Now you can enter the code in the REPL and run it.

debugging python code

Place a breakpoint on Line 2 by either clicking on the left side of a line before the line number or by placing a cursor on a line and pressing F9.

Press F5 to run the program.

A menu will appear that will present you with a list of different options from which you can choose the type of application to run. Choose Python File for now.

Visual Studio Code Aws Cli

You might be presented with an option on the left hand side to either Debug with Python or Run with Python. For now, choose Debug with Python.

The program will run and will pause on our breakpoint:

There are tons of things that you can do now in the debug mode in VS Code; you can inspect a variable in the VARIABLES pane, hover over variables to see values, in the Debug Console you can perform simulations on variables etc.

Running code form the terminal

If you want to run a Python script from the command terminal, simply enter this in the terminal:

Creating a Python Virtual Environment

Virtual Environments are useful in the scenarios when, let’s say, you have multiple Python installations on your system and some of your applications depend on a different version of Python and some depend on a different version one. With Virtual Environment, you can create a completely isolated environment for an application, in which that application is isolated and runs using the specified Python Interpreter and standard library. Anything in the global environment does not affect it and it itself does not effect anything outside of it.

To set it up:

Visual Studio Code Aws Codecommit

  • Open Command Palette.
  • Type ‘Terminal: Create new integrated terminal’
  • For macOS/Linux:
    • python3 -m venv .venv
    • source .venv/bin/activate
  • For Windows:
    • py -3 -m venv .venv
    • .venv/scripts/activate
  • This new environment will now be visible in VS Code when you type Python: Select Interpreter. Select the newly created environment.

installing Python Packages in the Virtual Environment

If you need to install any packages, you can install them using pip this way:

  • Mac OS:
    • python3 -m pip install matplotlib
  • Windows:
    • python -m pip install matplotlib
  • Linux
    • apt-get install python3-tk
    • python3 -m pip install matplotlib

These packages are now installed within the domain of the Virtual Environment only. You can run the program in the Virtual Environment now and it will run in isolation from the global context.

To deactivate the Virtual Environment, type deactivate in the VS Code terminal.

This completes the first part of this tutorial for setting up Visual Code environment for Python. The second part for connecting and deploying to AWS can be found here.

summary

This first part of the tutorial enabled you to setup Visual Studio Code with Python, establish a Python Interpreter of your choice for use in a Visual Studio Workspace. This post also described different ways to run a Python program in Visual Studio Code and also how to debug Python code using Visual Studio Code. It also enabled the use of Python’s Virtual Environment inside Visual Studio Code.

You can now jump to the second part of this tutorial which describes how to connect your Visual Studio Code environment with your AWS Account and to deploy your serverless applications automatically without any manual deployment steps everytime you want to deploy it.

The AWS CLI Configure extension allows you to quickly access AWS CLI information and docs from Visual Studio Code.

Features

The extension adds commands for the following:

  • Open 'credentials' file
  • Open 'config' file
  • Open 'credentials' & 'config' files
  • Browse the online docs
  • Show [named] profile mapped to [default] in 'credentials'
  • Set 'credentials' [default] profile to...
  • Copy profile name from 'credentials'...

Commands

Copy profile name

Now the name is on the clipboard. Paste away!

Show named profile mapped to default

  • Saving the 'credentials' file from VSCode or setting the default profile updates the status bar.
  • Click the item in the status bar to set the [default] profile to a [named] profile.

Set default profile

To be able to set the [default] profile to a named profile, in the 'credentials' file each profile section must contain only two entries following one of these formats:

  • aws_access_key_id & aws_secret_access_key
  • role_arn & source_profile

This sample shows the master-profile in one account that uses an access key and secret and three role profiles each in a seperate account that references the master-profile as the source profile.

The [default] profile is set to qa-profile.

Put region and other entries in the config file.If a section in credentials includes more than the required two entries and you try to set the default profile to it, you will get the following error:

Known Issues

No known issues

Release Notes

See Changelog

License

This software is released under MIT License© Mark Tucker (@rmtuckerphx) & Contributors

Review

Feedback and contributions welcome. Please leave a review.