Skip to content

Commit de4c3b3

Browse files
refactor module (#7)
* refactor module * terraform-docs: automated action * update * update * updates * updates * updates * updates --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 260b5e4 commit de4c3b3

File tree

10 files changed

+164
-89
lines changed

10 files changed

+164
-89
lines changed

.github/workflows/documentation.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
name: Generate terraform docs
2+
23
on:
34
- pull_request
45
permissions:
56
contents: write
7+
68
jobs:
79
docs:
810
runs-on: ubuntu-latest
11+
912
steps:
10-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1114
with:
1215
ref: ${{ github.event.pull_request.head.ref }}
1316

1417
- name: Render terraform docs inside the README.md and push changes back to PR branch
15-
uses: terraform-docs/gh-actions@v1.0.0
18+
uses: terraform-docs/gh-actions@v1.2.0
1619
with:
1720
working-dir: .
1821
output-file: README.md

.github/workflows/lint.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Terraform Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
terraform-lint:
13+
name: Lint Terraform code
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up Terraform
21+
uses: hashicorp/setup-terraform@v3
22+
with:
23+
terraform_version: 0.13.1
24+
25+
- name: Terraform Init
26+
id: init
27+
run: terraform init -input=false
28+
29+
- name: Terraform Format Check
30+
id: fmt
31+
run: terraform fmt -check -recursive
32+
continue-on-error: true
33+
34+
- name: Terraform Format Diff
35+
if: failure()
36+
run: terraform fmt -diff -recursive
37+
38+
- name: Terraform Validate
39+
run: terraform validate

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ No modules.
4545

4646
| Name | Description | Type | Default | Required |
4747
|------|-------------|------|---------|:--------:|
48-
| <a name="input_primary_domain"></a> [primary\_domain](#input\_primary\_domain) | Route53 Primary domain | `any` | n/a | yes |
49-
| <a name="input_records_a"></a> [records\_a](#input\_records\_a) | Map of A records separate by comma (,) | `map(any)` | `{}` | no |
50-
| <a name="input_records_aaaa"></a> [records\_aaaa](#input\_records\_aaaa) | Map of AAAA records separate by comma (,) | `map(any)` | `{}` | no |
51-
| <a name="input_records_caa"></a> [records\_caa](#input\_records\_caa) | Map of CAA records separate by comma (,) | `map(any)` | `{}` | no |
52-
| <a name="input_records_cname"></a> [records\_cname](#input\_records\_cname) | Map of CNAME records separate by comma (,) | `map(any)` | `{}` | no |
53-
| <a name="input_records_mx"></a> [records\_mx](#input\_records\_mx) | Map of MX records separate by comma (,) | `map(any)` | `{}` | no |
54-
| <a name="input_records_ns"></a> [records\_ns](#input\_records\_ns) | Map of NS records separate by comma (,) | `map(any)` | `{}` | no |
55-
| <a name="input_records_txt"></a> [records\_txt](#input\_records\_txt) | Map of TXT records separate by comma (,) | `map(any)` | `{}` | no |
56-
| <a name="input_records_wr"></a> [records\_wr](#input\_records\_wr) | Map of redirect records | `map(any)` | `{}` | no |
48+
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Whether to enable Route 53 resources | `bool` | `true` | no |
49+
| <a name="input_primary_domain"></a> [primary\_domain](#input\_primary\_domain) | The domain name to manage | `string` | n/a | yes |
50+
| <a name="input_records_a"></a> [records\_a](#input\_records\_a) | Map of A records separate by comma (,) | `map(list(string))` | `{}` | no |
51+
| <a name="input_records_aaaa"></a> [records\_aaaa](#input\_records\_aaaa) | Map of AAAA records separate by comma (,) | `map(list(string))` | `{}` | no |
52+
| <a name="input_records_caa"></a> [records\_caa](#input\_records\_caa) | Map of CAA records separate by comma (,) | `map(list(string))` | `{}` | no |
53+
| <a name="input_records_cname"></a> [records\_cname](#input\_records\_cname) | Map of CNAME records separate by comma (,) | `map(list(string))` | `{}` | no |
54+
| <a name="input_records_mx"></a> [records\_mx](#input\_records\_mx) | Map of MX records separate by comma (,) | `map(list(string))` | `{}` | no |
55+
| <a name="input_records_ns"></a> [records\_ns](#input\_records\_ns) | Map of NS records separate by comma (,) | `map(list(string))` | `{}` | no |
56+
| <a name="input_records_txt"></a> [records\_txt](#input\_records\_txt) | Map of TXT records separate by comma (,) | `map(list(string))` | `{}` | no |
57+
| <a name="input_records_wr"></a> [records\_wr](#input\_records\_wr) | Map of redirect records | `map(list(string))` | `{}` | no |
5758
| <a name="input_ttl"></a> [ttl](#input\_ttl) | Default TTL for All records | `number` | `"3600"` | no |
5859
| <a name="input_ttl_acm"></a> [ttl\_acm](#input\_ttl\_acm) | Default TTL for acm records | `number` | `"60"` | no |
5960
| <a name="input_ttl_ns"></a> [ttl\_ns](#input\_ttl\_ns) | Default TTL for ns records | `number` | `"172800"` | no |
@@ -75,6 +76,7 @@ module "example-com" {
7576
source = "locus313/aws-route53/module"
7677
version = "1.0.3"
7778
79+
enabled = true
7880
primary_domain = "example.com"
7981
8082
records_wr = {

cert.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
resource "aws_acm_certificate" "records_wr" {
2-
provider = aws.acm
3-
for_each = var.records_wr
2+
for_each = var.records_wr
3+
4+
provider = aws.acm
45
domain_name = each.key
56
validation_method = "DNS"
67
}
78

89
resource "aws_acm_certificate_validation" "records_wr" {
9-
provider = aws.acm
10-
for_each = var.records_wr
10+
for_each = var.records_wr
11+
12+
provider = aws.acm
1113
certificate_arn = aws_acm_certificate.records_wr[each.key].arn
12-
validation_record_fqdns = [for record in aws_route53_record.records_wr_validation: record.fqdn]
14+
validation_record_fqdns = [for record in aws_route53_record.records_wr_validation : record.fqdn]
1315
}

cloudfront.tf

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#tfsec:ignore:AWS045 tfsec:ignore:AWS071
22
resource "aws_cloudfront_distribution" "records_wr" {
3-
depends_on = [aws_acm_certificate.records_wr]
4-
for_each = var.records_wr
3+
for_each = var.records_wr
4+
55
http_version = "http2"
66

77
origin {
@@ -17,13 +17,13 @@ resource "aws_cloudfront_distribution" "records_wr" {
1717
# doesn't support HTTPS connections for website endpoints."
1818
origin_protocol_policy = "http-only"
1919

20-
http_port = "80"
20+
http_port = "80"
2121
https_port = "443"
2222

2323
# TODO: given the origin_protocol_policy set to `http-only`,
2424
# not sure what this does...
2525
# "If the origin is an Amazon S3 bucket, CloudFront always uses TLSv1.2."
26-
origin_ssl_protocols = ["TLSv1.2"]
26+
origin_ssl_protocols = ["TLSv1.2"]
2727
}
2828

2929
# s3_origin_config is not compatible with S3 website hosting, if this
@@ -75,10 +75,12 @@ resource "aws_cloudfront_distribution" "records_wr" {
7575
}
7676

7777
viewer_certificate {
78-
acm_certificate_arn = aws_acm_certificate_validation.records_wr[each.key].certificate_arn
79-
ssl_support_method = "sni-only"
78+
acm_certificate_arn = aws_acm_certificate_validation.records_wr[each.key].certificate_arn
79+
ssl_support_method = "sni-only"
8080
#tfsec:ignore:AWS021
8181
minimum_protocol_version = "TLSv1.2_2021"
8282
}
8383

84+
depends_on = [aws_acm_certificate.records_wr]
85+
8486
}

main.tf

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,61 @@
11
resource "aws_route53_zone" "this" {
2-
name = var.primary_domain
2+
name = var.primary_domain
3+
count = var.enabled ? 1 : 0
34
}
45

56
resource "aws_route53_record" "records_a" {
7+
for_each = var.records_a
8+
9+
zone_id = aws_route53_zone.this[0].zone_id
10+
name = each.key
11+
type = "A"
12+
ttl = var.ttl
13+
records = each.value
14+
615
depends_on = [aws_route53_zone.this]
7-
for_each = var.records_a
8-
zone_id = aws_route53_zone.this.zone_id
9-
name = each.key
10-
type = "A"
11-
ttl = var.ttl
12-
records = each.value
1316
}
1417

1518
resource "aws_route53_record" "records_aaaa" {
19+
for_each = var.records_aaaa
20+
21+
zone_id = aws_route53_zone.this[0].zone_id
22+
name = each.key
23+
type = "AAAA"
24+
ttl = var.ttl
25+
records = each.value
26+
1627
depends_on = [aws_route53_zone.this]
17-
for_each = var.records_aaaa
18-
zone_id = aws_route53_zone.this.zone_id
19-
name = each.key
20-
type = "AAAA"
21-
ttl = var.ttl
22-
records = each.value
2328
}
2429

2530
resource "aws_route53_record" "records_caa" {
31+
for_each = var.records_caa
32+
33+
zone_id = aws_route53_zone.this[0].zone_id
34+
name = each.key
35+
type = "CAA"
36+
ttl = var.ttl
37+
records = each.value
38+
2639
depends_on = [aws_route53_zone.this]
27-
for_each = var.records_caa
28-
zone_id = aws_route53_zone.this.zone_id
29-
name = each.key
30-
type = "CAA"
31-
ttl = var.ttl
32-
records = each.value
3340
}
3441

3542
resource "aws_route53_record" "records_wr" {
36-
depends_on = [aws_route53_zone.this]
37-
for_each = var.records_wr
38-
zone_id = aws_route53_zone.this.zone_id
39-
name = each.key
40-
type = "A"
43+
for_each = var.records_wr
44+
45+
zone_id = aws_route53_zone.this[0].zone_id
46+
name = each.key
47+
type = "A"
4148

4249
alias {
4350
name = aws_cloudfront_distribution.records_wr[each.key].domain_name
4451
zone_id = aws_cloudfront_distribution.records_wr[each.key].hosted_zone_id
4552
evaluate_target_health = false
4653
}
54+
55+
depends_on = [aws_route53_zone.this]
4756
}
4857

4958
resource "aws_route53_record" "records_wr_validation" {
50-
depends_on = [aws_route53_zone.this]
5159
for_each = {
5260
for dvo in flatten([
5361
for cert in aws_acm_certificate.records_wr : cert.domain_validation_options
@@ -57,50 +65,61 @@ resource "aws_route53_record" "records_wr_validation" {
5765
type = dvo.resource_record_type
5866
}
5967
}
68+
6069
allow_overwrite = true
6170
name = each.value.name
6271
records = [each.value.record]
6372
ttl = var.ttl_acm
6473
type = each.value.type
65-
zone_id = aws_route53_zone.this.zone_id
74+
zone_id = aws_route53_zone.this[0].zone_id
75+
76+
depends_on = [aws_route53_zone.this]
6677
}
6778

6879
resource "aws_route53_record" "records_cname" {
80+
for_each = var.records_cname
81+
82+
zone_id = aws_route53_zone.this[0].zone_id
83+
name = each.key
84+
type = "CNAME"
85+
ttl = var.ttl
86+
records = each.value
87+
6988
depends_on = [aws_route53_zone.this]
70-
for_each = var.records_cname
71-
zone_id = aws_route53_zone.this.zone_id
72-
name = each.key
73-
type = "CNAME"
74-
ttl = var.ttl
75-
records = each.value
7689
}
7790

7891
resource "aws_route53_record" "records_mx" {
92+
for_each = var.records_mx
93+
94+
zone_id = aws_route53_zone.this[0].zone_id
95+
name = each.key
96+
type = "MX"
97+
ttl = var.ttl
98+
records = each.value
99+
79100
depends_on = [aws_route53_zone.this]
80-
for_each = var.records_mx
81-
zone_id = aws_route53_zone.this.zone_id
82-
name = each.key
83-
type = "MX"
84-
ttl = var.ttl
85-
records = each.value
86101
}
87102

88103
resource "aws_route53_record" "records_txt" {
104+
for_each = var.records_txt
105+
106+
zone_id = aws_route53_zone.this[0].zone_id
107+
name = each.key
108+
type = "TXT"
109+
ttl = var.ttl
110+
records = each.value
111+
89112
depends_on = [aws_route53_zone.this]
90-
for_each = var.records_txt
91-
zone_id = aws_route53_zone.this.zone_id
92-
name = each.key
93-
type = "TXT"
94-
ttl = var.ttl
95-
records = each.value
96113
}
97114

98115
resource "aws_route53_record" "records_ns" {
116+
for_each = var.records_ns
117+
118+
zone_id = aws_route53_zone.this[0].zone_id
119+
name = each.key
120+
type = "NS"
121+
ttl = var.ttl_ns
122+
records = each.value
123+
99124
depends_on = [aws_route53_zone.this]
100-
for_each = var.records_ns
101-
zone_id = aws_route53_zone.this.zone_id
102-
name = each.key
103-
type = "NS"
104-
ttl = var.ttl_ns
105-
records = each.value
106125
}

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
output "this_route53_zone_zone_id" {
22
description = "Zone ID of Route53 zone"
3-
value = aws_route53_zone.this.zone_id
3+
value = aws_route53_zone.this[0].zone_id
44
}
55

66
output "this_route53_zone_name_servers" {
77
description = "Name servers of Route53 zone"
8-
value = aws_route53_zone.this.name_servers
8+
value = aws_route53_zone.this[0].name_servers
99
}

provider.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
provider "aws" {
2-
alias = "acm"
2+
alias = "acm"
33
region = "us-east-1"
44
}

s3.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#tfsec:ignore:AWS002 tfsec:ignore:AWS017 tfsec:ignore:AWS077 tfsec:ignore:AWS098
22
resource "aws_s3_bucket" "records_wr" {
3-
for_each = var.records_wr
4-
bucket = each.key
3+
for_each = var.records_wr
4+
5+
bucket = each.key
56
}
67

78
resource "aws_s3_bucket_website_configuration" "records_wr" {
8-
for_each = var.records_wr
9-
bucket = aws_s3_bucket.records_wr[each.key].id
9+
for_each = var.records_wr
10+
11+
bucket = aws_s3_bucket.records_wr[each.key].id
1012

1113
redirect_all_requests_to {
1214
host_name = each.value

0 commit comments

Comments
 (0)