A lightweight Go library for detecting cloud providers and accessing basic instance metadata.
- Auto-detection - Automatically detects cloud provider
- Multi-cloud - Supports multiple cloud providers
- Zero dependencies - Only uses Go standard library
go get github.com/nickgarlis/go-cloudmetapackage main
import (
"context"
"fmt"
"log"
"github.com/nickgarlis/go-cloudmeta"
)
func main() {
ctx := context.Background()
provider, err := cloudmeta.GetProvider(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Running on: %s\n", provider.Name())
privateIP, err := provider.GetPrivateIPv4(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Private IPv4: %s\n", privateIP)
}type Provider interface {
Name() string
GetInstanceID(ctx context.Context) (string, error)
GetPrivateIPv4(ctx context.Context) (string, error)
GetPublicIPv4(ctx context.Context) (string, error)
GetHostname(ctx context.Context) (string, error)
GetPrimaryIPv6(ctx context.Context) (string, error)
}provider, err := cloudmeta.GetProvider(ctx)
if err != nil {
if errors.Is(err, cloudmeta.ErrUnknownProvider) {
// Handle unknown provider
}
// Handle other errors
}
ipv6, err := provider.GetPrimaryIPv6(ctx)
if err != nil {
if errors.Is(err, cloudmeta.ErrNotFound) {
// Handle not found case
}
// Handle error
}- AWS
- Google Cloud Platform
- Microsoft Azure
- DigitalOcean
- Hetzner Cloud
- Oracle Cloud Infrastructure
- OpenStack-based clouds
MIT License - see LICENSE file for details.