Grafana: Automating with the CLI

Start Grafana, retrieve its credentials, and programmatically update its configuration using the Stackhero CLI

👋 Welcome to the Stackhero documentation!

Stackhero provides you with a ready-to-use Grafana cloud environment, designed for fast setup and reliable performance:

  • Unlimited users, dashboards, and alerts: scale your monitoring as needed, with no artificial restrictions.
  • A dedicated SMTP email server is included, so you can send as many notifications as you want, with no quotas to monitor.
  • One-click plugin installation makes it easy to add new features to Grafana.
  • Built-in SSO support for GitLab, GitHub, Google, and any generic OAuth2 provider. User management remains simple and secure.
  • One-click updates keep your Grafana instance up to date, with no downtime or maintenance headaches.
  • Use your own custom domain with automated HTTPS provisioning for secure, branded access (for example, https://grafana.your-company.com).
  • High performance and enhanced security are included by default, running on your own private, dedicated infrastructure.

Get started faster and save time on setup: a secure Grafana cloud hosting instance is ready in about 5 minutes.

This guide explains how to create a Grafana service, access its credentials, and update its configuration entirely from the command line, without any interaction with the dashboard. This approach is ideal for scripts, CI pipelines, and AI agents.

We will use the Stackhero CLI for all operations. If you haven't already installed it, you can do so with:

curl -fsSL https://www.stackhero.io/install.sh | sh

The simplest way to get started is to log in through your browser. When you run the login command, the CLI opens a web page where you can grant access. No passwords or 2FA codes are entered directly into the CLI.

stackhero login

After logging in, your credentials are stored locally and will be used automatically for future CLI commands.

For fully automated environments such as scripts or CI pipelines, you may prefer a non-interactive access token. You can generate one from your dashboard (Account > Access tokens), then export it as an environment variable. The CLI and any scripts you run will automatically use it.

export STACKHERO_TOKEN="usr-xxxxxx:your-token"

Next, you'll want to list the Grafana service stores available to your account. The CLI accepts the store name (grafana) directly, so there's no need to look up or copy any IDs.

# List Grafana service stores (add --organization if you manage more than one)
stackhero services-store-list --name="grafana"

You can use the store name grafana in subsequent commands. It always points to the latest stable version.

# List instance sizes for your service store (use the NAME column for --instance)
stackhero instances-store-list --service-store=grafana

# List available regions (names like "europe")
stackhero regions-list

Below is a sample script that creates a stack, adds your Grafana service to it, waits for it to start, retrieves its configuration (including generated credentials), and then applies a new configuration.

#!/bin/bash
set -e

export STACKHERO_TOKEN="usr-xxxxxx:your-token"

serviceStore="grafana"   # The Grafana service store name (see step 2)
instance="..."                # An instance size from step 3
region="europe"               # A region name from step 3

# Create a stack for your service (uses your default organization; add --organization if needed)
stackId=$(stackhero --format=script stack-create \
  --name="My Grafana stack")
echo "Stack created: ${stackId}"

# Add Grafana to the stack (names are resolved automatically)
serviceId=$(stackhero --format=script service-add \
  --stack="My Grafana stack" \
  --service-store="${serviceStore}" \
  --instance="${instance}" \
  --region="${region}")
echo "Service added: grafana"

# Wait for the service to be fully running (this may take a couple of minutes)
stackhero service-wait-for --service="grafana"

# Retrieve the service configuration, including generated credentials
stackhero service-configuration-get --service="grafana" --format=json

The service-configuration-get command returns the complete configuration for your service, including auto-generated passwords and connection details. The output is in JSON format, making it easy to use in scripts and automation.

stackhero service-configuration-get --service=svc-xxxxxx --format=json

You can review a sample configuration schema and then apply your own settings. When you update the configuration, the service may restart to apply the changes.

# View the configuration schema and an example for your service
stackhero service-configuration-example --service=svc-xxxxxx

# Apply a custom configuration (the service restarts if needed)
stackhero service-configuration-set \
  --service=svc-xxxxxx \
  --configuration='{ "...": "..." }'

# Wait for the new configuration to be applied
stackhero service-wait-for --service=svc-xxxxxx

When a new version of Grafana is available, you can view and install it from the command line as well:

# Show the current version, available updates, and their severity
stackhero service-updates-list --service=svc-xxxxxx

# Install the next update (a backup is taken first), then wait for the service to be running again
stackhero service-update --service=svc-xxxxxx --confirm
stackhero service-wait-for --service=svc-xxxxxx

That's it. You've now seen the full lifecycle: start a service, retrieve its credentials, reconfigure it, and keep it up to date, all in a scriptable, automated way. To learn more, see the full CLI documentation, which also covers the non-interactive STACKHERO_TOKEN authentication shown here. To run your tests against a real Grafana in GitHub Actions or GitLab CI, see Start services for your CI tests.