Skip to content

Commit 33025c4

Browse files
committed
feat: api gateway config and other alb and vpc config
1 parent d0b6c76 commit 33025c4

File tree

10 files changed

+205
-10
lines changed

10 files changed

+205
-10
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44
push:
55
branches:
66
- main
7+
paths-ignore:
8+
- "**/README.md"
79
workflow_dispatch:
810

911
jobs:

api_gateway.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ resource "aws_apigatewayv2_stage" "main" {
1515
}
1616

1717
tags = {
18-
Name = "SOAT-TC API Default Stage"
18+
Name = "SOAT-TC API GW Default Stage"
1919
}
2020
}

api_gateway_integrations.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@ resource "aws_apigatewayv2_integration" "debug_integration" {
77
}
88

99

10-
resource "aws_apigatewayv2_vpc_link" "load_balancer" {
11-
name = "SOAT-TC API Gateway Private Subnets VPC Link"
10+
resource "aws_apigatewayv2_vpc_link" "private_subnets" {
11+
name = "SOAT-TC API GW Private Subnets VPC Link"
1212
subnet_ids = aws_subnet.private_subnets[*].id
1313
security_group_ids = [aws_default_security_group.default.id]
1414
}
15+
16+
resource "aws_apigatewayv2_integration" "proxy_to_alb" {
17+
api_id = aws_apigatewayv2_api.main.id
18+
description = "Will forward requests to the internal Application Load Balancer to access ECS services."
19+
integration_type = "HTTP_PROXY"
20+
integration_uri = aws_lb_listener.main.arn
21+
22+
integration_method = "ANY"
23+
connection_type = "VPC_LINK"
24+
connection_id = aws_apigatewayv2_vpc_link.private_subnets.id
25+
}

api_gateway_outputs.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
output "api_gateway_api" {
1+
output "api_gw_gateway_api" {
22
description = "HTTP API"
33
value = {
44
"api_endpoint" : aws_apigatewayv2_api.main.api_endpoint
@@ -11,7 +11,7 @@ output "api_gateway_api" {
1111
}
1212
}
1313

14-
output "api_gateway_stage" {
14+
output "api_gw_gateway_stage" {
1515
description = "Default Stage"
1616
value = {
1717
"api_id" : aws_apigatewayv2_stage.main.api_id
@@ -21,3 +21,7 @@ output "api_gateway_stage" {
2121
"tags" : aws_apigatewayv2_stage.main.tags
2222
}
2323
}
24+
25+
# output "api_gw_routes_to_be_integrated" {
26+
27+
# }

api_gateway_routes.tf

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,29 @@ resource "aws_apigatewayv2_route" "debug_route" {
99
resource "aws_apigatewayv2_route" "client_identification" {
1010
api_id = aws_apigatewayv2_api.main.id
1111
route_key = "POST /identification/clients/identification"
12+
1213
// Identification Lambda integration
1314
}
1415

15-
resource "aws_apigatewayv2_route" "order_checkout" {
16+
resource "aws_apigatewayv2_route" "order_checkout_and_listing" {
1617
api_id = aws_apigatewayv2_api.main.id
17-
route_key = "POST /order/orders"
18-
// Client Lambda Authorizer integration
18+
route_key = "ANY /order/orders" // due to Servlet Filter urlPatterns not supporting specific HTTP methods
19+
20+
// Client Lambda Authorizer authorization
21+
target = "integrations/${aws_apigatewayv2_integration.proxy_to_alb.id}"
1922
}
2023

2124
resource "aws_apigatewayv2_route" "order_confirmation" {
2225
api_id = aws_apigatewayv2_api.main.id
2326
route_key = "POST /payment/payments/initialize"
24-
// Client Lambda Authorizer integration
27+
28+
// Client Lambda Authorizer authorization
29+
target = "integrations/${aws_apigatewayv2_integration.proxy_to_alb.id}"
2530
}
2631

2732
resource "aws_apigatewayv2_route" "forward_to_alb_route" {
2833
api_id = aws_apigatewayv2_api.main.id
2934
route_key = "ANY /{proxy+}"
30-
// Private Resource integration (vpc link + alb)
35+
36+
target = "integrations/${aws_apigatewayv2_integration.proxy_to_alb.id}"
3137
}

cloudwatch.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ resource "aws_api_gateway_account" "main" {
33
cloudwatch_role_arn = data.aws_iam_role.lab_role.arn
44
}
55

6+
#tfsec:ignore:aws-cloudwatch-log-group-customer-key
67
resource "aws_cloudwatch_log_group" "api_gateway_access_logs" {
78
name = "/aws/apigateway/SOAT-TC_API_Gateway_Access_Logs"
89
retention_in_days = 30
10+
11+
tags = {
12+
Name : "SOAT-TC API Gateway Default Stage Access Logs"
13+
}
914
}

lb.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
resource "aws_lb" "main" {
2+
name = "SOAT-TC-ALB"
3+
internal = true
4+
load_balancer_type = "application"
5+
ip_address_type = "ipv4"
6+
7+
security_groups = [aws_default_security_group.default.id]
8+
subnets = aws_subnet.private_subnets[*].id
9+
10+
drop_invalid_header_fields = true
11+
12+
tags = {
13+
Name : "SOAT Tech Challenge Internal Application Load Balancer"
14+
}
15+
}
16+
17+
resource "aws_lb_listener" "main" {
18+
load_balancer_arn = aws_lb.main.arn
19+
port = 80
20+
protocol = "HTTP" #tfsec:ignore:aws-elb-http-not-used
21+
22+
default_action {
23+
type = "fixed-response"
24+
fixed_response {
25+
content_type = "text/plain"
26+
status_code = "418"
27+
message_body = "SOAT Tech Challenge - Invalid destination"
28+
}
29+
}
30+
31+
tags = {
32+
Name : "SOAT-TC ALB HTTP Listener"
33+
}
34+
}

lb_listener_rules.tf

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
resource "aws_lb_listener_rule" "identification_svc_rule" {
2+
listener_arn = aws_lb_listener.main.arn
3+
priority = 10
4+
5+
action {
6+
type = "forward"
7+
target_group_arn = aws_lb_target_group.ecs_identification_svc_tg.arn
8+
}
9+
10+
condition {
11+
path_pattern {
12+
values = ["/identification/*"]
13+
}
14+
}
15+
16+
tags = {
17+
Name : "SOAT-TC ALB Identification Service Listener Rule"
18+
}
19+
}
20+
21+
resource "aws_lb_listener_rule" "order_svc_rule" {
22+
listener_arn = aws_lb_listener.main.arn
23+
priority = 20
24+
25+
action {
26+
type = "forward"
27+
target_group_arn = aws_lb_target_group.ecs_order_svc_tg.arn
28+
}
29+
30+
condition {
31+
path_pattern {
32+
values = ["/order/*"]
33+
}
34+
}
35+
36+
tags = {
37+
Name : "SOAT-TC ALB Order Service Listener Rule"
38+
}
39+
}
40+
41+
resource "aws_lb_listener_rule" "payment_svc_rule" {
42+
listener_arn = aws_lb_listener.main.arn
43+
priority = 30
44+
45+
action {
46+
type = "forward"
47+
target_group_arn = aws_lb_target_group.ecs_payment_svc_tg.arn
48+
}
49+
50+
condition {
51+
path_pattern {
52+
values = ["/payment/*"]
53+
}
54+
}
55+
56+
tags = {
57+
Name : "SOAT-TC ALB Payment Service Listener Rule"
58+
}
59+
}
60+
61+
resource "aws_lb_listener_rule" "production_svc_rule" {
62+
listener_arn = aws_lb_listener.main.arn
63+
priority = 40
64+
65+
action {
66+
type = "forward"
67+
target_group_arn = aws_lb_target_group.ecs_production_svc_tg.arn
68+
}
69+
70+
condition {
71+
path_pattern {
72+
values = ["/production/*"]
73+
}
74+
}
75+
76+
tags = {
77+
Name : "SOAT-TC ALB Production Service Listener Rule"
78+
}
79+
}

lb_target_groups.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ECS service will register themselves into these target groups
2+
3+
4+
resource "aws_lb_target_group" "ecs_identification_svc_tg" {
5+
name = "SOAT-TC-ALB-IdentificationSVC-TG"
6+
port = 80
7+
protocol = "HTTP"
8+
target_type = "ip"
9+
vpc_id = aws_vpc.main.id
10+
11+
tags = {
12+
Name : "SOAT-TC ALB Identification Service Target Group"
13+
}
14+
}
15+
16+
resource "aws_lb_target_group" "ecs_order_svc_tg" {
17+
name = "SOAT-TC-ALB-OrderSVC-TG"
18+
port = 80
19+
protocol = "HTTP"
20+
target_type = "ip"
21+
vpc_id = aws_vpc.main.id
22+
23+
tags = {
24+
Name : "SOAT-TC ALB Order Service Target Group"
25+
}
26+
}
27+
28+
resource "aws_lb_target_group" "ecs_payment_svc_tg" {
29+
name = "SOAT-TC-ALB-PaymentSVC-TG"
30+
port = 80
31+
protocol = "HTTP"
32+
target_type = "ip"
33+
vpc_id = aws_vpc.main.id
34+
35+
tags = {
36+
Name : "SOAT-TC ALB Payment Service Target Group"
37+
}
38+
}
39+
40+
41+
resource "aws_lb_target_group" "ecs_production_svc_tg" {
42+
name = "SOAT-TC-ALB-ProductionSVC-TG"
43+
port = 80
44+
protocol = "HTTP"
45+
target_type = "ip"
46+
vpc_id = aws_vpc.main.id
47+
48+
tags = {
49+
Name : "SOAT-TC ALB Production Service Target Group"
50+
}
51+
}

vpc.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
resource "aws_vpc" "main" {
44
cidr_block = "10.0.0.0/16"
55

6+
enable_dns_hostnames = true
7+
enable_dns_support = true
8+
69
tags = {
710
Name = "SOAT Tech Challenge VPC"
811
}

0 commit comments

Comments
 (0)