Skip to content

Commit 2e9ac19

Browse files
MukeshMukesh
authored andcommitted
push
1 parent 679c07d commit 2e9ac19

3 files changed

Lines changed: 43 additions & 18 deletions

File tree

modules/api-gateways/main.tf

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,48 @@ terraform {
99
}
1010
}
1111

12-
# Create API Gateway
13-
resource "aws_apigatewayv2_api" "my_api" {
14-
name = "secure-api-101"
12+
# Create an HTTP API
13+
resource "aws_apigatewayv2_api" "example" {
14+
name = "example-http-api"
1515
protocol_type = "HTTP"
16+
description = "Example HTTP API Gateway"
1617
}
1718

18-
# Create API Gateway Stage
19-
resource "aws_apigatewayv2_stage" "dev" {
20-
api_id = aws_apigatewayv2_api.my_api.id
21-
name = "dev"
22-
auto_deploy = true
19+
# "/api/v1/hi" route
20+
resource "aws_apigatewayv2_route" "api_v1_hi_route" {
21+
api_id = aws_apigatewayv2_api.example.id
22+
route_key = "GET /api/v1/hi"
23+
target = "integrations/${aws_apigatewayv2_integration.api_v1_hi_integration.id}"
24+
}
25+
26+
# "/" route
27+
resource "aws_apigatewayv2_route" "root_route" {
28+
api_id = aws_apigatewayv2_api.example.id
29+
route_key = "GET /"
30+
target = "integrations/${aws_apigatewayv2_integration.root_integration.id}"
2331
}
2432

25-
# Attach API Gateway to ALB
26-
resource "aws_apigatewayv2_integration" "alb" {
27-
api_id = aws_apigatewayv2_api.my_api.id
28-
integration_type = "HTTP_PROXY"
33+
# Integration for the "/" route
34+
resource "aws_apigatewayv2_integration" "root_integration" {
35+
api_id = aws_apigatewayv2_api.example.id
36+
integration_type = "HTTP"
2937
integration_uri = var.alb_dns
30-
integration_method = "ANY"
38+
integration_method = "GET"
39+
payload_format_version = "1.0"
3140
}
3241

33-
# Create Route for API Gateway
34-
resource "aws_apigatewayv2_route" "hello_route" {
35-
api_id = aws_apigatewayv2_api.my_api.id
36-
route_key = "GET /api/v1/hi"
37-
target = "integrations/${aws_apigatewayv2_integration.alb.id}"
42+
# Integration for the "/api/v1/hi" route
43+
resource "aws_apigatewayv2_integration" "api_v1_hi_integration" {
44+
api_id = aws_apigatewayv2_api.example.id
45+
integration_type = "HTTP"
46+
integration_uri = "${var.alb_dns}/api/v1/hi"
47+
integration_method = "GET"
48+
payload_format_version = "1.0"
49+
}
50+
51+
# "dev" stage for the HTTP API
52+
resource "aws_apigatewayv2_stage" "dev_stage" {
53+
api_id = aws_apigatewayv2_api.example.id
54+
stage_name = "dev"
55+
auto_deploy = true
3856
}

modules/api-gateways/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "api_gateway_url" {
2+
value = "https://${aws_apigatewayv2_api.example.id}.execute-api.${var.region}.amazonaws.com/dev/"
3+
}

modules/api-gateways/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
variable "alb_dns" {
22
type = string
33
}
4+
5+
variable "region" {
6+
type = string
7+
}

0 commit comments

Comments
 (0)