-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent_setup.py
More file actions
129 lines (99 loc) · 4.81 KB
/
Copy pathagent_setup.py
File metadata and controls
129 lines (99 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
"""
CloudWatch Agent Setup using MCP
This leverages the existing CloudWatch MCP to get actual alarm recommendations
and create monitoring setup for the preemptive assessment agent.
"""
import os
import json
from strands import Agent
from strands_tools import use_aws, file_write
from strands.tools.mcp import MCPClient
from mcp import stdio_client, StdioServerParameters
def create_cloudwatch_agent():
"""Create an agent that can work with CloudWatch and Application Signals MCP"""
# Initialize CloudWatch MCP client (same as agent.py)
cloudwatch_mcp_client = MCPClient(lambda: stdio_client(
StdioServerParameters(
command="uvx",
args=["awslabs.cloudwatch-mcp-server@latest"]
)
))
# Initialize Application Signals MCP client (same as agent.py)
appsignals_mcp_client = MCPClient(lambda: stdio_client(
StdioServerParameters(
command="uvx",
args=["awslabs.cloudwatch-appsignals-mcp-server@latest"]
)
))
# Agent prompt for monitoring setup - actually create resources
monitoring_prompt = """
You are a CloudWatch and Application Signals expert who CREATES actual monitoring infrastructure for a preemptive assessment agent.
Your task is to:
1. Read the service configuration from service_config.json
2. Use the AWS region specified in the configuration for all AWS operations
3. CREATE actual CloudWatch alarms using the available MCP tools for:
- Human operators (critical thresholds requiring immediate attention)
- Agent triggers (early warning thresholds for preemptive assessment)
4. CREATE EventBridge rules that trigger when agent alarms fire
5. Focus on CPU, memory, error rates, traffic patterns, latency, and application-level metrics
You must ACTUALLY CREATE these resources, not just provide recommendations.
Use the CloudWatch and Application Signals MCP tools to create alarms with proper:
- Metric names and namespaces
- Thresholds and comparison operators
- Evaluation periods and datapoints to alarm
- Alarm descriptions and tags
- EventBridge integration for agent triggers
Write a summary of created resources to monitoring_setup_summary.md.
"""
with cloudwatch_mcp_client, appsignals_mcp_client:
cloudwatch_tools = cloudwatch_mcp_client.list_tools_sync()
appsignals_tools = appsignals_mcp_client.list_tools_sync()
# Create agent with both CloudWatch and Application Signals tools (same pattern as agent.py)
agent = Agent(
system_prompt=monitoring_prompt,
tools=[use_aws, file_write, cloudwatch_tools + appsignals_tools]
)
return agent, cloudwatch_mcp_client, appsignals_mcp_client
def setup_monitoring():
"""Set up CloudWatch monitoring using MCP tools - actually create alarms and EventBridge rules"""
# Set AWS region for demo
aws_region = "us-east-2"
os.environ['AWS_DEFAULT_REGION'] = aws_region
try:
# Create CloudWatch agent that will actually create alarms
agent, cloudwatch_client, appsignals_client = create_cloudwatch_agent()
# Use the agent within both MCP client contexts
with cloudwatch_client, appsignals_client:
response = agent(
f"""CREATE actual CloudWatch alarms for the preemptive assessment agent demo.
IMPORTANT: You need to create TWO types of alarms for a demo web service:
1. HUMAN ALARMS (for operators):
- demo-web-api-CPU-Critical: CPU > 90% for 2 periods
- demo-web-api-Memory-Critical: Memory > 95% for 2 periods
- demo-web-api-ErrorRate-Critical: Error rate > 5% for 1 period
2. AGENT TRIGGER ALARMS (for preemptive assessment):
- demo-web-api-CPU-Warning: CPU > 70% for 1 period
- demo-web-api-Latency-Warning: P95 latency increase > 15% for 1 period
- demo-web-api-Traffic-Surge: Request count increase > 20% for 1 period
For each alarm:
- Use appropriate CloudWatch metrics (AWS/EC2, AWS/ApplicationELB, or Application Signals)
- Set proper thresholds and evaluation periods
- Add alarm descriptions explaining their purpose
- Tag alarms with Purpose=Human or Purpose=AgentTrigger
After creating alarms, also create EventBridge rules that:
- Listen for CloudWatch alarm state changes to ALARM
- Filter for alarms tagged with Purpose=AgentTrigger
- Target a Lambda function or SNS topic named 'preemptive-assessment-agent'
Use the AWS region: {aws_region}
Actually CREATE these resources, don't just provide recommendations."""
)
return response
except Exception as e:
# If MCP fails, raise the exception - no fallbacks
raise RuntimeError(f"CloudWatch MCP setup failed: {e}") from e
def main():
"""Main function to set up CloudWatch monitoring"""
return setup_monitoring()
if __name__ == "__main__":
main()