Skip to content

Commit

Permalink
refactor module
Browse files Browse the repository at this point in the history
  • Loading branch information
locus313 committed Jun 22, 2024
1 parent 260b5e4 commit 5e2ab33
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
name: Generate terraform docs

on:
- pull_request
permissions:
contents: write

jobs:
docs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Terraform Lint

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
terraform-lint:
name: Lint Terraform code
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.13.1

- name: Terraform Format Check
id: fmt
run: terraform fmt -check -recursive
continue-on-error: true

- name: Terraform Format Diff
if: failure()
run: terraform fmt -diff -recursive

- name: Terraform Validate
run: terraform validate
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module "example-com" {
source = "locus313/aws-route53/module"
version = "1.0.3"
enabled = true
primary_domain = "example.com"
records_wr = {
Expand Down
39 changes: 29 additions & 10 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
resource "aws_route53_zone" "this" {
name = var.primary_domain
name = var.primary_domain
count = var.enabled ? 1 : 0
}

resource "aws_route53_record" "records_a" {
depends_on = [aws_route53_zone.this]
for_each = var.records_a

zone_id = aws_route53_zone.this.zone_id
name = each.key
type = "A"
ttl = var.ttl
records = each.value

depends_on = [aws_route53_zone.this]
}

resource "aws_route53_record" "records_aaaa" {
depends_on = [aws_route53_zone.this]
for_each = var.records_aaaa

zone_id = aws_route53_zone.this.zone_id
name = each.key
type = "AAAA"
ttl = var.ttl
records = each.value

depends_on = [aws_route53_zone.this]
}

resource "aws_route53_record" "records_caa" {
depends_on = [aws_route53_zone.this]
for_each = var.records_caa

zone_id = aws_route53_zone.this.zone_id
name = each.key
type = "CAA"
ttl = var.ttl
records = each.value

depends_on = [aws_route53_zone.this]
}

resource "aws_route53_record" "records_wr" {
depends_on = [aws_route53_zone.this]
for_each = var.records_wr

zone_id = aws_route53_zone.this.zone_id
name = each.key
type = "A"
Expand All @@ -44,10 +51,11 @@ resource "aws_route53_record" "records_wr" {
zone_id = aws_cloudfront_distribution.records_wr[each.key].hosted_zone_id
evaluate_target_health = false
}

depends_on = [aws_route53_zone.this]
}

resource "aws_route53_record" "records_wr_validation" {
depends_on = [aws_route53_zone.this]
for_each = {
for dvo in flatten([
for cert in aws_acm_certificate.records_wr : cert.domain_validation_options
Expand All @@ -57,50 +65,61 @@ resource "aws_route53_record" "records_wr_validation" {
type = dvo.resource_record_type
}
}

allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = var.ttl_acm
type = each.value.type
zone_id = aws_route53_zone.this.zone_id

depends_on = [aws_route53_zone.this]
}

resource "aws_route53_record" "records_cname" {
depends_on = [aws_route53_zone.this]
for_each = var.records_cname

zone_id = aws_route53_zone.this.zone_id
name = each.key
type = "CNAME"
ttl = var.ttl
records = each.value

depends_on = [aws_route53_zone.this]
}

resource "aws_route53_record" "records_mx" {
depends_on = [aws_route53_zone.this]
for_each = var.records_mx

zone_id = aws_route53_zone.this.zone_id
name = each.key
type = "MX"
ttl = var.ttl
records = each.value

depends_on = [aws_route53_zone.this]
}

resource "aws_route53_record" "records_txt" {
depends_on = [aws_route53_zone.this]
for_each = var.records_txt

zone_id = aws_route53_zone.this.zone_id
name = each.key
type = "TXT"
ttl = var.ttl
records = each.value

depends_on = [aws_route53_zone.this]
}

resource "aws_route53_record" "records_ns" {
depends_on = [aws_route53_zone.this]
for_each = var.records_ns

zone_id = aws_route53_zone.this.zone_id
name = each.key
type = "NS"
ttl = var.ttl_ns
records = each.value

depends_on = [aws_route53_zone.this]
}
26 changes: 16 additions & 10 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,52 +1,58 @@
variable "enabled" {
description = "Whether to enable Route 53 resources"
type = bool
default = true
}

variable "primary_domain" {
description = "Route53 Primary domain"
type = any
description = "The domain name to manage"
type = string
}

variable "records_a" {
type = map(any)
type = map(list(string))
description = "Map of A records separate by comma (,)"
default = {}
}

variable "records_aaaa" {
type = map(any)
type = map(list(string))
description = "Map of AAAA records separate by comma (,)"
default = {}
}

variable "records_caa" {
type = map(any)
type = map(list(string))
description = "Map of CAA records separate by comma (,)"
default = {}
}

variable "records_cname" {
type = map(any)
type = map(list(string))
description = "Map of CNAME records separate by comma (,)"
default = {}
}

variable "records_mx" {
type = map(any)
type = map(list(string))
description = "Map of MX records separate by comma (,)"
default = {}
}

variable "records_txt" {
type = map(any)
type = map(list(string))
description = "Map of TXT records separate by comma (,)"
default = {}
}

variable "records_ns" {
type = map(any)
type = map(list(string))
description = "Map of NS records separate by comma (,)"
default = {}
}

variable "records_wr" {
type = map(any)
type = map(list(string))
description = "Map of redirect records"
default = {}
}
Expand Down

0 comments on commit 5e2ab33

Please sign in to comment.