Skip to content

Commit 0d6d9b6

Browse files
Added API Gateway module
1 parent c175f88 commit 0d6d9b6

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

terraform/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,22 @@ module "dynamodb" {
3131
source = "./modules/dynamodb"
3232
environment = var.environment
3333
common_tags = local.common_tags
34+
}
35+
36+
# IAM Module
37+
module "iam" {
38+
source = "./modules/iam"
39+
40+
environment = var.environment
41+
dynamodb_table_arn = module.dynamodb.table_arn
42+
common_tags = local.common_tags
43+
}
44+
45+
# API Gateway Module
46+
module "api_gateway" {
47+
source = "./modules/api_gateway"
48+
49+
environment = var.environment
50+
lambda_invoke_arn = module.lambda.function_invoke_arn
51+
common_tags = local.common_tags
3452
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
resource "aws_apigatewayv2_api" "health_api" {
2+
name = "${var.environment}-health-check-api"
3+
protocol_type = "HTTP"
4+
5+
cors_configuration {
6+
allow_origins = ["*"]
7+
allow_methods = ["GET", "POST", "OPTIONS"]
8+
allow_headers = ["*"]
9+
}
10+
11+
tags = var.common_tags
12+
}
13+
14+
resource "aws_apigatewayv2_integration" "lambda_integration" {
15+
api_id = aws_apigatewayv2_api.health_api.id
16+
integration_type = "AWS_PROXY"
17+
integration_method = "POST"
18+
payload_format_version = "2.0"
19+
integration_uri = var.lambda_invoke_arn
20+
depends_on = []
21+
}
22+
23+
resource "aws_apigatewayv2_route" "health_route_get" {
24+
api_id = aws_apigatewayv2_api.health_api.id
25+
route_key = "GET /health"
26+
target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}"
27+
}
28+
29+
resource "aws_apigatewayv2_route" "health_route_post" {
30+
api_id = aws_apigatewayv2_api.health_api.id
31+
route_key = "POST /health"
32+
target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}"
33+
}
34+
35+
resource "aws_apigatewayv2_stage" "default" {
36+
api_id = aws_apigatewayv2_api.health_api.id
37+
name = "$default"
38+
auto_deploy = true
39+
40+
access_log_settings {
41+
destination_arn = aws_cloudwatch_log_group.api_gateway_logs.arn
42+
format = jsonencode({
43+
requestId = "$context.requestId"
44+
ip = "$context.identity.sourceIp"
45+
requestTime = "$context.requestTime"
46+
httpMethod = "$context.httpMethod"
47+
resourcePath = "$context.resourcePath"
48+
status = "$context.status"
49+
protocol = "$context.protocol"
50+
responseLength = "$context.responseLength"
51+
})
52+
}
53+
54+
tags = var.common_tags
55+
}
56+
57+
resource "aws_cloudwatch_log_group" "api_gateway_logs" {
58+
name = "/aws/apigateway/${var.environment}-health-check-api"
59+
retention_in_days = 7
60+
61+
tags = var.common_tags
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "api_endpoint" {
2+
description = "The endpoint URL of the API Gateway"
3+
value = "${aws_apigatewayv2_api.health_api.api_endpoint}/"
4+
}
5+
6+
output "api_id" {
7+
description = "The ID of the API Gateway"
8+
value = aws_apigatewayv2_api.health_api.id
9+
}
10+
11+
output "execution_arn" {
12+
description = "The execution ARN of the API Gateway"
13+
value = aws_apigatewayv2_api.health_api.execution_arn
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "environment" {
2+
description = "Environment name"
3+
type = string
4+
}
5+
6+
variable "lambda_invoke_arn" {
7+
description = "Invoke ARN of the Lambda function"
8+
type = string
9+
}
10+
11+
variable "common_tags" {
12+
description = "Common tags for all resources"
13+
type = map(string)
14+
}

0 commit comments

Comments
 (0)