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-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
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:
- Download and install it from the Windows Microsoft Store
- Download and manually unpack and install it
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.
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.
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.
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:
sudo apt-get install python
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:
curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py –user
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.
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.
pip install awscli --upgrade –user
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.
AWS Access Key ID [None]: YOUR ACCESS ID
AWS Secret Access Key [None]: YOUR SECRET KEY
Default region name [None]: ap-south-1
Default output format [None]: json
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.
aws ec2 create-key-pair --key-name ec2-keypair --query 'KeyMaterial' --output text > ec2-keypair.pem
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.
chmod 400 ec2-keypair.pem
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.
aws ec2 run-instances --image-id ami-00b6a8a2bd28daf19 --security-group-ids sg-XXXXXXXX --count 1 --instance-type t2.micro --key-name ec2-keypair --query 'Instances.InstanceId'
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.
ssh -i ec2-keypair.pem firstname.lastname@example.org
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.