Remote IoT Development with VS Code
Development environment for IoT or Docker based application will be time consuming and error prone setup. To avoid the same we can configure a remote VM and connect the same from your VS code. This will help to avoid the repetition of steps and also avoiding errors white recreating the same.
In our case, we are using Linux OS as our Remote Machine. We can use both Linux or Windows based local machine to connect to the Remote VM
Prerequisites
VS Code
Remote-SSH ( VS Code Extension)
Creating SSH Public Key
Windows users can start from the step 1 and Linux users can start from step4
- Open PowerShell command
- Run below command
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
- Close the PowerShell
- Open PowerShell in windows or Command window in Linux. This will generate a new public token for you.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- This will request you to enter a passphrase for the key
- To view the token generated from last step, use script below.
cat C:\Users\<your_user>\.ssh\id_rsa.pub
- Please copy and keep the key with you, we need the same while creating new Linux VM on cloud.
Creating Linux VM on Azure
We can create a normal Ubuntu Server VM for the purpose.
- Login To Azure Portal
- Create new VM
- Select Ubuntu Server
- Choose any location and SKU based on your need
- Select the authentication as “SSH Public Key”
- The it will bring a new text area to paste your public key.
- Paste the key which you copied from previous section
Connecting to VM from VS Code
Now you are ready to connect your VS code to the Linux VM
- Open VS Code and open Command palette by “ctrl+shft+p”
- search for “Remote-SSH: Add New SSH Host”
- enter below value on the popped box
ssh user@hostname
- Then you will be requested to enter the passphrase, you can enter the passphrase which you used while creating the public key
- Open the command palette again
- search for “Remote-SSH: Connect to host”
Now you are ready with the setup to start your IoT or docker based application. Now the VS Code commands will be executing the same in the Linux VM
for advanced steps or more you can refer below link
https://code.visualstudio.com/docs/remote/ssh
Configure the VM
Now we can configure the VM, you can run the commands from the VS Code terminal
# Install Docker
sudo apt update
sudo apt install -y docker.io
# Configure Docker
sudo usermod -aG docker $USER
sudo reboot
# Install .NET Core and its dependencies
sudo apt-get install -y apt-transport-https
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt update
sudo apt-get install -y dotnet-sdk-3.1
# Install docker-compose and its dependencies
sudo apt install -y curl
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Install IoT Edge Hub Dev Simulator and its dependencies
sudo apt install -y python3-pip
python3 -m pip install --upgrade pip
sudo -H python3 -m pip install --upgrade pip
sudo -H python3 -m pip install --ignore-installed PyYAML
sudo -H python3 -m pip install --upgrade iotedgehubdev
Now you are ready to do the Azure IoT Edge Development
Happy coding