The Windows Subsystem for Linux (WSL) lets developers and system administrators run a Bash shell in a chosen Linux distribution package. They can run their favorite command-line Linux tools like the Amazon Web Services (AWS) Command Line Interface (CLI) in Python on Windows 10.

Prateek Singh

Prateek Singh is an infrastructure developer, an avid PowerShell blogger, and a community contributor. In 2017, FeedSpot recognized his blog RidiCurious.com as among the "Top 50 PowerShell blogs in the world." All of his PowerShell projects and modules are open-sourced at GitHub, and you can reach out to him on Twitter at @SinghPrateik.

Latest posts by Prateek Singh (see all)

The Bash shell has been part of Linux for a quite long time now, but Microsoft recently added it into Windows 10 with the Anniversary Update. This is not an emulation of Linux Bash, but an actual Bash shell that can now work natively on Windows in a new subsystem as if you're using an actual Linux computer. If you prefer Python as a scripting language to manage Amazon's cloud, you can use this Windows feature to the run the AWS CLI in a Python environment.

Enable the Windows Subsystem for Linux ^

Before we start anything and install the Linux distribution package for WSL, make sure you enable the "Windows Subsystem for Linux" optional feature on your machine. Running the following command from your PowerShell console will enable this feature.

Open PowerShell as an administrator and run:

Enable the Windows Subsytem on Linux feature

Enable the Windows Subsytem on Linux feature

It will prompt you to reboot your computer. Type Y and proceed with that to complete the operation.

Install your Linux distribution package ^

After enabling WSL, you need to install one of the Linux distribution packages of your choice on this subsystem. Microsoft offers couple of options to download and install your preferred distribution package:

In case you have an earlier version of the Windows 10 build, follow the instructions in the second option above.

Only for the Windows 10 Fall Creators Update (Windows build 16215) and later, you can install it from the Microsoft Store by performing the following steps:

Open the Microsoft Store from the Start menu and then search for "Linux" in the Search bar at the top-right corner. You'll get a list of Linux packages.

Search for Linux in the Microsoft Store

Search for Linux in the Microsoft Store

Now choose any Linux distribution. I'll choose Ubuntu in this case.

From the distribution package page for Ubuntu, click on the Get button to start the installation.

Install the Ubuntu distribution package

Install the Ubuntu distribution package

Once the installation is complete, go to the Start menu and search for Ubuntu. Click on it for the first launch.

You'll see the Ubuntu Bash shell. Please allow it some time to do the setup, which may take few minutes.

Now provide a username and password.

Provide a username and password

Provide a username and password

Install Python and pip ^

Once you have set up Bash, you need to install Python version 2.7 or later if it's not already installed on your machine, as it would be required to run the pip command. This is a package management framework written in Python. In this case, I'll install Python version 2.7:

Once the installation is complete, you can verify the version of Python installed:

Now we have to install pip, and for that, we must first download a script using curl and then run it with Python:

Install Python and pip

Install Python and pip

Then run the following command to add ~/.local/bin to the current PATH variable. Unless you do this, the Bash shell won't be able to find the pip command.

Add pip to the PATH variable

Add pip to the PATH variable

Install the AWS CLI and configure the environment ^

Use pip to install the AWS CLI so we can create an Elastic Compute Cloud (EC2) instance and key pair for access.

Then you have to set up your AWS user credentials (access ID and secret key) in the Bash shell, and you can obtain these from the AWS Identity and Access Management (IAM) console. Run the following AWS CLI command and provide your keys, region, and output format like in the sample below.

Create an AWS EC2 security group and key pair ^

Now we'll create a key pair in AWS, which we'll use to access EC2 later. Please download a copy of this key pair to your local machine.

Run the following command to avoid any "Access Denied" errors while connecting to the virtual machine (VM). You'll need it to change the file mode of the key pair to read only so only you can access it.

Create a key pair

Create a key pair

Launch a new EC2 instance and SSH ^

Launch a new AWS EC2 instance using the key pair like in the following example, which will return an instance ID.

Now query the public IP address using the instance ID obtained in previous step. Then SSH into the instance using the key pair we created earlier and the public IP address. This will create a session on the AWS EC2 instance and connect your Bash shell to it.

Launch an EC2 instance and SSH into it

Launch an EC2 instance and SSH into it

Conclusion ^

In this article, we demonstrated how we can install Ubuntu in WSL and run an AWS CLI command from the Bash shell to create an EC2 instance and SSH into this remote AWS EC2 instance. It's just like any other Linux user experience, but built natively in Windows.

Are you an IT pro? Apply for membership!

0
Share
0 Comments

Leave a reply

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

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account