* docs: add examples and recreate TF documentation * update readme * add information about experimental support of firewall zones
9.6 KiB
layout, page_title, description
| layout | page_title | description |
|---|---|---|
| Provider: UniFi | The UniFi provider enables management and automation of Ubiquiti UniFi network infrastructure through Terraform. |
UniFi Provider
The UniFi provider enables infrastructure-as-code management of Ubiquiti's UniFi network controllers and devices. This provider allows you to automate the configuration and management of your UniFi network infrastructure using Terraform.
Supported Features
- Manage UniFi network resources using Infrastructure as Code
- Support for UniFi Controller version 6.x and later
- Compatible with UDM, UDM-Pro, UCG, and standard controller deployments
- Comprehensive resource management including:
- Network/WLAN configuration
- Firewall rules and groups
- Port forwarding
- DNS records
- User management
- Device management
- And more...
Supported Platforms
- UniFi Controller version 6.x and later
- UniFi Dream Machine (UDM)
- UniFi Dream Machine Pro (UDM-Pro)
- UniFi Cloud Gateway (UCG)
- Standard UniFi Controller deployments
Authentication
The provider supports two authentication methods:
- Username/Password Authentication (Traditional method)
- API Key Authentication (Recommended, requires controller version 9.0.108 or later)
!> Hard-coding credentials into any Terraform configuration is not recommended, and risks secret leakage should this file ever be committed to a public version control system.
Security Recommendations
- Use API Key authentication instead of username/password
- Create a dedicated service account for Terraform with appropriate permissions
- Use a Limited Admin role with Local Access Only
- Enable HTTPS and valid SSL certificates for your controller
- Store credentials securely using Terraform variables or environment variables
- Two-factor authentication (2FA) is not supported
Generating an API Key
- Open your Site in UniFi Site Manager
- Click on
Control Plane -> Admins & Users. - Select your Admin user.
- Click
Create API Key. - Add a name for your API Key.
- Copy the key and store it securely, as it will only be displayed once.
- Click
Doneto ensure the key is hashed and securely stored. - Use the API Key 🎉
Example Usage
Using API Key authentication:
provider "unifi" {
api_key = var.api_key # optionally use UNIFI_API_KEY env var
api_url = var.api_url # optionally use UNIFI_API env var
# you may need to allow insecure TLS communications unless you have configured
# certificates for your controller
allow_insecure = var.insecure # optionally use UNIFI_INSECURE env var
# if you are not configuring the default site, you can change the site
# site = "foo" or optionally use UNIFI_SITE env var
}
Using Username/Password authentication:
provider "unifi" {
username = var.username # optionally use UNIFI_USERNAME env var
password = var.password # optionally use UNIFI_PASSWORD env var
api_url = var.api_url # optionally use UNIFI_API env var
# you may need to allow insecure TLS communications unless you have configured
# certificates for your controller
allow_insecure = var.insecure # optionally use UNIFI_INSECURE env var
# if you are not configuring the default site, you can change the site
# site = "foo" or optionally use UNIFI_SITE env var
}
Schema
Optional
allow_insecure(Boolean) Skip verification of TLS certificates of API requests. You may need to set this totrueif you are using your local API without setting up a signed certificate. Can be specified with theUNIFI_INSECUREenvironment variable.api_key(String, Sensitive) API Key for the user accessing the API. Can be specified with theUNIFI_API_KEYenvironment variable. Controller version 9.0.108 or later is required.api_url(String) URL of the controller API. Can be specified with theUNIFI_APIenvironment variable. You should NOT supply the path (/api), the SDK will discover the appropriate paths. This is to support UDM Pro style API paths as well as more standard controller paths.password(String, Sensitive) Password for the user accessing the API. Can be specified with theUNIFI_PASSWORDenvironment variable.site(String) The site in the Unifi controller this provider will manage. Can be specified with theUNIFI_SITEenvironment variable. Default:defaultusername(String) Local user name for the Unifi controller API. Can be specified with theUNIFI_USERNAMEenvironment variable.
Migrating from paultyng/terraform-provider-unifi
This provider is a fork of the original paultyng/terraform-provider-unifi with significant enhancements, improvements, and additional features. If you're currently using the original provider, this guide will help you migrate to this enhanced version.
Key Differences and Improvements
| Feature | paultyng/unifi | filipowm/unifi |
|---|---|---|
| Framework | Terraform SDK v2 | Terraform Plugin Framework |
| Controller Support | UniFi Controller v6.x | UniFi Controller v6.x and later (including v9.x) |
| Authentication | Username/Password | Username/Password and API Key |
| Resource Organization | Flat structure | Organized by domain (settings, dns, etc.) |
| Validation | Basic | Enhanced with custom validators |
| Documentation | Basic | Comprehensive with examples |
| Settings Resources | Limited | Expanded (IPS, Guest Access, etc.) |
| DNS Management | Limited | Enhanced with dedicated resources |
Migration Steps
-
Update Provider Configuration
Change your provider source from
paultyng/unifitofilipowm/unifi:terraform { required_providers { unifi = { source = "filipowm/unifi" version = "~> 0.0.1" # Use the latest version } } } -
Authentication Updates
The provider configuration remains compatible, but now offers API Key authentication as an alternative to username/password:
# Using API Key (recommended for newer controllers) provider "unifi" { api_key = var.api_key api_url = var.api_url allow_insecure = var.insecure } # Using Username/Password (backward compatible) provider "unifi" { username = var.username password = var.password api_url = var.api_url allow_insecure = var.insecure } -
Resource State Migration
Most resources maintain backward compatibility, so your existing state should migrate seamlessly. However, for resources with enhanced functionality, you may need to run
terraform importto reconcile state differences.# Example: Re-importing a network resource terraform import unifi_network.my_network 5dc28e5e9106d105bdc87217 -
Enhanced Resource Configuration
Take advantage of new validation and configuration options:
- Use the new validators for attributes like URLs, emails, and hostnames
- Leverage nested attributes for more organized configuration
- Utilize new settings resources for comprehensive network management
New and Enhanced Resources
New Settings Resources
unifi_setting_auto_speedtest- Manage automatic speed test configurationunifi_setting_country- Configure country settingsunifi_setting_dpi- Manage Deep Packet Inspection settingsunifi_setting_guest_access- Configure guest network access settingsunifi_setting_ips- Manage Intrusion Prevention System settingsunifi_setting_lcd_monitor- Configure LCD monitor settings for devicesunifi_setting_locale- Set locale preferencesunifi_setting_magic_site_to_site_vpn- Configure site-to-site VPNunifi_setting_mgmt- Manage management settingsunifi_setting_network_optimization- Configure network optimizationunifi_setting_ntp- Manage NTP server settingsunifi_setting_radius- Configure RADIUS server settingsunifi_setting_rsyslogd- Manage remote syslog settingsunifi_setting_ssl_inspection- Configure SSL inspectionunifi_setting_teleport- Manage Teleport settingsunifi_setting_usg- Configure UniFi Security Gateway settingsunifi_setting_usw- Manage UniFi Switch settings
Enhanced DNS Management
unifi_dns_record- Create and manage DNS records
Other Improvements
- Enhanced validation for all resources
- Better error messages and diagnostics
- Improved documentation with comprehensive examples
- Support for the latest UniFi Controller features
Developer-Focused Improvements
For developers extending or customizing the provider:
-
Framework Migration
- Migrated from Terraform SDK v2 to Terraform Plugin Framework
- Better type safety and validation capabilities
- Enhanced testing infrastructure
-
Code Organization
- Resources organized by domain in separate packages
- Base types and utilities for consistent implementation
- Custom validators for common validation patterns
-
Testing
- Comprehensive acceptance tests
- Test helpers and utilities
- Improved test stability
Compatibility Notes
- The provider maintains backward compatibility with existing configurations where possible
- Some advanced features may require updates to your configuration
- The provider follows semantic versioning for releases
Getting Help
If you encounter issues during migration:
- Check the documentation
- Review examples in the GitHub repository
- Open an issue on GitHub for assistance