Skip to content

Commit 3ed1f7b

Browse files
committed
Add logs for gateway
1 parent 38c9af9 commit 3ed1f7b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

terraform/api_gateway.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,40 @@ resource "aws_api_gateway_stage" "api_stage" {
194194
deployment_id = aws_api_gateway_deployment.api_deployment.id
195195
rest_api_id = aws_api_gateway_rest_api.api.id
196196
stage_name = "dev"
197+
198+
access_log_settings {
199+
destination_arn = aws_cloudwatch_log_group.api_gateway_logs.arn
200+
format = jsonencode({
201+
requestId = "$context.requestId",
202+
ip = "$context.identity.sourceIp",
203+
caller = "$context.identity.caller",
204+
user = "$context.identity.user",
205+
requestTime = "$context.requestTime",
206+
httpMethod = "$context.httpMethod",
207+
resourcePath = "$context.resourcePath",
208+
status = "$context.status",
209+
protocol = "$context.protocol",
210+
responseLength = "$context.responseLength"
211+
})
212+
}
213+
214+
xray_tracing_enabled = true
215+
}
216+
217+
resource "aws_api_gateway_method_settings" "method_settings" {
218+
rest_api_id = aws_api_gateway_rest_api.api.id
219+
stage_name = aws_api_gateway_stage.api_stage.stage_name
220+
221+
method_path = "*/*"
222+
223+
settings {
224+
metrics_enabled = true
225+
logging_level = "INFO"
226+
data_trace_enabled = true
227+
}
197228
}
198229

230+
199231
output "api_gateway_invoke_url" {
200232
value = "https://${aws_api_gateway_rest_api.api.id}.execute-api.sa-east-1.amazonaws.com/${aws_api_gateway_stage.api_stage.stage_name}/api/v1/products"
201233
}

terraform/iam.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,28 @@ resource "aws_iam_role_policy_attachment" "lambda_invoke_policy_attachment" {
105105
role = aws_iam_role.lambda_role.name
106106
policy_arn = aws_iam_policy.lambda_invoke_policy.arn
107107
}
108+
109+
resource "aws_iam_role" "api_gateway_role" {
110+
name = "api-gateway-cloudwatch-logs-role"
111+
112+
assume_role_policy = jsonencode({
113+
Version = "2012-10-17"
114+
Statement = [
115+
{
116+
Action = "sts:AssumeRole"
117+
Effect = "Allow"
118+
Principal = {
119+
Service = "apigateway.amazonaws.com"
120+
}
121+
},
122+
]
123+
})
124+
125+
managed_policy_arns = [
126+
"arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
127+
]
128+
}
129+
130+
resource "aws_api_gateway_account" "api_account" {
131+
cloudwatch_role_arn = aws_iam_role.api_gateway_role.arn
132+
}

0 commit comments

Comments
 (0)