{"slug":"terraform-data-sources","title":"Terraform Data Sources","description":"Data sources allow you to query existing resources and use their attributes in your Terraform configuration. The DanubeData provider includes 4 data sources.","section":"Terraform","url":"https://docs.danubedata.ro/terraform-data-sources","markdown_url":"https://docs.danubedata.ro/terraform-data-sources.md","breadcrumbs":[{"title":"Terraform","slug":null},{"title":"Data Sources","slug":"terraform-data-sources"}],"headings":[{"level":1,"title":"Terraform Data Sources","id":"terraform-data-sources"},{"level":2,"title":"danubedatavpsimages","id":"danubedatavpsimages"},{"level":3,"title":"Example Usage","id":"example-usage"},{"level":3,"title":"Filtering Images","id":"filtering-images"},{"level":3,"title":"Attribute Reference","id":"attribute-reference"},{"level":3,"title":"Available Images","id":"available-images"},{"level":2,"title":"danubedatacacheproviders","id":"danubedatacacheproviders"},{"level":3,"title":"Example Usage","id":"example-usage"},{"level":3,"title":"Attribute Reference","id":"attribute-reference"},{"level":3,"title":"Available Providers","id":"available-providers"},{"level":2,"title":"danubedatadatabaseproviders","id":"danubedatadatabaseproviders"},{"level":3,"title":"Example Usage","id":"example-usage"},{"level":3,"title":"Attribute Reference","id":"attribute-reference"},{"level":3,"title":"Available Providers","id":"available-providers"},{"level":2,"title":"danubedatasshkeys","id":"danubedatasshkeys"},{"level":3,"title":"Example Usage","id":"example-usage"},{"level":3,"title":"Checking for Existing Keys","id":"checking-for-existing-keys"},{"level":3,"title":"Attribute Reference","id":"attribute-reference"},{"level":2,"title":"Resource List Data Sources","id":"resource-list-data-sources"},{"level":3,"title":"Example Usage","id":"example-usage"},{"level":2,"title":"Using Data Sources with Count and For Each","id":"using-data-sources-with-count-and-for-each"},{"level":3,"title":"Dynamic VPS Creation","id":"dynamic-vps-creation"},{"level":3,"title":"Conditional Resource Creation","id":"conditional-resource-creation"},{"level":2,"title":"Best Practices","id":"best-practices"},{"level":3,"title":"Cache Data Source Results","id":"cache-data-source-results"},{"level":3,"title":"Use Meaningful Variable Names","id":"use-meaningful-variable-names"},{"level":3,"title":"Handle Empty Results","id":"handle-empty-results"},{"level":2,"title":"Next Steps","id":"next-steps"}],"format":"markdown","word_count":1236,"content":"# Terraform Data Sources\n\nData sources allow you to query existing resources and use their attributes in your Terraform configuration. The DanubeData provider includes 4 data sources.\n\n## danubedata_vps_images\n\nRetrieves the list of available OS images for VPS instances.\n\n### Example Usage\n\n```hcl\n# Get all available images\ndata \"danubedata_vps_images\" \"available\" {}\n\n# Output all image names\noutput \"available_images\" {\n  value = data.danubedata_vps_images.available.images[*].image\n}\n\n# Use in a VPS resource\nresource \"danubedata_vps\" \"web\" {\n  name       = \"web-server\"\n  image      = data.danubedata_vps_images.available.images[0].image\n  datacenter = \"fsn1\"\n  # ...\n}\n```\n\n### Filtering Images\n\n```hcl\ndata \"danubedata_vps_images\" \"available\" {}\n\n# Find Ubuntu images\nlocals {\n  ubuntu_images = [\n    for img in data.danubedata_vps_images.available.images :\n    img if img.distro == \"ubuntu\"\n  ]\n}\n\noutput \"ubuntu_images\" {\n  value = local.ubuntu_images[*].image\n}\n```\n\n### Attribute Reference\n\nThe `images` attribute contains a list of objects with the following attributes:\n\n| Attribute | Type | Description |\n|-----------|------|-------------|\n| `id` | string | Image ID. |\n| `image` | string | Image identifier (e.g., `ubuntu-24.04`). |\n| `label` | string | Human-readable label (e.g., \"Ubuntu 24.04 LTS\"). |\n| `description` | string | Image description. |\n| `distro` | string | Distribution name (e.g., `ubuntu`, `debian`, `alpine`). |\n| `version` | string | OS version. |\n| `family` | string | OS family. |\n| `default_user` | string | Default SSH user (`root` for Linux, `Administrator` for Windows). |\n\n### Available Images\n\nThe data source returns images including:\n\n| Image | Label | Default User |\n|-------|-------|--------------|\n| `ubuntu-26.04` | Ubuntu 26.04 LTS | `root` |\n| `ubuntu-24.04` | Ubuntu 24.04 LTS | `root` |\n| `ubuntu-22.04` | Ubuntu 22.04 LTS | `root` |\n| `debian-13` | Debian 13 (Trixie) | `root` |\n| `debian-12` | Debian 12 (Bookworm) | `root` |\n| `debian-11` | Debian 11 (Bullseye) | `root` |\n| `alma-9` | AlmaLinux 9 | `root` |\n| `alma-8` | AlmaLinux 8 | `root` |\n| `rocky-9` | Rocky Linux 9 | `root` |\n| `rocky-8` | Rocky Linux 8 | `root` |\n| `fedora-42` | Fedora 42 | `root` |\n| `fedora-41` | Fedora 41 | `root` |\n| `alpine-3.22` | Alpine Linux 3.22 | `root` |\n| `alpine-3.21` | Alpine Linux 3.21 | `root` |\n| `alpine-3.20` | Alpine Linux 3.20 | `root` |\n| `windows-server-2025` | Windows Server 2025 | `Administrator` |\n\n---\n\n## danubedata_cache_providers\n\nRetrieves the list of available cache providers.\n\n### Example Usage\n\n```hcl\ndata \"danubedata_cache_providers\" \"available\" {}\n\noutput \"cache_providers\" {\n  value = data.danubedata_cache_providers.available.providers[*].name\n}\n\n# Find a specific provider\nlocals {\n  redis_provider = [\n    for p in data.danubedata_cache_providers.available.providers :\n    p if p.type == \"redis\"\n  ][0]\n}\n\noutput \"redis_default_port\" {\n  value = local.redis_provider.default_port\n}\n```\n\n### Attribute Reference\n\nThe `providers` attribute contains a list of objects with the following attributes:\n\n| Attribute | Type | Description |\n|-----------|------|-------------|\n| `id` | string | Provider ID. |\n| `name` | string | Provider name (e.g., \"Redis\", \"Valkey\", \"Dragonfly\"). |\n| `type` | string | Provider type identifier (e.g., `redis`, `valkey`, `dragonfly`). |\n| `description` | string | Provider description. |\n| `version` | string | Default version. |\n| `default_port` | number | Default connection port. |\n\n### Available Providers\n\n| Provider | Type | Default Port | Description |\n|----------|------|--------------|-------------|\n| Redis | `redis` | 6379 | Industry-standard in-memory data store |\n| Valkey | `valkey` | 6379 | Redis-compatible, community-driven fork |\n| Dragonfly | `dragonfly` | 6379 | Multi-threaded, high-performance cache |\n\n---\n\n## danubedata_database_providers\n\nRetrieves the list of available database engines.\n\n### Example Usage\n\n```hcl\ndata \"danubedata_database_providers\" \"available\" {}\n\noutput \"database_engines\" {\n  value = data.danubedata_database_providers.available.providers[*].name\n}\n\n# Find PostgreSQL info\nlocals {\n  postgresql = [\n    for p in data.danubedata_database_providers.available.providers :\n    p if p.type == \"postgresql\"\n  ][0]\n}\n\noutput \"postgresql_default_port\" {\n  value = local.postgresql.default_port\n}\n```\n\n### Attribute Reference\n\nThe `providers` attribute contains a list of objects with the following attributes:\n\n| Attribute | Type | Description |\n|-----------|------|-------------|\n| `id` | string | Provider ID. |\n| `name` | string | Provider name (e.g., \"PostgreSQL\", \"MySQL\", \"MariaDB\"). |\n| `type` | string | Provider type identifier (e.g., `postgresql`, `mysql`, `mariadb`). |\n| `description` | string | Provider description. |\n| `version` | string | Default version. |\n| `default_port` | number | Default connection port. |\n\n### Available Providers\n\n| Provider | Type | Default Port | Versions |\n|----------|------|--------------|----------|\n| PostgreSQL | `postgresql` | 5432 | 15, 16, 17, 18 |\n| MySQL | `mysql` | 3306 | 8.0, 8.4, 9.0, 9.1 |\n| MariaDB | `mariadb` | 3306 | 10.11, 11.4, 11.6 |\n\n---\n\n## danubedata_ssh_keys\n\nRetrieves the list of SSH keys in your account.\n\n### Example Usage\n\n```hcl\ndata \"danubedata_ssh_keys\" \"all\" {}\n\noutput \"ssh_key_names\" {\n  value = data.danubedata_ssh_keys.all.ssh_keys[*].name\n}\n\n# Find a specific key by name\nlocals {\n  deploy_key = [\n    for key in data.danubedata_ssh_keys.all.ssh_keys :\n    key if key.name == \"deploy-key\"\n  ]\n}\n\n# Use existing key in VPS\nresource \"danubedata_vps\" \"web\" {\n  name        = \"web-server\"\n  image       = \"ubuntu-24.04\"\n  datacenter  = \"fsn1\"\n  auth_method = \"ssh_key\"\n  ssh_key_id  = local.deploy_key[0].id\n  # ...\n}\n```\n\n### Checking for Existing Keys\n\n```hcl\ndata \"danubedata_ssh_keys\" \"existing\" {}\n\n# Create key only if it doesn't exist\nresource \"danubedata_ssh_key\" \"deploy\" {\n  count = length([\n    for k in data.danubedata_ssh_keys.existing.ssh_keys :\n    k if k.name == \"deploy-key\"\n  ]) == 0 ? 1 : 0\n\n  name       = \"deploy-key\"\n  public_key = file(\"~/.ssh/id_ed25519.pub\")\n}\n```\n\n### Attribute Reference\n\nThe `ssh_keys` attribute contains a list of objects with the following attributes:\n\n| Attribute | Type | Description |\n|-----------|------|-------------|\n| `id` | string | SSH key ID. |\n| `name` | string | Key name. |\n| `fingerprint` | string | SHA256 fingerprint. |\n| `public_key` | string | Public key content. |\n| `created_at` | string | Creation timestamp. |\n\n---\n\n## Resource List Data Sources\n\nIn addition to the lookup data sources above, the provider exposes a data source for each resource type that returns all of your existing resources of that type. Each returns a single list attribute you can iterate over.\n\n| Data Source | List Attribute | Notable Item Fields | Filter Arguments |\n|-------------|----------------|---------------------|------------------|\n| `danubedata_vpss` | `instances` | `id`, `name`, `status`, `image`, `datacenter` | none |\n| `danubedata_databases` | `instances` | `id`, `name`, `status`, `engine`, `version` | none |\n| `danubedata_caches` | `instances` | `id`, `name`, `status`, `cache_provider`, `version` | none |\n| `danubedata_serverless_containers` | `containers` | `id`, `name`, `status`, `deployment_type`, `image_url` | none |\n| `danubedata_static_sites` | `sites` | `id`, `name`, `status`, `url` | `team_id` (required) |\n| `danubedata_storage_buckets` | `buckets` | `id`, `name`, `display_name`, `status`, `region` | none |\n| `danubedata_storage_access_keys` | `keys` | `id`, `name`, `access_key_id`, `status`, `access_type` | none |\n| `danubedata_firewalls` | `firewalls` | `id`, `name`, `description`, `status`, `default_action` | none |\n| `danubedata_parameter_groups` | `groups` | `id`, `name`, `type`, `provider_type` | `type`, `provider_type` (optional) |\n| `danubedata_vps_snapshots` | `snapshots` | `id`, `name`, `status`, `vps_instance_id` | none |\n| `danubedata_database_snapshots` | `snapshots` | `id`, `name`, `status`, `database_instance_id` | none |\n| `danubedata_cache_snapshots` | `snapshots` | `id`, `name`, `status`, `cache_instance_id` | none |\n\n### Example Usage\n\n```hcl\n# List all buckets and output their names\ndata \"danubedata_storage_buckets\" \"all\" {}\n\noutput \"bucket_names\" {\n  value = [for b in data.danubedata_storage_buckets.all.buckets : b.name]\n}\n\n# Static sites are scoped to a team\ndata \"danubedata_static_sites\" \"team\" {\n  team_id = 42\n}\n\n# Filter parameter groups by provider\ndata \"danubedata_parameter_groups\" \"redis\" {\n  type          = \"cache\"\n  provider_type = \"redis\"\n}\n```\n\n## Using Data Sources with Count and For Each\n\n### Dynamic VPS Creation\n\n```hcl\nvariable \"servers\" {\n  type = map(object({\n    image    = string\n    profile  = string\n  }))\n  default = {\n    \"web\" = {\n      image   = \"ubuntu-24.04\"\n      profile = \"small_shared\"\n    }\n    \"api\" = {\n      image   = \"debian-12\"\n      profile = \"medium_shared\"\n    }\n  }\n}\n\ndata \"danubedata_ssh_keys\" \"all\" {}\n\nresource \"danubedata_vps\" \"servers\" {\n  for_each = var.servers\n\n  name             = each.key\n  image            = each.value.image\n  resource_profile = each.value.profile\n  datacenter       = \"fsn1\"\n  auth_method      = \"ssh_key\"\n  ssh_key_id       = data.danubedata_ssh_keys.all.ssh_keys[0].id\n}\n```\n\n### Conditional Resource Creation\n\n```hcl\ndata \"danubedata_vps_images\" \"available\" {}\n\nlocals {\n  has_alpine = length([\n    for img in data.danubedata_vps_images.available.images :\n    img if img.distro == \"alpine\"\n  ]) > 0\n}\n\nresource \"danubedata_vps\" \"alpine\" {\n  count = local.has_alpine ? 1 : 0\n\n  name       = \"alpine-server\"\n  image      = \"alpine-3.20\"\n  datacenter = \"fsn1\"\n  # ...\n}\n```\n\n---\n\n## Best Practices\n\n### Cache Data Source Results\n\nData sources are read on every plan/apply. For performance, cache results in locals:\n\n```hcl\ndata \"danubedata_vps_images\" \"available\" {}\n\nlocals {\n  images        = data.danubedata_vps_images.available.images\n  ubuntu_latest = [for img in local.images : img if img.image == \"ubuntu-24.04\"][0]\n}\n```\n\n### Use Meaningful Variable Names\n\n```hcl\ndata \"danubedata_ssh_keys\" \"team_keys\" {}\n\nlocals {\n  production_key = [\n    for key in data.danubedata_ssh_keys.team_keys.ssh_keys :\n    key if key.name == \"production-deploy\"\n  ][0]\n}\n```\n\n### Handle Empty Results\n\n```hcl\ndata \"danubedata_ssh_keys\" \"all\" {}\n\nlocals {\n  has_keys   = length(data.danubedata_ssh_keys.all.ssh_keys) > 0\n  first_key  = local.has_keys ? data.danubedata_ssh_keys.all.ssh_keys[0] : null\n}\n```\n\n---\n\n## Next Steps\n\n- [Terraform Resources](https://docs.danubedata.ro/terraform-resources) - Create and manage resources\n- [Terraform Examples](https://docs.danubedata.ro/terraform-examples) - Real-world examples\n- [Terraform Overview](https://docs.danubedata.ro/terraform-overview) - Getting started guide\n","prev":{"title":"Resources","slug":"terraform-resources","url":"https://docs.danubedata.ro/terraform-resources","markdown_url":"https://docs.danubedata.ro/terraform-resources.md","json_url":"https://docs.danubedata.ro/terraform-resources.json"},"next":{"title":"Examples","slug":"terraform-examples","url":"https://docs.danubedata.ro/terraform-examples","markdown_url":"https://docs.danubedata.ro/terraform-examples.md","json_url":"https://docs.danubedata.ro/terraform-examples.json"},"index_url":"https://docs.danubedata.ro/index.json"}