Skip to content

Commit

Permalink
add endpoint type variable (#9)
Browse files Browse the repository at this point in the history
* add endpoint type variable

* remove space

* add support for edge endpoint type

* add validation to endpoint_type

* fix variable error
  • Loading branch information
lekchin authored Mar 14, 2023
1 parent cbc5b8a commit 6f912c1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
31 changes: 26 additions & 5 deletions modules/custom_domain/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Provisioned only for regional types. @todo: Enhance for other types as well
resource "aws_api_gateway_domain_name" "domain" {
resource "aws_api_gateway_domain_name" "domain_regional" {
count = var.endpoint_type == "REGIONAL" ? 1 : 0

domain_name = var.domain_name
regional_certificate_arn = var.cert_arn == "" ? aws_acm_certificate.cert[0].arn : var.cert_arn
security_policy = var.security_policy
Expand All @@ -10,13 +12,27 @@ resource "aws_api_gateway_domain_name" "domain" {

}

resource "aws_api_gateway_domain_name" "domain_edge" {
count = var.endpoint_type == "EDGE" ? 1 : 0

domain_name = var.domain_name
certificate_arn = var.cert_arn == "" ? aws_acm_certificate.cert[0].arn : var.cert_arn
security_policy = var.security_policy

endpoint_configuration {
types = ["EDGE"]
}

}

resource "aws_api_gateway_base_path_mapping" "mapping" {
for_each = var.path_mappings

api_id = each.value.api_id
stage_name = each.value.stage_name
base_path = each.value.base_path
domain_name = aws_api_gateway_domain_name.domain.domain_name
api_id = each.value.api_id
stage_name = each.value.stage_name
base_path = each.value.base_path

domain_name = var.endpoint_type == "EDGE" ? aws_api_gateway_domain_name.domain_edge[0].domain_name : aws_api_gateway_domain_name.domain_regional[0].domain_name
}

resource "aws_acm_certificate" "cert" {
Expand All @@ -29,3 +45,8 @@ resource "aws_acm_certificate" "cert" {
create_before_destroy = true
}
}

moved {
from = aws_api_gateway_domain_name.domain
to = aws_api_gateway_domain_name.domain_regional[0]
}
2 changes: 1 addition & 1 deletion modules/custom_domain/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "arn" {
value = aws_api_gateway_domain_name.domain.arn
value = var.endpoint_type == "EDGE" ? aws_api_gateway_domain_name.domain_edge[0].arn : aws_api_gateway_domain_name.domain_regional[0].arn
description = "ARN of domain name."
}
11 changes: 11 additions & 0 deletions modules/custom_domain/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ variable "path_mappings" {
})
)
}

variable "endpoint_type" {
description = "Endpoint type."
type = string
default = "REGIONAL"

validation {
condition = var.endpoint_type == "EDGE" || var.endpoint_type == "REGIONAL"
error_message = "endpoint_type must be 'EDGE' or 'REGIONAL'."
}
}

0 comments on commit 6f912c1

Please sign in to comment.