Deduplicators
An infrastructure usually has several scopes, each managed by a different team. Their modules often create resources in a shared namespace: DNS names, bucket names, database names. Each scope has its own Terraform state, and a state only knows its own resources, so Terraform cannot stop two scopes from claiming the same name.
A deduplicator is a short value that every scope has and that is unique within the infrastructure. Combined with the block name, it gives each block a name that no other block in the infrastructure can have.
Why a validator is not enough
The quick start keeps DNS names unique with a JavaScript validator: before a
block is saved, the validator searches the infrastructure for another block with the same fqdn. This catches
almost every conflict, but not all of them. The check and the save are separate steps, so two users who save blocks
with the same name at the same moment can both pass the check.
A deduplicator closes that gap with database guarantees:
- A block name is unique within its scope.
- A scope’s deduplicator is unique within its infrastructure.
So the pair of block name and deduplicator is unique across the whole infrastructure. Build the external name from that pair, and the database rejects any conflict, concurrent or not.
Settings
| Where | Field | Purpose |
|---|---|---|
| Infrastructure | Deduplicator length (dedup_length) | Every scope’s deduplicator must have exactly this length. Allowed values: 1 to 5. |
| Scope | Deduplicator (deduplicator) | Must be unique within the infrastructure and match the length above. |
| Block template | A field with type: context | Receives the scope’s deduplicator and the block’s name. |
The context field’s name becomes the module input name. With a field named ctx, the module receives
ctx.deduplicator and ctx.block_name. Users never enter these values. See the
context field reference.
Example: DNS names derived from the block
This example reworks the quick start’s DNS template. Instead of asking for an fqdn and checking it with a
validator, the module derives the DNS name from the block name and the scope’s deduplicator.
- Set Deduplicator length to
3on the infrastructure. - Use the
appsscope from the quick start (deduplicatorapp), and create a second scopepaymentswith deduplicatorpay, granted to another team. Set the DNS provider as the default provider in both. - In the data source owned by the infrastructure’s team, add
modules/scoped-dns-record/main.tf:
terraform {
required_providers {
dns = {
source = "hashicorp/dns"
version = "3.6.2"
}
}
}
variable "ctx" {
type = object({
block_name = string
deduplicator = string
})
}
variable "addresses" {
type = list(string)
}
resource "dns_a_record_set" "record" {
zone = "example.com."
name = "${var.ctx.block_name}-${var.ctx.deduplicator}"
addresses = var.addresses
ttl = 300
}
output "fqdn" {
value = "${dns_a_record_set.record.name}.example.com"
}- Create a block template that points to that data source and module path, with these form fields:
[
{"name": "ctx", "type": "context", "mode": "internal"},
{"name": "addresses", "type": "array", "mode": "required", "item_schema": {"type": "string"}}
]- Block names are not limited to DNS characters, so add a validator that keeps them usable as a DNS label. The
limit of 59 characters leaves room for
-and the three-character deduplicator within the 63-character label limit:
function validate(block, context) {
if (!/^[a-z0-9]([a-z0-9-]{0,57}[a-z0-9])?$/.test(block.name)) {
return "Use up to 59 lowercase letters, digits, and hyphens; start and end with a letter or digit";
}
}- Create a block named
checkoutin each scope, with the IPv4 addresses you want.
The apps block’s module receives this context:
ctx = {
block_name = "checkout"
deduplicator = "app"
}| Scope | Block name | Deduplicator | DNS name |
|---|---|---|---|
| apps | checkout | app | checkout-app.example.com |
| payments | checkout | pay | checkout-pay.example.com |
Both teams can pick the same block name without coordinating, and a second checkout block in apps is rejected
when it is saved. No validator needs to search other scopes.
Calculate a diff in each scope and review it before you apply. After the apply, the block’s
Outputs tab shows the resulting fqdn, so its users do not have to
work out the name themselves.
Choose and change deduplicators
- Pick values that fit the naming rules of the external service. InfraBuffet checks the length and uniqueness within an infrastructure, but not the service’s character or total-length limits.
- The guarantee covers only names built from the block name and the deduplicator. Resources created by hand or by other tools can still collide.
- Deduplicators can repeat across infrastructures. If two infrastructures create names in the same namespace, add another distinguishing part in the module.
- Changing a scope’s deduplicator, or a block’s name, changes the names the module creates. The next diff may rename or replace resources. Review it before you apply.