diff --git a/modules/custom_domain/main.tf b/modules/custom_domain/main.tf index 7c6f80a..95ab10d 100644 --- a/modules/custom_domain/main.tf +++ b/modules/custom_domain/main.tf @@ -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 @@ -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" { @@ -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] +} diff --git a/modules/custom_domain/outputs.tf b/modules/custom_domain/outputs.tf index efbdeb3..87a8328 100644 --- a/modules/custom_domain/outputs.tf +++ b/modules/custom_domain/outputs.tf @@ -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." } diff --git a/modules/custom_domain/variables.tf b/modules/custom_domain/variables.tf index 192d0b9..b803bc4 100644 --- a/modules/custom_domain/variables.tf +++ b/modules/custom_domain/variables.tf @@ -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'." + } +}