ChiragBarsaiya/bedrock-agentcore-fullstack

GitHub: ChiragBarsaiya/bedrock-agentcore-fullstack

基于 Amazon Bedrock AgentCore 构建的 AI 驱动 DevOps 事件响应全栈应用,通过自然语言交互实现 AWS 生产环境的实时故障诊断与处置。

Stars: 0 | Forks: 0

# DevOps 事件响应 Agent — AWS Bedrock AgentCore 一个 AI 驱动的 DevOps 助手,可以实时诊断和响应生产环境中的事件。基于 **Amazon Bedrock AgentCore** 和 **Strands Agents SDK** 构建,配备全栈 React 前端、Cognito 身份验证以及完全自动化的 CDK 基础设施。 这是一个关于 **AWS 上的 Agentic AI** 的动手实践探索——涵盖了从构建调用实时 AWS API 的自定义工具,到通过单条命令部署一个生产级的多栈 CDK 应用程序的全过程。 ## 功能简介 该 Agent 在事件发生时充当第一响应者。您可以用自然语言向它提问,它将会: - **查询 CloudWatch Logs** — 从任何日志组中提取错误模式、异常和异常波动 - **拉取 CloudWatch Metrics** — 获取 CPU、延迟、错误率及其他指标,并附带统计摘要 - **检查基础设施健康状况** — 检查 EC2 实例状态、RDS 数据库状态以及 Lambda 函数配置 - **检查 AWS Service Health Dashboard** — 在调查您的应用程序之前,主动排除 AWS 侧的故障 - **综合分析结果** — 关联多个数据源的信息,并推荐具体的补救步骤 **示例提示词:** ``` What errors appeared in /aws/lambda/my-api in the last hour? Show me CPU utilization for my EC2 instances over the last 4 hours Is there any active AWS outage in us-east-1 that might explain this latency spike? Analyze the error rate trend for my RDS instance ``` ## 架构 ``` Browser (React + Cloudscape) │ │ JWT Bearer Token (Cognito) ▼ Amazon Bedrock AgentCore Runtime │ (ARM64 container, isolated microVM) ▼ Strands Agent (Claude Haiku 4.5 via Bedrock) ├── query_cloudwatch_logs() ├── get_cloudwatch_metrics() ├── check_aws_service_health() └── check_aws_service_health_dashboard() ``` ![架构图](https://static.pigsec.cn/wp-content/uploads/repos/cas/c8/c8d558bcd822bb9d9805a02ea591b1e986bb0955087f9afaff9b614e3bebbebc.svg) ### 基础设施 (AWS CDK — 4 个 Stack) | Stack | 用途 | 关键资源 | |---|---|---| | **AgentCoreInfra** | 构建流水线 | ECR, CodeBuild (ARM64), IAM, S3 | | **AgentCoreAuth** | 用户管理 | Cognito User Pool + Client | | **AgentCoreRuntime** | Agent 运行时 | AgentCore Runtime, Lambda waiter | | **AgentCoreFrontend** | Web UI | S3, CloudFront (OAC), React app | ## 技术栈 | 层级 | 技术 | |---|---| | AI Agent | [Strands Agents SDK](https://github.com/awslabs/strands) | | LLM | Anthropic Claude Haiku 4.5 (通过 Amazon Bedrock) | | Agent 运行时 | Amazon Bedrock AgentCore | | 基础设施 | AWS CDK (TypeScript) | | 前端 | React + TypeScript + [Cloudscape Design System](https://cloudscape.design/) | | 认证 | Amazon Cognito (JWT, 邮箱验证) | | CDN | CloudFront + S3 (Origin Access Control) | | 容器 | ARM64 Docker (通过 CodeBuild 构建 — 无需本地 Docker) | | 监控 | CloudWatch Logs, Metrics, X-Ray tracing | ## 项目结构 ``` ├── sample-amazon-bedrock-agentcore-fullstack-webapp/ │ ├── agent/ │ │ ├── strands_agent.py # Agent implementation + custom DevOps tools │ │ ├── requirements.txt # Python dependencies │ │ └── Dockerfile # ARM64 container (built by CodeBuild) │ │ │ ├── cdk/ │ │ ├── bin/app.ts # CDK app entry point │ │ └── lib/ │ │ ├── infra-stack.ts # ECR, CodeBuild, IAM │ │ ├── auth-stack.ts # Cognito User Pool │ │ ├── runtime-stack.ts # AgentCore Runtime + Lambda waiter │ │ └── frontend-stack.ts # CloudFront + S3 │ │ │ ├── frontend/src/ │ │ ├── App.tsx # Chat UI (Cloudscape GenAI components) │ │ ├── AuthModal.tsx # Sign in / sign up flow │ │ ├── auth.ts # Cognito token management │ │ └── agentcore.ts # Direct AgentCore invocation with JWT │ │ │ ├── scripts/ │ │ ├── build-frontend.sh # Injects ARN + Cognito config at build time │ │ └── build-frontend.ps1 # Windows equivalent │ │ │ ├── deploy-all.sh # One-command full deployment (macOS/Linux) │ └── deploy-all.ps1 # One-command full deployment (Windows) │ ├── test-scenarios/ # CloudFormation stacks for demo Lambda errors │ ├── deploy-test-lambdas.yaml # Deploys error/slow/high-memory Lambda functions │ ├── lambda-error-simulator.py # Generates realistic incident log data │ ├── deploy.sh # Deploy test scenario │ └── cleanup.sh # Cleanup test resources │ ├── devops_agent_code_reference.py # Standalone agent code reference ├── devops-permissions-policy.json # Minimal IAM policy for the agent └── update_frontend_prompts.py # Script to update UI sample prompts ``` ## 关键工程决策 ### 为什么选择 AgentCore 而不是自托管? AgentCore 开箱即用地提供了内置的 JWT 身份验证、每个请求独立的 microVM 隔离执行、X-Ray tracing 以及 CloudWatch 可观测性——所有这些都无需在运行时管理服务器或容器。 ### 为什么使用 Lambda waiter Custom Resource? CDK 通过 CloudFormation 进行同步部署。CodeBuild 的容器构建需要 5-10 分钟,且其 API 响应超过了 CloudFormation 4 KB 的 Custom Resource 限制。Lambda waiter 会在内部轮询 CodeBuild,并仅向 CloudFormation 返回成功/失败信号,从而在保持部署同步的同时,不会阻塞基础设施的创建。 ### 为什么使用 CodeBuild 进行容器构建? AgentCore 需要 ARM64 镜像。CodeBuild 原生运行在 ARM64 上,避免了模拟带来的性能开销。这也意味着不需要本地的 Docker Desktop——构建过程完全在 AWS 中运行。 ### 为什么拆分为四个 CDK Stack? 每个 Stack 的变更频率不同。将它们拆分意味着更新 Agent 代码时只需重新部署 `AgentCoreRuntime`(约 10 分钟),而无需重新部署整个基础设施。修改前端 CSS 也只需重新部署 `AgentCoreFrontend`(约 2 分钟)。 ### 前端直接调用 AgentCore React 前端使用 HTTPS + JWT Bearer token (Cognito access token) 直接调用 AgentCore。这避免了为演示项目额外添加后端 API 层,充分利用了 AgentCore 内置的 Cognito JWT authorizer。 ## 自行部署 ### 前置条件 - **AWS CLI v2.31.13+** (于 2025 年 1 月添加了对 AgentCore 的支持) - **Node.js 22+** - **AWS 凭证**:配置好具有以下服务权限的凭证:CloudFormation, Bedrock, Lambda, S3, ECR, CodeBuild, Cognito, IAM, CloudFront - 确认您的区域支持 AgentCore:[AgentCore 支持区域](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/agentcore-regions.html) ### 一键部署 ``` # macOS / Linux cd sample-amazon-bedrock-agentcore-fullstack-webapp chmod +x deploy-all.sh scripts/build-frontend.sh ./deploy-all.sh ``` ``` # Windows cd sample-amazon-bedrock-agentcore-fullstack-webapp .\deploy-all.ps1 ``` **约 10 分钟。** 大部分时间花在 CodeBuild 构建 ARM64 容器镜像上。CloudFront URL 会在最后打印出来。 ### 本地开发 (无需部署) ``` # macOS / Linux cd sample-amazon-bedrock-agentcore-fullstack-webapp chmod +x dev-local.sh ./dev-local.sh ``` ``` # Windows cd sample-amazon-bedrock-agentcore-fullstack-webapp .\dev-local.ps1 ``` 这会在 `http://localhost:8080` 启动 Agent,并在 `http://localhost:5173` 启动 Vite 开发服务器。本地无需身份验证。修改 Agent 代码后重启仅需约 10 秒,而完整重新部署大约需要 10 分钟。 ### 测试场景 (真实事件模拟) ``` cd test-scenarios ./deploy.sh # Deploys 4 Lambda functions that simulate errors, timeouts, and high memory ./cleanup.sh # Removes all test resources ``` 部署完成后,您可以要求 Agent 调查 `demo-error-simulator`、`demo-slow-response`、`demo-api-errors` 或 `demo-high-memory` Lambda 函数。 ### 清理环境 ``` cd sample-amazon-bedrock-agentcore-fullstack-webapp/cdk npx cdk destroy AgentCoreFrontend --no-cli-pager npx cdk destroy AgentCoreRuntime --no-cli-pager npx cdk destroy AgentCoreAuth --no-cli-pager npx cdk destroy AgentCoreInfra --no-cli-pager ``` ## 扩展 Agent ### 添加新工具 ``` # 在 agent/strands_agent.py @tool def restart_ecs_service(cluster_name: str, service_name: str): """Restart an ECS service by forcing a new deployment.""" ecs = boto3.client('ecs') ecs.update_service( cluster=cluster_name, service=service_name, forceNewDeployment=True ) return f"Triggered new deployment for {service_name} in {cluster_name}" ``` 然后将其添加到 `Agent(...)` 构造函数的 `tools=[...]` 列表中。重新部署运行时: ``` cd sample-amazon-bedrock-agentcore-fullstack-webapp/cdk npx cdk deploy AgentCoreRuntime --no-cli-pager ``` ### 更改 LLM 编辑 `agent/strands_agent.py` 中的 `model_id`: ``` model_id = "us.anthropic.claude-opus-4-8-20251101-v1:0" # Upgrade to Opus ``` ### 更新 system prompt Agent 的行为由 `Agent(...)` 中的 `system_prompt` 参数控制。编辑它即可调整语气、范围或特定领域的侧重点。 ## 成本估算 轻度使用(每月 100-500 次请求),us-east-1 区域: | 服务 | 预估成本 | |---|---| | AgentCore Runtime | 约 $0.50-2/月 | | Bedrock (Claude Haiku 4.5) | 约 $0.50-3/月 | | CloudFront + S3 | 免费层可覆盖轻度使用量 | | Cognito | 10,000 MAU 以内免费 | | ECR | 约 $0.10/月 | | CloudWatch Logs | 约 $0.50-1/月 | | **总计** | **约 $3-10/月** | ## 资源 - [Amazon Bedrock AgentCore 文档](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-agentcore.html) - [Strands Agents SDK](https://github.com/awslabs/strands) - [AgentCore JWT 身份验证](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-oauth.html#invoke-agent) - [Cloudscape Design System](https://cloudscape.design/) - [CDK API 参考](https://docs.aws.amazon.com/cdk/api/v2/) ## 许可证 MIT-0 — 详情请见 [LICENSE](./sample-amazon-bedrock-agentcore-fullstack-webapp/LICENSE)
标签:AgentCore, AIOps, AI智能体, AWS, DPI, 故障响应, 漏洞探索, 自动化攻击, 逆向工具