Skip to content

Commit d7c9ff7

Browse files
committed
Now the custom domain works just like the invoke url, e.g.,
curl https://api.dev.interviewprep.onyxdevtutorials.com/api/v0/users
1 parent 93f639f commit d7c9ff7

File tree

8 files changed

+48
-43
lines changed

8 files changed

+48
-43
lines changed

terraform/environments/development/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,15 @@ module "dns" {
170170
backend_record_name = "api.dev.interviewprep.onyxdevtutorials.com"
171171
lb_dns_name = module.load_balancer.lb_dns_name
172172
lb_zone_id = module.load_balancer.lb_zone_id
173-
api_invoke_url = replace(module.api_gateway.api_invoke_url, "/dev", "")
173+
custom_domain_name = module.api_gateway.custom_domain_name
174+
custom_domain_zone_id = module.api_gateway.custom_domain_zone_id
174175
}
175176

176177
module "api_gateway" {
177178
source = "../../modules/api_gateway"
178179
environment = var.environment
179-
# backend_dns_name = module.dns.backend_record_name
180180
cloudwatch_role_arn = module.iam.api_gateway_cloudwatch_role_arn
181181
lb_dns_name = module.load_balancer.lb_dns_name
182182
region = var.region
183+
certificate_arn = var.certificate_arn
183184
}

terraform/environments/development/outputs.tf

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,3 @@ output "backend_target_group_arn" {
139139
description = "The ARN of the backend target group"
140140
value = module.load_balancer.backend_target_group_arn
141141
}
142-
143-
# output "api_key_id" {
144-
# description = "The ID of the API Gateway API key"
145-
# value = module.api_gateway.api_key_id
146-
# }
147-
148-
output "api_invoke_url" {
149-
description = "The invoke URL of the API Gateway"
150-
value = module.api_gateway.api_invoke_url
151-
}

terraform/environments/development/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ variable "key_name" {
107107
variable "lambda_package_migrate" {
108108
description = "The path to the ZIP file containing the Lambda function code"
109109
type = string
110+
}
111+
112+
variable "certificate_arn" {
113+
description = "The ARN of the certificate to use for the custom domain"
114+
type = string
110115
}

terraform/modules/api_gateway/main.tf

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,18 @@ resource "aws_iam_role_policy_attachment" "api_gateway_cloudwatch_policy_attachm
160160
role = aws_iam_role.api_gateway_cloudwatch_role.name
161161
}
162162

163-
# resource "aws_api_gateway_api_key" "api_key" {
164-
# name = "${var.environment}-interview-prep-api-key"
165-
# description = "API Key for Interview Prep ${var.environment} environment"
166-
# enabled = true
167-
# }
168-
169-
# resource "aws_api_gateway_usage_plan" "usage_plan" {
170-
# name = "${var.environment}-interview-prep-usage-plan"
171-
# description = "Usage Plan for Interview Prep ${var.environment} environment"
172-
# api_stages {
173-
# api_id = aws_api_gateway_rest_api.api.id
174-
# stage = aws_api_gateway_stage.api_stage.stage_name
175-
# }
176-
# }
177-
178-
# resource "aws_api_gateway_usage_plan_key" "usage_plan_key" {
179-
# key_id = aws_api_gateway_api_key.api_key.id
180-
# key_type = "API_KEY"
181-
# usage_plan_id = aws_api_gateway_usage_plan.usage_plan.id
182-
# }
163+
resource "aws_api_gateway_domain_name" "custom_domain" {
164+
domain_name = "api.dev.interviewprep.onyxdevtutorials.com"
165+
166+
endpoint_configuration {
167+
types = ["EDGE"]
168+
}
169+
170+
certificate_arn = var.certificate_arn
171+
}
172+
173+
resource "aws_api_gateway_base_path_mapping" "custom_domain_mapping" {
174+
api_id = aws_api_gateway_rest_api.api.id
175+
stage_name = aws_api_gateway_stage.api_stage.stage_name
176+
domain_name = aws_api_gateway_domain_name.custom_domain.domain_name
177+
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
output "api_invoke_url" {
2-
value = "https://${aws_api_gateway_rest_api.api.id}.execute-api.${var.region}.amazonaws.com/dev"
1+
output "custom_domain_name" {
2+
description = "The custom domain name"
3+
value = aws_api_gateway_domain_name.custom_domain.cloudfront_domain_name
34
}
45

5-
# output "api_key_id" {
6-
# value = aws_api_gateway_api_key.api_key.id
7-
# }
6+
output "custom_domain_zone_id" {
7+
description = "The custom domain zone ID"
8+
value = aws_api_gateway_domain_name.custom_domain.cloudfront_zone_id
9+
}

terraform/modules/api_gateway/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ variable "cloudwatch_role_arn" {
1616
variable "lb_dns_name" {
1717
description = "The DNS name of the load balancer"
1818
type = string
19+
}
20+
21+
variable "certificate_arn" {
22+
description = "The ARN of the certificate to use for the custom domain"
23+
type = string
1924
}

terraform/modules/dns/main.tf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ resource "aws_route53_record" "frontend" {
1313
resource "aws_route53_record" "backend" {
1414
zone_id = var.zone_id
1515
name = var.backend_record_name
16-
type = "CNAME"
16+
type = "A"
1717

18-
records = [replace(var.api_invoke_url, "https://", "")]
19-
ttl = 300
18+
alias {
19+
name = var.custom_domain_name
20+
zone_id = var.custom_domain_zone_id
21+
evaluate_target_health = false
22+
}
2023
}

terraform/modules/dns/variables.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ variable "lb_zone_id" {
2323
type = string
2424
}
2525

26-
variable "api_invoke_url" {
27-
description = "The invoke URL of the API Gateway"
26+
variable "custom_domain_name" {
27+
description = "The custom domain name for api"
28+
type = string
29+
}
30+
31+
variable "custom_domain_zone_id" {
32+
description = "The custom domain zone ID for api"
2833
type = string
29-
default = "placeholder"
3034
}

0 commit comments

Comments
 (0)