Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve records management #24

Merged
merged 15 commits into from
Oct 8, 2024
51 changes: 9 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,17 @@ It creates:
* WWW CNAME to naked;
* all additional records provided.

## Usage on terraform >= v1.0
## Usage
```hcl
provider "cloudflare" {
email = ""
api_key = ""
version = ">= 3.32"
version = "~> 4"
}

module "domain_com" {
source = "tcarrondo/zone/cloudflare"
version = "4.1.0"

domain = "domain.com"

ipv4 = ["1.2.3.4"]
ipv6 = ["2607:f0d0:1002:51::4"]

records = {
mail = {
name = "mail"
value = "1.2.3.4"
},
mx_10 = {
name = "${module.domain_com.domain}"
value = "mail.${module.domain_com.domain}"
type = "MX"
priority = "10"
proxied = false
},
}
}
```

## Usage on terraform <=v0.13
```hcl
provider "cloudflare" {
email = ""
api_key = ""
version = "~> 2.2"
}

module "domain_com" {
source = "tcarrondo/zone/cloudflare"
version = "3.0.0"
version = "~> 4"

domain = "domain.com"

Expand All @@ -62,15 +29,14 @@ module "domain_com" {
{
name = "mail"
value = "1.2.3.4"
type = "A"
},
{
name = "${module.domain_com.domain}"
value = "mail.${module.domain_com.domain}"
type = "MX"
priority = "10"
proxied = false
},
}
]
}
```
Expand All @@ -80,14 +46,14 @@ module "domain_com" {

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_cloudflare"></a> [cloudflare](#requirement\_cloudflare) | >= 3.32 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1 |
| <a name="requirement_cloudflare"></a> [cloudflare](#requirement\_cloudflare) | ~> 4 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_cloudflare"></a> [cloudflare](#provider\_cloudflare) | >= 3.32 |
| <a name="provider_cloudflare"></a> [cloudflare](#provider\_cloudflare) | ~> 4 |

## Modules

Expand All @@ -109,12 +75,13 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_account_name"></a> [account\_name](#input\_account\_name) | Cloudflare account name | `string` | n/a | yes |
| <a name="input_account_name"></a> [account\_name](#input\_account\_name) | Cloudflare account name where the resources will be created. If not specified will use the first account, so it is recommended to be specified when your user has access to more then one account. | `string` | `""` | no |
| <a name="input_domain"></a> [domain](#input\_domain) | Zone domain name | `string` | n/a | yes |
| <a name="input_ipv4"></a> [ipv4](#input\_ipv4) | Naked ipv4 (A) record value | `list(string)` | `[]` | no |
| <a name="input_ipv6"></a> [ipv6](#input\_ipv6) | Naked ipv6 (AAAA) record value | `list(string)` | `[]` | no |
| <a name="input_plan"></a> [plan](#input\_plan) | Plan associated with the zone | `string` | `"free"` | no |
| <a name="input_records"></a> [records](#input\_records) | Other (A, CNAME, MX, TXT) records | `list(map(any))` | `[]` | no |
| <a name="input_www_cname"></a> [www\_cname](#input\_www\_cname) | Custom www CNAME record value | `string` | `""` | no |
| <a name="input_zone_on"></a> [zone\_on](#input\_zone\_on) | Zone creation | `bool` | `true` | no |

## Outputs
Expand Down
4 changes: 2 additions & 2 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ locals {
}

# completed DNS records with (sort of) magic
final_records = { for k, v in var.records :
k => merge(local.record_defaults, v)
final_records = { for record in var.records :
"${record.name}_${record.type}" => merge(local.record_defaults, record)
}

}
28 changes: 20 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ resource "cloudflare_zone" "domain" {
resource "cloudflare_zone_settings_override" "domain" {
zone_id = cloudflare_zone.domain[0].id

depends_on = [cloudflare_zone.domain]

settings {
ssl = "full"
Expand All @@ -19,45 +18,58 @@ resource "cloudflare_zone_settings_override" "domain" {
cache_level = "aggressive"
development_mode = "off"
}

depends_on = [
cloudflare_zone.domain
]
}

# Naked A record
resource "cloudflare_record" "domain_ipv4" {
count = length(var.ipv4)

depends_on = [cloudflare_zone.domain]

zone_id = cloudflare_zone.domain[0].id
name = var.domain
value = var.ipv4[count.index]
content = var.ipv4[count.index]
proxied = "true"
type = "A"

depends_on = [
cloudflare_zone.domain
]
}

# Naked AAAA record
resource "cloudflare_record" "domain_ipv6" {
count = length(var.ipv6)

depends_on = [cloudflare_zone.domain]

zone_id = cloudflare_zone.domain[0].id
name = var.domain
value = var.ipv6[count.index]
content = var.ipv6[count.index]
proxied = "true"
type = "AAAA"

depends_on = [
cloudflare_zone.domain
]
}

# www > naked domain
resource "cloudflare_record" "domain_www" {
count = length(var.ipv4)

depends_on = [cloudflare_zone.domain]

zone_id = cloudflare_zone.domain[0].id
name = "www"
value = var.domain
content = var.www_cname == "" ? var.domain : var.www_cname
proxied = "true"
type = "CNAME"

depends_on = [
cloudflare_zone.domain
]
}

# Other A, CNAME, MX, TXT records
Expand All @@ -67,7 +79,7 @@ resource "cloudflare_record" "records" {

zone_id = cloudflare_zone.domain[0].id
name = each.value.name
value = each.value.value
content = each.value.value
type = each.value.type
priority = each.value.priority
proxied = each.value.proxied
Expand Down
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ variable "ipv6" {
default = []
}

variable "www_cname" {
description = "Custom www CNAME record value"
type = string
default = ""
}

variable "records" {
description = "Other (A, CNAME, MX, TXT) records"
Expand Down
4 changes: 2 additions & 2 deletions terraform.tf → versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = ">= 3.32"
version = "~> 4"
}
}
required_version = ">= 1.0"
required_version = "~> 1"
}
Loading