Skip to content

Commit 3f16b9e

Browse files
author
Kilian
committed
enha: added option to pass the zone ID of public hosted zone
1 parent 801b0f0 commit 3f16b9e

File tree

6 files changed

+36
-35
lines changed

6 files changed

+36
-35
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ This module provides an API Gateway with the option to define routes which invok
2121

2222
## Inputs
2323

24-
| Name | Description | Type | Default | Required |
25-
| ----------- | --------------------------------------------------------------------------- | -------------- | ------- | :------: |
26-
| identifier | Unique identifier to differentiate global resources. | `string` | n/a | yes |
27-
| description | Short text of what the API Gateway is trying to accomplish. | `string` | "" | no |
28-
| domain | Custom domain pointed to the API Gateway. | `string` | "" | no |
29-
| routes | A list of objects for the definition of routes in the API Gateway. | `list(object)` | [] | no |
30-
| log_config | An object for the definition of a CloudWatch log for the API Gateway. | `object` | null | no |
31-
| cors_config | An object for the definition of the CORS configuration for the API Gateway. | `object` | null | no |
32-
| tags | A map of tags to add to all resources. | `map(string)` | {} | no |
24+
| Name | Description | Type | Default | Required |
25+
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------- | ------- | :------: |
26+
| identifier | Unique identifier to differentiate global resources. | `string` | n/a | yes |
27+
| description | Short text of what the API Gateway is trying to accomplish. | `string` | "" | no |
28+
| domain | Custom domain pointed to the API Gateway. | `string` | n/a | yes |
29+
| zone_id | ID of the public hosted zone for the domain. (When not specified the public hosted zone of the domain will be pulled with a data resource from your account) | `string` | "" | no |
30+
| routes | A list of objects for the definition of routes in the API Gateway. | `list(object)` | [] | no |
31+
| log_config | An object for the definition of a CloudWatch log for the API Gateway. | `object` | null | no |
32+
| cors_config | An object for the definition of the CORS configuration for the API Gateway. | `object` | null | no |
33+
| tags | A map of tags to add to all resources. | `map(string)` | {} | no |
3334

3435
### `routes`
3536

main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ locals {
99

1010
# get public zone for base domain (must be already present in account)
1111
data "aws_route53_zone" "main" {
12-
count = !var.test ? 1 : 0
12+
count = length(var.zone_id) < 1 ? 1 : 0
1313
name = local.base_domain
1414
private_zone = false
1515
}
1616

17-
# conditionally set the zone_id to a dummy value for unit tests to run
17+
# conditionally set the zone_id to avoid duplication of conditions
1818
locals {
19-
zone_id = !var.test ? data.aws_route53_zone.main[0].id : "testzone123"
19+
zone_id = length(var.zone_id) < 1 ? data.aws_route53_zone.main[0].id : var.zone_id
2020
}
2121

2222
resource "aws_acm_certificate" "main" {

tests/api-gw.tftest.hcl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ run "invalid_identifier" {
1111
command = plan
1212

1313
variables {
14-
test = true
1514
identifier = "ab"
1615
domain = "test.com"
16+
zone_id = "test-zone"
1717
}
1818

1919
expect_failures = [var.identifier]
@@ -23,19 +23,19 @@ run "valid_identifier" {
2323
command = plan
2424

2525
variables {
26-
test = true
2726
identifier = "abc"
2827
domain = "test.com"
28+
zone_id = "test-zone"
2929
}
3030
}
3131

3232
run "invalid_route_method" {
3333
command = plan
3434

3535
variables {
36-
test = true
3736
identifier = "abc"
3837
domain = "test.com"
38+
zone_id = "test-zone"
3939
routes = [
4040
{
4141
route = "/test"
@@ -71,9 +71,9 @@ run "duplicate_route" {
7171
command = plan
7272

7373
variables {
74-
test = true
7574
identifier = "abc"
7675
domain = "test.com"
76+
zone_id = "test-zone"
7777
routes = [
7878
{
7979
route = "/test"
@@ -109,9 +109,9 @@ run "invalid_route_function_arn" {
109109
command = plan
110110

111111
variables {
112-
test = true
113112
identifier = "abc"
114113
domain = "test.com"
114+
zone_id = "test-zone"
115115
routes = [
116116
{
117117
route = "/test"
@@ -147,9 +147,9 @@ run "valid_routes" {
147147
command = plan
148148

149149
variables {
150-
test = true
151150
identifier = "abc"
152151
domain = "test.com"
152+
zone_id = "test-zone"
153153
routes = [
154154
{
155155
route = "/test"
@@ -183,9 +183,9 @@ run "invalid_log_config" {
183183
command = plan
184184

185185
variables {
186-
test = true
187186
identifier = "abc"
188187
domain = "test.com"
188+
zone_id = "test-zone"
189189
log_config = {
190190
retention_in_days = 200
191191
}
@@ -198,9 +198,9 @@ run "invalid_route_invoke_arn" {
198198
command = plan
199199

200200
variables {
201-
test = true
202201
identifier = "abc"
203202
domain = "test.com"
203+
zone_id = "test-zone"
204204
routes = [
205205
{
206206
route = "/test"
@@ -236,9 +236,9 @@ run "valid_log_config" {
236236
command = plan
237237

238238
variables {
239-
test = true
240239
identifier = "abc"
241240
domain = "test.com"
241+
zone_id = "test-zone"
242242
log_config = {
243243
retention_in_days = 7
244244
}

tests/cors.tftest.hcl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ run "duplicate_allow_methods" {
1111
command = plan
1212

1313
variables {
14-
test = true
1514
identifier = "abc"
1615
domain = "test.com"
16+
zone_id = "test-zone"
1717

1818
cors_config = {
1919
allow_methods = ["GET", "POST", "GET", "DELETE"]
@@ -29,9 +29,9 @@ run "duplicate_allow_origins" {
2929
command = plan
3030

3131
variables {
32-
test = true
3332
identifier = "abc"
3433
domain = "test.com"
34+
zone_id = "test-zone"
3535

3636
cors_config = {
3737
allow_methods = ["GET", "POST", "DELETE"]
@@ -47,9 +47,9 @@ run "duplicate_allow_headers" {
4747
command = plan
4848

4949
variables {
50-
test = true
5150
identifier = "abc"
5251
domain = "test.com"
52+
zone_id = "test-zone"
5353

5454
cors_config = {
5555
allow_methods = ["GET", "POST", "DELETE"]
@@ -65,9 +65,9 @@ run "invalid_allow_methods" {
6565
command = plan
6666

6767
variables {
68-
test = true
6968
identifier = "abc"
7069
domain = "test.com"
70+
zone_id = "test-zone"
7171

7272
cors_config = {
7373
allow_methods = ["GET", "POST", "FOO", "DELETE"]
@@ -83,9 +83,9 @@ run "valid_cors_config" {
8383
command = plan
8484

8585
variables {
86-
test = true
8786
identifier = "abc"
8887
domain = "test.com"
88+
zone_id = "test-zone"
8989

9090
cors_config = {
9191
allow_methods = ["GET", "POST", "DELETE"]

tests/dns.tftest.hcl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ run "base_domain_split_without_sub_domain" {
1111
command = plan
1212

1313
variables {
14-
test = true
1514
identifier = "abc"
1615
domain = "test.com"
16+
zone_id = "test-zone"
1717
}
1818

1919
assert {
@@ -26,9 +26,9 @@ run "base_domain_split_with_single_sub_domain" {
2626
command = plan
2727

2828
variables {
29-
test = true
3029
identifier = "abc"
3130
domain = "www.test.com"
31+
zone_id = "test-zone"
3232
}
3333

3434
assert {
@@ -41,9 +41,9 @@ run "base_domain_split_with_multiple_sub_domain" {
4141
command = plan
4242

4343
variables {
44-
test = true
4544
identifier = "abc"
4645
domain = "www.blog.api.test.com"
46+
zone_id = "test-zone"
4747
}
4848

4949
assert {

variables.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ variable "domain" {
1818
type = string
1919
}
2020

21+
variable "zone_id" {
22+
description = "ID of the public hosted zone for the domain."
23+
type = string
24+
default = ""
25+
}
26+
2127
variable "log_config" {
2228
description = "An object for the definition of a CloudWatch log for the API Gateway."
2329
type = object({
@@ -102,9 +108,3 @@ variable "tags" {
102108
type = map(string)
103109
default = {}
104110
}
105-
106-
variable "test" {
107-
description = "A flag for wether or not creating a test environment to conduct unit tests with."
108-
type = bool
109-
default = false
110-
}

0 commit comments

Comments
 (0)