diff --git a/main.tf b/main.tf index 88973e3..cec2e13 100644 --- a/main.tf +++ b/main.tf @@ -1,25 +1,59 @@ #----------------------------------------------------------- -# API Gateway +# API Gateway v1 +#----------------------------------------------------------- +resource "aws_api_gateway_rest_api" "this" { + count = var.apigateway_version == "v1" ? 1 : 0 + + name = var.name + description = var.description + + endpoint_configuration { + types = ["REGIONAL"] + } + + tags = var.tags +} + +resource "aws_api_gateway_domain_name" "this" { + count = var.custom_domain_enabled && var.apigateway_version == "v1" ? 1 : 0 + + domain_name = var.custom_domain_name + regional_certificate_arn = module.certificate[count.index].certificate_arn + + endpoint_configuration { + types = ["REGIONAL"] + } + + tags = var.tags +} + +#----------------------------------------------------------- +# API Gateway v2 #----------------------------------------------------------- resource "aws_apigatewayv2_api" "this" { + count = var.apigateway_version == "v2" ? 1 : 0 + name = var.name protocol_type = var.protocol_type + description = var.description tags = var.tags } resource "aws_apigatewayv2_stage" "this" { + count = var.apigateway_version == "v2" ? 1 : 0 + name = var.name - api_id = aws_apigatewayv2_api.this.id + api_id = aws_apigatewayv2_api.this[count.index].id auto_deploy = true tags = var.tags } resource "aws_apigatewayv2_domain_name" "this" { - count = var.custom_dns_enabled ? 1 : 0 + count = var.custom_domain_enabled && var.apigateway_version == "v2" ? 1 : 0 - domain_name = var.custom_dns + domain_name = var.custom_domain_name domain_name_configuration { certificate_arn = module.certificate[count.index].certificate_arn @@ -33,23 +67,23 @@ resource "aws_apigatewayv2_domain_name" "this" { } resource "aws_apigatewayv2_api_mapping" "this" { - count = var.custom_dns_enabled ? 1 : 0 + count = var.custom_domain_enabled && var.apigateway_version == "v2" ? 1 : 0 - api_id = aws_apigatewayv2_api.this.id + api_id = aws_apigatewayv2_api.this[count.index].id domain_name = aws_apigatewayv2_domain_name.this[count.index].id - stage = aws_apigatewayv2_stage.this.id + stage = aws_apigatewayv2_stage.this[count.index].id } #----------------------------------------------------------- # Certificate #----------------------------------------------------------- module "certificate" { - count = var.custom_dns_enabled ? 1 : 0 + count = var.custom_domain_enabled ? 1 : 0 source = "./modules/certificate" hosted_zone = var.hosted_zone - custom_dns = var.custom_dns + custom_dns = var.custom_domain_name tags = var.tags } @@ -58,15 +92,15 @@ module "certificate" { # Route53 #----------------------------------------------------------- resource "aws_route53_record" "this" { - count = var.custom_dns_enabled ? 1 : 0 + count = var.custom_domain_enabled ? 1 : 0 - name = aws_apigatewayv2_domain_name.this[count.index].domain_name + name = var.custom_domain_name type = "A" zone_id = module.certificate[count.index].hosted_zone_id alias { - name = aws_apigatewayv2_domain_name.this[count.index].domain_name_configuration[0].target_domain_name - zone_id = aws_apigatewayv2_domain_name.this[count.index].domain_name_configuration[0].hosted_zone_id + name = var.apigateway_version == "v1" ? aws_api_gateway_domain_name.this[count.index].regional_domain_name : aws_apigatewayv2_domain_name.this[count.index].domain_name_configuration[0].target_domain_name + zone_id = var.apigateway_version == "v1" ? aws_api_gateway_domain_name.this[count.index].regional_zone_id : aws_apigatewayv2_domain_name.this[count.index].domain_name_configuration[0].hosted_zone_id evaluate_target_health = false } diff --git a/outputs.tf b/outputs.tf index 5fb6d89..e69de29 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,7 +0,0 @@ -output "api_id" { - value = aws_apigatewayv2_api.this.id -} - -output "api_execution_arn" { - value = aws_apigatewayv2_api.this.execution_arn -} diff --git a/variables.tf b/variables.tf index 0390fb4..88f76ff 100644 --- a/variables.tf +++ b/variables.tf @@ -15,9 +15,15 @@ variable "tags" { #----------------------------------------------------------- # API Gateway #----------------------------------------------------------- +variable "apigateway_version" { + type = string + description = "(Optional) Type of API Gateway to create. Use v1 for REST APIs and v2 for HTTP/WEBSOCKET APIs." + default = "v2" +} + variable "protocol_type" { type = string - description = "(Optional) API protocol. Valid values: HTTP, WEBSOCKET." + description = "(Optional) API protocol. Only for API GAteway v2. Valid values: HTTP, WEBSOCKET." default = "HTTP" } @@ -36,7 +42,7 @@ variable "disable_execute_api_endpoint" { #----------------------------------------------------------- # Route 53 #----------------------------------------------------------- -variable "custom_dns_enabled" { +variable "custom_domain_enabled" { type = bool description = "(Optional) Enable custom DNS resources." default = false @@ -48,7 +54,7 @@ variable "hosted_zone" { default = "" } -variable "custom_dns" { +variable "custom_domain_name" { type = string description = "(Optional) Domain name. Must be between 1 and 512 characters in length." default = ""