Author : MD TAREQ HASSAN | Updated : 2023/07/25
Scenario
Let’s say we want to create a devcontainer as an alternative to Azure CloudShell (why? you may ask, well Azure CloudShell is slow).
First, see what are the tools pre-installed in Azure CloudShell.
We need subset of those tools, for example, I want to create PowerShell scripts as IaC (infrastructure-as-code).
Image selection
We are gonna use Azure PowerShell docker image provided by Microsoft (mcr.microsoft.com/azure-powershell
).
devcontainer.json
{
"name": "Azure PowerShell IaC Env",
"image": "mcr.microsoft.com/azure-powershell",
"features": {
}
}
Links:
- Azure PowerShell in a container: https://learn.microsoft.com/en-us/powershell/azure/azureps-in-docker#using-azure-powershell-in-a-container
- Azure PowerShell docker image: https://hub.docker.com/_/microsoft-azure-powershell
Feature selection
Other than Azure PowerShell, the rest of the tools, runtime, languages etc. will be selected as feature.
devcontainer.json
{
"features": {
"ghcr.io/devcontainers/features/azure-cli:1": {},
"ghcr.io/devcontainers/features/dotnet:1": {},
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/kubectl-helm-minikube:1": {},
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers/features/common-utils:2": {}
}
}
See: list of devcontainer features
Final devcontainer.json
.devcontainer/devcontainer.json
{
"name": "Azure PowerShell IaC",
"image": "mcr.microsoft.com/azure-powershell",
"features": {
"ghcr.io/devcontainers/features/azure-cli:1": {},
"ghcr.io/devcontainers/features/dotnet:1": {},
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/kubectl-helm-minikube:1": {},
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers/features/common-utils:2": {}
},
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "pwsh"
},
"extensions": [
"ms-vscode.powershell",
"ms-vscode.azurecli",
"editorconfig.editorconfig",
"oderwat.indent-rainbow"
]
}
}
}
Create repository
Create a repository named “azure-powershell-iac”.
Add “.gitignore
” file (https://github.com/PowerShell/vscode-powershell/blob/main/.gitignore)
.vscode-test/
logs/
modules
modules/
node_modules/
obj/
bin/
out/
sessions/
test-results.xml
*.vsix
*.DS_Store
Add “.editorconfig
” file (https://github.com/microsoft/vscode/blob/main/.editorconfig)
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
# Tab indentation
[*]
indent_style = tab
trim_trailing_whitespace = true
# The indent size used in the `package.json` file cannot be changed
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
[{*.yml,*.yaml,package.json}]
indent_style = space
indent_size = 2
Add “.devcontainer/devcontainer.json
”
- Create “
.devcontainer/devcontainer.json
” - Copy and paste final
devcontainer.json
from previous section - Commit
Create codespace
Codespace with custom config
- Go to https://github.com/codespaces/new (make sure you are already logged in to github)
- Select repository (“azure-powershell-iac”)
- Select branch (main)
- Select Dev container configuration (in case you have multiple “
.devcontainer.json
”) - Select Region
- Select Machine type (4-core • 8GB RAM • 32GB storage)
- Create codespace
Codespace from repository (default config: 2-core • 4GB RAM)
- Go to github repository “azure-powershell-iac”
- Click “Code” > Click “Codespaces”
- Create codespace on main (you can change branch beforehand if you need to create codespace on other branch)
- “Setting up your codespace” will be shown (it might take some time to launch)
Tools version check
# PowerShell
$PSVersionTable.PSVersion
# Azure cli
az --version
# Git
git --version
# Kubectl
kubectl version -o json
# Helm
helm version
Rename codespace
- Go to https://github.com/codespaces (make sure you are already logged in to github)
- Click three dots on right of target codespace > Rename