@@ -132,12 +132,65 @@ resource "aws_api_gateway_deployment" "mcp_deployment" {
132132 ]
133133}
134134
135+ # API Gateway needs an account-level role to write logs to CloudWatch.
136+ # This is a per-region, per-account setting.
137+
138+ resource "aws_iam_role" "api_gateway_cloudwatch" {
139+ name = " ${ local . lambda_name } -apigw-cloudwatch"
140+
141+ assume_role_policy = jsonencode ({
142+ Version = " 2012-10-17"
143+ Statement = [{
144+ Action = " sts:AssumeRole"
145+ Effect = " Allow"
146+ Principal = {
147+ Service = " apigateway.amazonaws.com"
148+ }
149+ }]
150+ })
151+ }
152+
153+ resource "aws_iam_role_policy_attachment" "api_gateway_cloudwatch" {
154+ role = aws_iam_role. api_gateway_cloudwatch . name
155+ policy_arn = " arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
156+ }
157+
158+ resource "aws_api_gateway_account" "this" {
159+ cloudwatch_role_arn = aws_iam_role. api_gateway_cloudwatch . arn
160+
161+ depends_on = [aws_iam_role_policy_attachment . api_gateway_cloudwatch ]
162+ }
163+
164+ resource "aws_cloudwatch_log_group" "api_gateway_access_logs" {
165+ name = " /aws/apigateway/${ local . lambda_name } -access"
166+ retention_in_days = 14
167+
168+ tags = {
169+ Project = " mcp-server"
170+ }
171+ }
172+
135173resource "aws_api_gateway_stage" "stage" {
136174 deployment_id = aws_api_gateway_deployment. mcp_deployment . id
137175 rest_api_id = aws_api_gateway_rest_api. mcp_api . id
138176 stage_name = var. stage_name
139177
140178 xray_tracing_enabled = true
179+
180+ access_log_settings {
181+ destination_arn = aws_cloudwatch_log_group. api_gateway_access_logs . arn
182+ format = jsonencode ({
183+ requestId = " $context.requestId"
184+ ip = " $context.identity.sourceIp"
185+ requestTime = " $context.requestTime"
186+ httpMethod = " $context.httpMethod"
187+ resourcePath = " $context.resourcePath"
188+ status = " $context.status"
189+ responseLength = " $context.responseLength"
190+ })
191+ }
192+
193+ depends_on = [aws_api_gateway_account . this ]
141194}
142195
143196resource "aws_api_gateway_method_settings" "mcp_post" {
0 commit comments