Skip to content

Commit 08618c2

Browse files
brendanbabbclaude
andcommitted
Tag CloudWatch log groups for mcp-observability discovery
Add Project = "mcp-server" tag to the Lambda log group so the separate mcp-observability project can discover it via the Resource Groups Tagging API. The API Gateway access log group did not previously exist, so this also adds the supporting infrastructure to make access logging work: - aws_cloudwatch_log_group.api_gateway_access_logs (tagged) - access_log_settings block on the stage pointing at it - aws_api_gateway_account + IAM role, the account-level prerequisite API Gateway needs to write logs to CloudWatch Tags only take effect on the real resources after a deploy. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent cfb9a5c commit 08618c2

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

terraform/aws/api_gateway.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
135173
resource "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

143196
resource "aws_api_gateway_method_settings" "mcp_post" {

terraform/aws/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,8 @@ resource "aws_lambda_function" "mcp_server" {
9191
resource "aws_cloudwatch_log_group" "lambda_logs" {
9292
name = "/aws/lambda/${local.lambda_name}"
9393
retention_in_days = 14
94+
95+
tags = {
96+
Project = "mcp-server"
97+
}
9498
}

0 commit comments

Comments
 (0)