Terraform Data Sources

Data sources allow you to query existing resources and use their attributes in your Terraform configuration. The DanubeData provider includes 4 data sources.

danubedata_vps_images

Retrieves the list of available OS images for VPS instances.

Example Usage

HCL
# Get all available images
data "danubedata_vps_images" "available" {}

# Output all image names
output "available_images" {
  value = data.danubedata_vps_images.available.images[*].image
}

# Use in a VPS resource
resource "danubedata_vps" "web" {
  name       = "web-server"
  image      = data.danubedata_vps_images.available.images[0].image
  datacenter = "fsn1"
  # ...
}

Filtering Images

HCL
data "danubedata_vps_images" "available" {}

# Find Ubuntu images
locals {
  ubuntu_images = [
    for img in data.danubedata_vps_images.available.images :
    img if img.distro == "ubuntu"
  ]
}

output "ubuntu_images" {
  value = local.ubuntu_images[*].image
}

Attribute Reference

The images attribute contains a list of objects with the following attributes:

AttributeTypeDescription
idstringImage ID.
imagestringImage identifier (e.g., ubuntu-24.04).
labelstringHuman-readable label (e.g., "Ubuntu 24.04 LTS").
descriptionstringImage description.
distrostringDistribution name (e.g., ubuntu, debian, almalinux).
versionstringOS version.
familystringOS family.
default_userstringDefault SSH user (e.g., ubuntu, root).

Available Images

The data source returns images including:

ImageLabelDefault User
ubuntu-24.04Ubuntu 24.04 LTSubuntu
ubuntu-22.04Ubuntu 22.04 LTSubuntu
ubuntu-20.04Ubuntu 20.04 LTSubuntu
debian-12Debian 12 (Bookworm)debian
debian-11Debian 11 (Bullseye)debian
almalinux-9AlmaLinux 9almalinux
almalinux-8AlmaLinux 8almalinux
rocky-9Rocky Linux 9rocky
rocky-8Rocky Linux 8rocky
fedora-40Fedora 40fedora
fedora-39Fedora 39fedora
alpine-3.20Alpine Linux 3.20alpine
alpine-3.19Alpine Linux 3.19alpine

danubedata_cache_providers

Retrieves the list of available cache providers.

Example Usage

HCL
data "danubedata_cache_providers" "available" {}

output "cache_providers" {
  value = data.danubedata_cache_providers.available.providers[*].name
}

# Find a specific provider
locals {
  redis_provider = [
    for p in data.danubedata_cache_providers.available.providers :
    p if p.type == "redis"
  ][0]
}

output "redis_default_port" {
  value = local.redis_provider.default_port
}

Attribute Reference

The providers attribute contains a list of objects with the following attributes:

AttributeTypeDescription
idstringProvider ID.
namestringProvider name (e.g., "Redis", "Valkey", "Dragonfly").
typestringProvider type identifier (e.g., redis, valkey, dragonfly).
descriptionstringProvider description.
versionstringDefault version.
default_portnumberDefault connection port.

Available Providers

ProviderTypeDefault PortDescription
Redisredis6379Industry-standard in-memory data store
Valkeyvalkey6379Redis-compatible, community-driven fork
Dragonflydragonfly6379Multi-threaded, high-performance cache

danubedata_database_providers

Retrieves the list of available database engines.

Example Usage

HCL
data "danubedata_database_providers" "available" {}

output "database_engines" {
  value = data.danubedata_database_providers.available.providers[*].name
}

# Find PostgreSQL info
locals {
  postgresql = [
    for p in data.danubedata_database_providers.available.providers :
    p if p.type == "postgresql"
  ][0]
}

output "postgresql_default_port" {
  value = local.postgresql.default_port
}

Attribute Reference

The providers attribute contains a list of objects with the following attributes:

AttributeTypeDescription
idstringProvider ID.
namestringProvider name (e.g., "PostgreSQL", "MySQL", "MariaDB").
typestringProvider type identifier (e.g., postgresql, mysql, mariadb).
descriptionstringProvider description.
versionstringDefault version.
default_portnumberDefault connection port.

Available Providers

ProviderTypeDefault PortVersions
PostgreSQLpostgresql543215, 16, 17
MySQLmysql33068.0, 8.4, 9.0, 9.1
MariaDBmariadb330610.11, 11.4, 11.6

danubedata_ssh_keys

Retrieves the list of SSH keys in your account.

Example Usage

HCL
data "danubedata_ssh_keys" "all" {}

output "ssh_key_names" {
  value = data.danubedata_ssh_keys.all.ssh_keys[*].name
}

# Find a specific key by name
locals {
  deploy_key = [
    for key in data.danubedata_ssh_keys.all.ssh_keys :
    key if key.name == "deploy-key"
  ]
}

# Use existing key in VPS
resource "danubedata_vps" "web" {
  name        = "web-server"
  image       = "ubuntu-24.04"
  datacenter  = "fsn1"
  auth_method = "ssh_key"
  ssh_key_id  = local.deploy_key[0].id
  # ...
}

Checking for Existing Keys

HCL
data "danubedata_ssh_keys" "existing" {}

# Create key only if it doesn't exist
resource "danubedata_ssh_key" "deploy" {
  count = length([
    for k in data.danubedata_ssh_keys.existing.ssh_keys :
    k if k.name == "deploy-key"
  ]) == 0 ? 1 : 0

  name       = "deploy-key"
  public_key = file("~/.ssh/id_ed25519.pub")
}

Attribute Reference

The ssh_keys attribute contains a list of objects with the following attributes:

AttributeTypeDescription
idstringSSH key ID.
namestringKey name.
fingerprintstringSHA256 fingerprint.
public_keystringPublic key content.
created_atstringCreation timestamp.

Using Data Sources with Count and For Each

Dynamic VPS Creation

HCL
variable "servers" {
  type = map(object({
    image    = string
    profile  = string
  }))
  default = {
    "web" = {
      image   = "ubuntu-24.04"
      profile = "small_shared"
    }
    "api" = {
      image   = "debian-12"
      profile = "medium_shared"
    }
  }
}

data "danubedata_ssh_keys" "all" {}

resource "danubedata_vps" "servers" {
  for_each = var.servers

  name             = each.key
  image            = each.value.image
  resource_profile = each.value.profile
  datacenter       = "fsn1"
  auth_method      = "ssh_key"
  ssh_key_id       = data.danubedata_ssh_keys.all.ssh_keys[0].id
}

Conditional Resource Creation

HCL
data "danubedata_vps_images" "available" {}

locals {
  has_alpine = length([
    for img in data.danubedata_vps_images.available.images :
    img if img.distro == "alpine"
  ]) > 0
}

resource "danubedata_vps" "alpine" {
  count = local.has_alpine ? 1 : 0

  name       = "alpine-server"
  image      = "alpine-3.20"
  datacenter = "fsn1"
  # ...
}

Best Practices

Cache Data Source Results

Data sources are read on every plan/apply. For performance, cache results in locals:

HCL
data "danubedata_vps_images" "available" {}

locals {
  images        = data.danubedata_vps_images.available.images
  ubuntu_latest = [for img in local.images : img if img.image == "ubuntu-24.04"][0]
}

Use Meaningful Variable Names

HCL
data "danubedata_ssh_keys" "team_keys" {}

locals {
  production_key = [
    for key in data.danubedata_ssh_keys.team_keys.ssh_keys :
    key if key.name == "production-deploy"
  ][0]
}

Handle Empty Results

HCL
data "danubedata_ssh_keys" "all" {}

locals {
  has_keys   = length(data.danubedata_ssh_keys.all.ssh_keys) > 0
  first_key  = local.has_keys ? data.danubedata_ssh_keys.all.ssh_keys[0] : null
}

Next Steps