Commit 47262df8 authored by Anton Babenko's avatar Anton Babenko Committed by GitHub

fix: Fixed records with different length (#54)

parent be1d1f8c
...@@ -63,16 +63,10 @@ module "records" { ...@@ -63,16 +63,10 @@ module "records" {
} }
``` ```
Note that `depends_on` in modules is available since Terraform 0.13.
## Examples ## Examples
- [Complete Route53 zones and records example](https://github.com/terraform-aws-modules/terraform-aws-route53/tree/master/examples/complete) which shows how to create Route53 records of various types like S3 bucket and CloudFront distribution. - [Complete Route53 zones and records example](https://github.com/terraform-aws-modules/terraform-aws-route53/tree/master/examples/complete) which shows how to create Route53 records of various types like S3 bucket and CloudFront distribution.
## Conditional creation
Sometimes you need to have a way to create resources conditionally but Terraform does not allow to use `count` inside `module` block, so the solution is to specify argument `create`.
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
......
...@@ -37,7 +37,9 @@ Note that this example may create resources which cost money. Run `terraform des ...@@ -37,7 +37,9 @@ Note that this example may create resources which cost money. Run `terraform des
| <a name="module_cloudfront"></a> [cloudfront](#module\_cloudfront) | terraform-aws-modules/cloudfront/aws | | | <a name="module_cloudfront"></a> [cloudfront](#module\_cloudfront) | terraform-aws-modules/cloudfront/aws | |
| <a name="module_disabled_records"></a> [disabled\_records](#module\_disabled\_records) | ../../modules/records | | | <a name="module_disabled_records"></a> [disabled\_records](#module\_disabled\_records) | ../../modules/records | |
| <a name="module_records"></a> [records](#module\_records) | ../../modules/records | | | <a name="module_records"></a> [records](#module\_records) | ../../modules/records | |
| <a name="module_records_with_lists"></a> [records\_with\_lists](#module\_records\_with\_lists) | ../../modules/records | |
| <a name="module_records_with_terragrunt"></a> [records\_with\_terragrunt](#module\_records\_with\_terragrunt) | ../../modules/records | | | <a name="module_records_with_terragrunt"></a> [records\_with\_terragrunt](#module\_records\_with\_terragrunt) | ../../modules/records | |
| <a name="module_records_with_terragrunt_with_lists"></a> [records\_with\_terragrunt\_with\_lists](#module\_records\_with\_terragrunt\_with\_lists) | ../../modules/records | |
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | | | <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | | | <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | |
| <a name="module_vpc2"></a> [vpc2](#module\_vpc2) | terraform-aws-modules/vpc/aws | | | <a name="module_vpc2"></a> [vpc2](#module\_vpc2) | terraform-aws-modules/vpc/aws | |
......
...@@ -2,6 +2,11 @@ provider "aws" { ...@@ -2,6 +2,11 @@ provider "aws" {
region = "eu-west-1" region = "eu-west-1"
} }
locals {
zone_name = sort(keys(module.zones.route53_zone_zone_id))[0]
# zone_id = module.zones.route53_zone_zone_id["app.terraform-aws-modules-example.com"]
}
module "zones" { module "zones" {
source = "../../modules/zones" source = "../../modules/zones"
...@@ -46,8 +51,8 @@ module "zones" { ...@@ -46,8 +51,8 @@ module "zones" {
module "records" { module "records" {
source = "../../modules/records" source = "../../modules/records"
zone_name = keys(module.zones.route53_zone_zone_id)[0] zone_name = local.zone_name
# zone_id = module.zones.route53_zone_zone_id["terraform-aws-modules-example.com"] # zone_id = local.zone_id
records = [ records = [
{ {
...@@ -128,15 +133,36 @@ module "records" { ...@@ -128,15 +133,36 @@ module "records" {
failover_routing_policy = { failover_routing_policy = {
type = "SECONDARY" type = "SECONDARY"
} }
}
]
depends_on = [module.zones]
}
module "records_with_lists" {
source = "../../modules/records"
zone_name = local.zone_name
# zone_id = local.zone_id
records = [
{
name = "tf-list1"
type = "A"
ttl = 3600
records = [
"10.10.10.10",
]
}, },
{ {
name = "alternative-resource-name" name = "tf-list2"
type = "A" type = "A"
alias = { ttl = 3600
name = module.s3_bucket.s3_bucket_website_domain records = [
zone_id = module.s3_bucket.s3_bucket_hosted_zone_id "20.10.10.10",
} "30.10.10.10",
} ]
},
] ]
depends_on = [module.zones] depends_on = [module.zones]
...@@ -145,7 +171,8 @@ module "records" { ...@@ -145,7 +171,8 @@ module "records" {
module "records_with_terragrunt" { module "records_with_terragrunt" {
source = "../../modules/records" source = "../../modules/records"
zone_name = keys(module.zones.route53_zone_zone_id)[0] zone_name = local.zone_name
# zone_id = local.zone_id
# Terragrunt has a bug (https://github.com/gruntwork-io/terragrunt/issues/1211) that requires `records` to be wrapped with `jsonencode()` # Terragrunt has a bug (https://github.com/gruntwork-io/terragrunt/issues/1211) that requires `records` to be wrapped with `jsonencode()`
records = jsonencode([ records = jsonencode([
...@@ -170,6 +197,36 @@ module "records_with_terragrunt" { ...@@ -170,6 +197,36 @@ module "records_with_terragrunt" {
depends_on = [module.zones] depends_on = [module.zones]
} }
module "records_with_terragrunt_with_lists" {
source = "../../modules/records"
zone_name = local.zone_name
# zone_id = local.zone_id
# Terragrunt has a bug (https://github.com/gruntwork-io/terragrunt/issues/1211) that requires `records` to be wrapped with `jsonencode()`
records = jsonencode([
{
name = "tg-list1"
type = "A"
ttl = 3600
records = [
"10.10.10.10",
]
},
{
name = "tg-list2"
type = "A"
ttl = 3600
records = [
"20.10.10.10",
"30.10.10.10",
]
}
])
depends_on = [module.zones]
}
module "disabled_records" { module "disabled_records" {
source = "../../modules/records" source = "../../modules/records"
......
...@@ -7,7 +7,7 @@ This module creates DNS records in Route53 zone. ...@@ -7,7 +7,7 @@ This module creates DNS records in Route53 zone.
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.12.26 | | <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.49 | | <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.49 |
## Providers ## Providers
......
...@@ -3,8 +3,21 @@ locals { ...@@ -3,8 +3,21 @@ locals {
# See details: https://github.com/gruntwork-io/terragrunt/issues/1211 # See details: https://github.com/gruntwork-io/terragrunt/issues/1211
records = try(jsondecode(var.records), var.records) records = try(jsondecode(var.records), var.records)
# convert from list to map with unique keys # Convert `records` from list to map with unique keys
recordsets = { for rs in local.records : join(" ", compact(["${rs.name} ${rs.type}", lookup(rs, "set_identifier", "")])) => rs } #
# A list of `records` values is jsonencode()'d to a string to solve issue:
# The true result value has the wrong type:
# element types must all match for conversion to map.
# Ref:
# https://github.com/terraform-aws-modules/terraform-aws-route53/issues/47
# https://github.com/terraform-aws-modules/terraform-aws-route53/pull/39
recordsets = {
for rs in local.records :
join(" ", compact(["${rs.name} ${rs.type}", lookup(rs, "set_identifier", "")])) => merge(rs, {
records = jsonencode(try(rs.records, null))
})
}
} }
data "aws_route53_zone" "this" { data "aws_route53_zone" "this" {
...@@ -23,7 +36,7 @@ resource "aws_route53_record" "this" { ...@@ -23,7 +36,7 @@ resource "aws_route53_record" "this" {
name = each.value.name != "" ? "${each.value.name}.${data.aws_route53_zone.this[0].name}" : data.aws_route53_zone.this[0].name name = each.value.name != "" ? "${each.value.name}.${data.aws_route53_zone.this[0].name}" : data.aws_route53_zone.this[0].name
type = each.value.type type = each.value.type
ttl = lookup(each.value, "ttl", null) ttl = lookup(each.value, "ttl", null)
records = lookup(each.value, "records", null) records = jsondecode(each.value.records)
set_identifier = lookup(each.value, "set_identifier", null) set_identifier = lookup(each.value, "set_identifier", null)
health_check_id = lookup(each.value, "health_check_id", null) health_check_id = lookup(each.value, "health_check_id", null)
multivalue_answer_routing_policy = lookup(each.value, "multivalue_answer_routing_policy", null) multivalue_answer_routing_policy = lookup(each.value, "multivalue_answer_routing_policy", null)
......
terraform { terraform {
required_version = ">= 0.12.26" required_version = ">= 0.13"
required_providers { required_providers {
aws = ">= 2.49" aws = ">= 2.49"
......
...@@ -7,7 +7,7 @@ This module creates Route53 zones. ...@@ -7,7 +7,7 @@ This module creates Route53 zones.
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.12.26 | | <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.49 | | <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.49 |
## Providers ## Providers
......
terraform { terraform {
required_version = ">= 0.12.26" required_version = ">= 0.13"
required_providers { required_providers {
aws = ">= 2.49" aws = ">= 2.49"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment