Skip to content

Commit

Permalink
feat: Added support for API Gateway v1 APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
laz-rb committed May 10, 2023
1 parent 10670f1 commit dc57ecd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
60 changes: 47 additions & 13 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
7 changes: 0 additions & 7 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
output "api_id" {
value = aws_apigatewayv2_api.this.id
}

output "api_execution_arn" {
value = aws_apigatewayv2_api.this.execution_arn
}
12 changes: 9 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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
Expand All @@ -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 = ""
Expand Down

0 comments on commit dc57ecd

Please sign in to comment.