DevOps人工智能代理
构建在以下基础上的智能DevOps代理 AWS基岩代理核心 使用 模型上下文协议(MCP) 通过Microsoft Teams管理AWS基础架构、监控资源运行状况和报告事件。
建筑
flowchart LR
subgraph Triggers
CW[CloudWatch Alarm] --> EB[EventBridge Rule]
EB --> LH[Lambda Handler]
end
subgraph Agent
LH --> AC[AgentCore\nReasoning Loop]
AC --> MC[MCP Client]
end
subgraph MCP Servers
MC --> AWS[AWS Infra Server\nEC2 tools]
MC --> MON[Monitoring Server\nCloudWatch tools]
MC --> TMS[Teams Server\nWebhook tools]
end
AWS --> EC2[(EC2)]
MON --> CWM[(CloudWatch\nMetrics)]
TMS --> TEA[(Teams\nWebhook)]快速开始
# 1. Create & activate virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows PowerShell
source .venv/bin/activate # Linux / macOS
# 2. Install dependencies (dev + infra extras)
make install
# or manually:
pip install -e ".[dev,infra]"
# 3. Run linter & type checker
make lint
make typecheck
# 4. Run tests
make test
# 5. Start an MCP server locally (stdio transport)
make run-mcp-awsCDK部署
先决条件
# Install the CDK CLI (one-time)
npm install -g aws-cdk
# Bootstrap CDK in your account/region (one-time)
cdk bootstrap aws://650251690796/ap-southeast-2部署所有堆栈
# Activate venv first
.venv\Scripts\activate
# Deploy everything (Networking → Monitoring → Runner)
cdk deploy --all --require-approval never部署单个堆栈
# Monitoring stack (CloudWatch alarm + EventBridge rule)
cdk deploy DevOpsAgent-Monitoring --require-approval never
# Agent Runner stack (Lambda function)
cdk deploy DevOpsAgent-Runner --require-approval never其他CDK命令
# Synthesize CloudFormation templates (no deploy)
cdk synth
# Show diff between deployed and local
cdk diff
# Destroy all stacks
cdk destroy --allCDK上下文(cdk.json)
以下上下文值配置部署:
| 关键字 | 描述 | 示例 |
|---|---|---|
region | AWS区域 | ap-southeast-2 |
monitored_instance_id | 要监视的EC2实例 | i-0bf11b006e8f12844 |
测试Lambda
使用测试事件调用
CloudWatch警报事件示例见 test_event.json:
aws lambda invoke \
--function-name devops-ai-agent-handler \
--payload fileb://test_event.json \
--cli-binary-format raw-in-base64-out \
response.json \
--region ap-southeast-2查看响应
# Linux / macOS
cat response.json | python -m json.tool
# Windows PowerShell
Get-Content response.json | python -m json.tool预期成功响应
{
"statusCode": 200,
"body": {
"alarm_name": "devops-agent-high-cpu",
"instance_id": "i-0bf11b006e8f12844",
"agent_response": "...",
"tool_calls_count": 3,
"session_id": "..."
}
}运行测试
# All tests
make test
# or: pytest -v
# Unit tests only
make test-unit
# or: pytest tests/unit/ -v
# Integration tests only
make test-integration
# or: pytest tests/integration/ -v装订和格式化
# Lint (check only)
make lint
# Auto-format + fix
make format
# Type check with mypy
make typecheck在本地运行MCP服务器
make run-mcp-aws # AWS Infra server (EC2 tools)
make run-mcp-monitoring # Monitoring server (CloudWatch tools)
make run-mcp-teams # Teams server (webhook tools)基岩模型验证
# List available models
aws bedrock list-foundation-models \
--region ap-southeast-2 \
--by-provider Anthropic \
--query "modelSummaries[].modelId" \
--output json
# Test direct model invocation
aws bedrock-runtime invoke-model \
--model-id "amazon.nova-lite-v1:0" \
--region ap-southeast-2 \
--body '{"inputText":"hello"}' \
--content-type application/json \
response_model.json
# Test inline agent from Python
python -c "
import boto3
c = boto3.client('bedrock-agent-runtime', region_name='ap-southeast-2')
r = c.invoke_inline_agent(
foundationModel='amazon.nova-lite-v1:0',
instruction='You are a helpful DevOps assistant that diagnoses infrastructure issues.',
sessionId='test-123',
inputText='Say hello',
)
print([e for e in r['completion']])
"清理
# Remove Python caches and build artifacts
make clean
# Destroy all deployed AWS resources
cdk destroy --all目录结构
devops-ai-agent/
├── infra/ # IaC (CDK stacks)
│ ├── stacks/
│ │ ├── monitoring_stack.py # CloudWatch alarms, EventBridge rules
│ │ ├── agent_runner_stack.py # Lambda for agent invocation
│ │ └── networking_stack.py # VPC, subnets, security groups
│ └── app.py
├── src/
│ ├── agent/
│ │ ├── agent_core.py # AgentCore client & reasoning bridge
│ │ ├── system_prompt.py # Agent persona & instructions
│ │ └── config.py # Env-based settings (Pydantic)
│ ├── mcp_servers/
│ │ ├── aws_infra/ # MCP server: EC2 tools
│ │ ├── monitoring/ # MCP server: CloudWatch metrics
│ │ └── teams/ # MCP server: Teams webhook
│ ├── mcp_client/
│ │ └── client.py # Unified MCP client adapter
│ ├── handlers/
│ │ ├── lambda_handler.py # EventBridge → Agent entry point
│ │ └── event_parser.py # Alarm event → typed dataclass
│ └── utils/
│ ├── aws_helpers.py # Shared boto3 helpers
│ └── teams_webhook.py # Low-level webhook HTTP helper
├── tests/
├── demo.py # 🚀 Interactive demo — see below
├── pyproject.toml
├── Makefile
└── README.md演示/帮助脚本
跑吧 demo.py 每个模块的指导性演练脚本:
python demo.py它将:
- 展示如何加载配置
- 演示MCP工具发现和调用(模拟)
- 模拟EventBridge报警事件解析
- 模拟团队通知
- 展示完整的代理端到端推理流程
先决条件
| 要求 | 注意事项 |
|---|---|
| Python 3.12+ | 必填 |
| 已配置AWS CLI | 用于boto3凭据 |
| AWS基岩代理核心访问 | 请求Claude模型访问 |
| Microsoft Teams webhook URL | 创建传入的webhook连接器 |
CDK-CLI(npm i -g aws-cdk) | 用于部署基础设施 |
环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
AWS_REGION | AWS区域 | us-east-1 |
BEDROCK_MODEL_ID | 基岩模型标识符 | anthropic.claude-3-5-sonnet-20241022-v2:0 |
AGENT_ID | 基岩药剂ID(内联模式省略) | -- |
AGENT_ALIAS_ID | 基岩药剂别名(内联模式省略) | -- |
TEAMS_WEBHOOK_URL | 团队传入Webhook URL | -- |
TOOL_TIMEOUT_SECONDS | 每次工具调用超时 | 30 |
LAMBDA_TIMEOUT_SECONDS | Lambda函数超时 | 300 |
MAX_REASONING_TURNS | 最大代理推理迭代次数 | 10 |
LOG_LEVEL | 日志记录级别 | INFO |
LOG_FORMAT | 日志格式(json 或 text) | json |
内联模式与注册模式: 当AGENT_ID和AGENT_ALIAS_ID两者都已设置,代理将使用invoke_agent(预先注册)。如果省略,则使用invoke_inline_agent(在每个请求中内联发送系统提示和工具)。
许可证
麻省理工学院
