Skip to content

Commit

Permalink
Add records (#1)
Browse files Browse the repository at this point in the history
* Add simple records

* Move iteration over records to locals

* Fix fmt

* Add collection submodule

* Use collection submodule for the same name/type

* Add examples
  • Loading branch information
snovikov authored Nov 29, 2021
1 parent e5a7086 commit a778d4c
Show file tree
Hide file tree
Showing 17 changed files with 397 additions and 17 deletions.
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ test: _pull-tf
echo "------------------------------------------------------------"; \
if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
init \
-verify-plugins=true \
-lock=false \
-upgrade=true \
-upgrade \
-reconfigure \
-input=false \
-get-plugins=true \
-get=true \
.; then \
-get=true; \
then \
echo "OK"; \
else \
echo "Failed"; \
Expand Down
68 changes: 56 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# terraform-module-template
Template for Terraform modules

<!-- Uncomment and replace with your module name
[![lint](https://github.com/flaconi/<MODULENAME>/workflows/lint/badge.svg)](https://github.com/flaconi/<MODULENAME>/actions?query=workflow%3Alint)
[![test](https://github.com/flaconi/<MODULENAME>/workflows/test/badge.svg)](https://github.com/flaconi/<MODULENAME>/actions?query=workflow%3Atest)
[![Tag](https://img.shields.io/github/tag/flaconi/<MODULENAME>.svg)](https://github.com/flaconi/<MODULENAME>/releases)
-->
# terraform-cloudflare-records

Terraform module to create set of DNS records in Cloudflare Hosted Zone.

[![lint](https://github.com/flaconi/terraform-cloudflare-records/workflows/lint/badge.svg)](https://github.com/flaconi/terraform-cloudflare-records/actions?query=workflow%3Alint)
[![test](https://github.com/flaconi/terraform-cloudflare-records/workflows/test/badge.svg)](https://github.com/flaconi/terraform-cloudflare-records/actions?query=workflow%3Atest)
[![Tag](https://img.shields.io/github/tag/flaconi/terraform-cloudflare-records.svg)](https://github.com/flaconi/terraform-cloudflare-records/releases)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)

<!-- TFDOCS_HEADER_START -->
Expand All @@ -16,7 +15,9 @@ Template for Terraform modules
<!-- TFDOCS_PROVIDER_START -->
## Providers

No providers.
| Name | Version |
|------|---------|
| <a name="provider_cloudflare"></a> [cloudflare](#provider\_cloudflare) | ~> 3.2 |

<!-- TFDOCS_PROVIDER_END -->

Expand All @@ -26,24 +27,67 @@ No providers.
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.0 |
| <a name="requirement_cloudflare"></a> [cloudflare](#requirement\_cloudflare) | ~> 3.2 |

<!-- TFDOCS_REQUIREMENTS_END -->

<!-- TFDOCS_INPUTS_START -->
## Required Inputs

No required inputs.
The following input variables are required:

### <a name="input_api_token"></a> [api\_token](#input\_api\_token)

Description: The Cloudflare API token.

Type: `string`

### <a name="input_domain"></a> [domain](#input\_domain)

Description: Cloudflare domain name to create

Type: `string`

## Optional Inputs

No optional inputs.
The following input variables are optional (have default values):

### <a name="input_records"></a> [records](#input\_records)

Description: List of names to create

Type:

```hcl
list(object({
name = string
value = string
type = string
ttl = number
proxied = bool
priority = number
}))
```

Default: `[]`

### <a name="input_allow_overwrite"></a> [allow\_overwrite](#input\_allow\_overwrite)

Description: Allow override existing records

Type: `bool`

Default: `false`

<!-- TFDOCS_INPUTS_END -->

<!-- TFDOCS_OUTPUTS_START -->
## Outputs

No outputs.
| Name | Description |
|------|-------------|
| <a name="output_records"></a> [records](#output\_records) | Cloudflare Zone DNS Records |
| <a name="output_zone_id"></a> [zone\_id](#output\_zone\_id) | Cloudflare Zone ID |

<!-- TFDOCS_OUTPUTS_END -->

Expand Down
3 changes: 3 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "cloudflare_zone" "this" {
name = var.domain
}
82 changes: 82 additions & 0 deletions examples/records/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Example

This example will create multiple DNS records.

Output for `example.com`:

```hcl
records = {
"A_mytestdomain.example.com" = {
"4.4.4.4" = {
"created" = "2021-11-26T15:36:09.973899Z"
"id" = "90bc69d4b5e5bb5c7a58858d7156cf36"
}
"8.8.8.8" = {
"created" = "2021-11-26T15:36:09.764104Z"
"id" = "03aae5145060e77dfc903e4a41ad7f4d"
}
}
"CNAME_myproxieddomain.example.com" = {
"example.com" = {
"created" = "2021-11-26T15:36:10.544251Z"
"id" = "20cfad416b4dcd3fdbb16d3d8e7ce2d3"
}
}
"MX_mymaildomain.example.com" = {
"mail1.mx.maildomainexample.com" = {
"created" = "2021-11-26T15:36:10.323258Z"
"id" = "61cc65ae9845eeb5d0a253f03cac2acd"
}
"mail2.mx.maildomainexample.com" = {
"created" = "2021-11-26T15:36:10.189034Z"
"id" = "e16fd484fa77f12049af5e8c66958626"
}
}
"NS_mynsdomain.example.com" = {
"ns1.mytestdns.com" = {
"created" = "2021-11-26T15:36:11.447047Z"
"id" = "b2ba6ade689541d5afff586bdacbfd5f"
}
"ns2.mytestdns.com" = {
"created" = "2021-11-26T15:36:11.126715Z"
"id" = "8df957fd25f7a2212fcae8ba00cd6b39"
}
}
}
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

No requirements.

## Providers

No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_records"></a> [records](#module\_records) | ./../../ | n/a |

## Resources

No resources.

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_api_token"></a> [api\_token](#input\_api\_token) | The Cloudflare API token. | `string` | n/a | yes |
| <a name="input_domain"></a> [domain](#input\_domain) | Cloudflare domain name to create | `string` | `"example.com"` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_records"></a> [records](#output\_records) | Cloudflare Zone DNS Records |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Copyright (c) 2021 **[Flaconi GmbH](https://github.com/flaconi)**
67 changes: 67 additions & 0 deletions examples/records/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
locals {
records = [
{
name = "myproxieddomain"
value = "example.com"
type = "CNAME"
ttl = 1
proxied = true
priority = null
},
{
name = "mytestdomain"
value = "8.8.8.8"
type = "A"
ttl = 600
proxied = false
priority = null
},
{
name = "mytestdomain"
value = "4.4.4.4"
type = "A"
ttl = 600
proxied = false
priority = null
},
{
name = "mymaildomain"
value = "mail1.mx.maildomainexample.com"
type = "MX"
ttl = 300
proxied = false
priority = 10
},
{
name = "mymaildomain"
value = "mail2.mx.maildomainexample.com"
type = "MX"
ttl = 300
proxied = false
priority = 20
},
{
name = "mynsdomain"
value = "ns1.mytestdns.com"
type = "NS"
ttl = 300
proxied = false
priority = null
},
{
name = "mynsdomain"
value = "ns2.mytestdns.com"
type = "NS"
ttl = 300
proxied = false
priority = null
},
]
}

module "records" {
source = "./../../"
api_token = var.api_token
domain = var.domain
records = local.records
}
4 changes: 4 additions & 0 deletions examples/records/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "records" {
description = "Cloudflare Zone DNS Records"
value = module.records.records
}
10 changes: 10 additions & 0 deletions examples/records/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "api_token" {
description = "The Cloudflare API token."
type = string
}

variable "domain" {
description = "Cloudflare domain name to create"
type = string
default = "example.com"
}
22 changes: 22 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
locals {
# Grouping records by type and name
collections = { for r in var.records : "${r.type}_${r.name}.${var.domain}" => r... }
}

module "records" {
source = "./modules/record_collection"
for_each = local.collections

zone_id = data.cloudflare_zone.this.id
name = each.value[0].name
type = each.value[0].type
values = [for r in each.value :
{
value = r.value
ttl = r.ttl
proxied = r.proxied
priority = r.priority
}
]
allow_overwrite = var.allow_overwrite
}
47 changes: 47 additions & 0 deletions modules/record_collection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Submodule record_collection

Terraform submodule to create a list of Cloudflare DNS records grouped by type and name.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

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

## Providers

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

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [cloudflare_record.this](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/record) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_zone_id"></a> [zone\_id](#input\_zone\_id) | Cloudflare Hosted Zone ID | `string` | n/a | yes |
| <a name="input_name"></a> [name](#input\_name) | Cloudflare Hosted Zone Record Name | `string` | n/a | yes |
| <a name="input_type"></a> [type](#input\_type) | Cloudflare Hosted Zone Record Type | `string` | n/a | yes |
| <a name="input_values"></a> [values](#input\_values) | List of values to create | <pre>list(object({<br> value = string<br> ttl = number<br> proxied = bool<br> priority = number<br> }))</pre> | `[]` | no |
| <a name="input_allow_overwrite"></a> [allow\_overwrite](#input\_allow\_overwrite) | Allow override existing records | `bool` | `false` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_values"></a> [values](#output\_values) | Cloudflare Zone DNS Records |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Copyright (c) 2021 **[Flaconi GmbH](https://github.com/flaconi)**
13 changes: 13 additions & 0 deletions modules/record_collection/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "cloudflare_record" "this" {
count = length(var.values)

zone_id = var.zone_id
type = var.type
name = var.name
value = var.values[count.index].value
ttl = var.values[count.index].ttl
proxied = var.values[count.index].proxied
priority = var.values[count.index].priority

allow_overwrite = var.allow_overwrite
}
4 changes: 4 additions & 0 deletions modules/record_collection/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "values" {
description = "Cloudflare Zone DNS Records"
value = { for v in cloudflare_record.this : v.value => { id = v.id, created = v.created_on } }
}
Loading

0 comments on commit a778d4c

Please sign in to comment.