Getting Started with Infrahub VSCode Extension
This tutorial will walk you through installing the Infrahub VSCode extension, connecting to your first Infrahub server, and exploring the core features. By the end of this tutorial, you'll be ready to start developing infrastructure schemas and executing GraphQL queries directly from VSCode.
What you'll learn
- How to install the Infrahub extension
- Setting up your first server connection
- Navigating the extension interface
- Creating your first schema
- Executing a GraphQL query
Prerequisites
Before starting this tutorial, ensure you have:
- Visual Studio Code version 1.99.0 or higher installed
- Access to an Infrahub server (either local or remote)
- Basic familiarity with VSCode extensions
- (Optional) An API token for your Infrahub server
Step 1: Install the extension
Option A: from VSCode marketplace
- Open Visual Studio Code
- Click on the Extensions icon in the Activity Bar (
Ctrl+Shift+X
on Windows/Linux,Cmd+Shift+X
on macOS) - Search for "Infrahub"
- Find the extension published by "opsmill"
- Click the Install button
Option B: from a VSIX file
If you have a .vsix
file:
- Open VSCode
- Press
Ctrl+Shift+P
(orCmd+Shift+P
on macOS) to open the Command Palette - Type "Extensions: Install from VSIX"
- Navigate to your
.vsix
file and select it
Step 2: Configure your first server
Once installed, you'll need to connect to an Infrahub server.
-
Open VSCode Settings:
- Press
Ctrl+,
(Windows/Linux) orCmd+,
(macOS) - Or go to File → Preferences → Settings
- Press
-
Search for "Infrahub" in the settings search bar
-
Find the Infrahub-vscode: Servers setting
-
Click Edit in settings.json
-
Add your server configuration:
{
"infrahub-vscode.servers": [
{
"name": "Local Development",
"address": "http://localhost:8000"
}
]
}
If your server requires authentication, add an API token:
{
"infrahub-vscode.servers": [
{
"name": "Local Development",
"address": "http://localhost:8000",
"api_token": "your-api-token-here"
}
]
}
Tip: You can use environment variables for sensitive tokens:
"api_token": "${env:INFRAHUB_API_TOKEN}"
Step 3: Verify your connection
After configuring your server:
- Look at the VSCode status bar (bottom of the window)
- You should see "Infrahub: v[version] (Local Development)"
- If you see "Infrahub: Server unreachable", check your server address and network connection
The extension checks the connection every 10 seconds and updates the status automatically.
Step 4: Explore the extension interface
Activity bar
Click on the Infrahub icon in the Activity Bar (left side of VSCode). You'll see two tree views:
- Infrahub Servers: Shows your configured servers and their branches
- Infrahub YAML: Displays the structure of
.infrahub.yml
files in your workspace
Server tree view
- Expand your server in the Infrahub Servers view
- You'll see a list of branches available on that server
- The default branch is marked with "(default)"
Context menu actions
Right-click on elements in the tree views to see available actions:
- Servers: Create new branches
- Branches: Delete branches
- YAML items: Edit files, execute queries
Step 5: Create your first schema
Let's create a network device schema.
-
Create a new directory called
schemas
in your project root:mkdir schemas
-
Create a new file
schemas/device.yml
: -
Add the following schema definition:
---
version: '1.0'
nodes:
- name: Device
namespace: Network
description: Basic network device
attributes:
- name: hostname
kind: Text
unique: true
description: Device hostname
- name: model
kind: Text
optional: true
description: Device model number
- name: status
kind: Text
default_value: "active"
description: Operational status
-
Save the file. The extension will automatically validate your schema.
-
If there are any validation errors, they'll appear in the Problems panel (
Ctrl+Shift+M
)
Step 6: Execute your first GraphQL query
Now let's query the devices in your Infrahub instance.
- Create a
.infrahub.yml
file in your project root:
---
queries:
- name: list_devices
file: queries/list_devices.gql
- Create the query file
queries/list_devices.gql
:
query ListDevices {
NetworkDevice {
edges {
node {
hostname {
value
}
model {
value
}
status {
value
}
}
}
}
}
-
In the Infrahub YAML tree view, expand your
.infrahub.yml
file -
Find the "list_devices" query
-
Right-click and select Execute GraphQL Query
-
When prompted:
- Select your server
- Choose a branch (usually "main")
-
The query results will appear in a new panel
Step 7: Working with branches
Let's create a new branch for development work.
-
In the Infrahub Servers tree view, right-click on your server
-
Select New Branch
-
Enter a branch name (for example, "feature-network-schema")
-
Optionally add a description
-
The new branch will appear under your server in the tree
You can now execute queries against this branch by selecting it when prompted during query execution.
Next steps
Congratulations! You've successfully:
- Installed and configured the Infrahub VSCode extension
- Connected to an Infrahub server
- Created a schema with validation
- Executed a GraphQL query
- Created a development branch
What to explore next
- How to Configure Multiple Servers: Work with development, staging, and production environments
- Understanding Schema Validation: Deep dive into how the extension validates your schemas
- Extension Commands Reference: Complete list of available commands and settings
Troubleshooting tips
If you encounter issues:
- Server connection fails: Verify the server URL and check if the server is running
- Schema validation errors: Check the Problems panel for specific error messages
- GraphQL queries fail: Ensure the schema is loaded in Infrahub and you're querying the correct branch