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
# 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
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:
| Attribute | Type | Description |
|---|---|---|
id | string | Image ID. |
image | string | Image identifier (e.g., ubuntu-24.04). |
label | string | Human-readable label (e.g., "Ubuntu 24.04 LTS"). |
description | string | Image description. |
distro | string | Distribution name (e.g., ubuntu, debian, almalinux). |
version | string | OS version. |
family | string | OS family. |
default_user | string | Default SSH user (e.g., ubuntu, root). |
Available Images
The data source returns images including:
| Image | Label | Default User |
|---|---|---|
ubuntu-24.04 | Ubuntu 24.04 LTS | ubuntu |
ubuntu-22.04 | Ubuntu 22.04 LTS | ubuntu |
ubuntu-20.04 | Ubuntu 20.04 LTS | ubuntu |
debian-12 | Debian 12 (Bookworm) | debian |
debian-11 | Debian 11 (Bullseye) | debian |
almalinux-9 | AlmaLinux 9 | almalinux |
almalinux-8 | AlmaLinux 8 | almalinux |
rocky-9 | Rocky Linux 9 | rocky |
rocky-8 | Rocky Linux 8 | rocky |
fedora-40 | Fedora 40 | fedora |
fedora-39 | Fedora 39 | fedora |
alpine-3.20 | Alpine Linux 3.20 | alpine |
alpine-3.19 | Alpine Linux 3.19 | alpine |
danubedata_cache_providers
Retrieves the list of available cache providers.
Example Usage
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:
| Attribute | Type | Description |
|---|---|---|
id | string | Provider ID. |
name | string | Provider name (e.g., "Redis", "Valkey", "Dragonfly"). |
type | string | Provider type identifier (e.g., redis, valkey, dragonfly). |
description | string | Provider description. |
version | string | Default version. |
default_port | number | Default connection port. |
Available Providers
| Provider | Type | Default Port | Description |
|---|---|---|---|
| Redis | redis | 6379 | Industry-standard in-memory data store |
| Valkey | valkey | 6379 | Redis-compatible, community-driven fork |
| Dragonfly | dragonfly | 6379 | Multi-threaded, high-performance cache |
danubedata_database_providers
Retrieves the list of available database engines.
Example Usage
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:
| Attribute | Type | Description |
|---|---|---|
id | string | Provider ID. |
name | string | Provider name (e.g., "PostgreSQL", "MySQL", "MariaDB"). |
type | string | Provider type identifier (e.g., postgresql, mysql, mariadb). |
description | string | Provider description. |
version | string | Default version. |
default_port | number | Default connection port. |
Available Providers
| Provider | Type | Default Port | Versions |
|---|---|---|---|
| PostgreSQL | postgresql | 5432 | 15, 16, 17 |
| MySQL | mysql | 3306 | 8.0, 8.4, 9.0, 9.1 |
| MariaDB | mariadb | 3306 | 10.11, 11.4, 11.6 |
danubedata_ssh_keys
Retrieves the list of SSH keys in your account.
Example Usage
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
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:
| Attribute | Type | Description |
|---|---|---|
id | string | SSH key ID. |
name | string | Key name. |
fingerprint | string | SHA256 fingerprint. |
public_key | string | Public key content. |
created_at | string | Creation timestamp. |
Using Data Sources with Count and For Each
Dynamic VPS Creation
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
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:
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
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
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
- Terraform Resources - Create and manage resources
- Terraform Examples - Real-world examples
- Terraform Overview - Getting started guide