View a markdown version of this page

创建测试环境 - AWS DevOps 代理人

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

创建测试环境

本指南提供动手测试,以使用示例架构验证 AWS DevOps 代理的事件响应功能。如果您想在连接生产系统之前测试 DevOps 代理,请使用此补充工具。

先决条件

  • AWS 具有管理访问权限的账户

  • AWS DevOps 使用自动创建代理角色流程创建和配置的 DevOps 代理空间

  • 对于 EC2 测试:在您要部署的区域中至少有一个子网的现有 VPC。

成本和安全概述

成本保障

  • EC2 测试:免费(AWS 免费套餐)或 2 小时约 0.02 美元

  • Lambda 测试:免费(100 万 requests/month 免费套餐)

  • CloudWatch: 免费(10 个警报,包括基本指标)

  • 预计总成本:0.00-0.05 美元,用于完成测试

这些测试中的安全功能

  • Auto-termination: Built-in 自动关机

  • 符合免费套餐资格:使用最小的实例类型

  • 范围有限:最少、孤立的测试资源

  • 轻松清理:通过简单的控制台步骤即可删除所有内容

  • 不影响生产:完全独立的测试环境

设置你的 AWS 用于测试的账户

重要

基础设施资源需要部署在您创建 DevOps Agent Space 主云帐户的账户中。 AWS 具体区域无关紧要。

  1. 登录 AWS 控制台:https://console.aws.amazon.com

  2. 确保你使用的是 DevOps 代理空间所在的同一个 AWS 账户

  3. 您可以使用任何区域作为测试资源

注意

DevOps 代理的主账户和正在创建的测试环境资源之间的 1:1 映射简化了测试设置。您可以轻松地扩展 DevOps 代理空间以包括辅助账户并启用跨账户调查。

选择你的测试

你可以单独运行测试,也可以同时运行这两个测试:

测试选项 A:EC2 CPU 容量测试

目的:验证 AWS DevOps 代理检测和调查 EC2 性能问题的能力

预计时间:5 分钟设置 + 10 分钟自动执行

难度:全自动(无需手动步骤)

测试选项 B:Lambda 错误率测试

目的:验证 AWS DevOps 代理检测和调查 Lambda 函数错误的能力

预计时间:10 分钟设置 + 2 分钟触发

难度:非常简单

测试选项 A:EC2 CPU 容量测试

第 1 步:为 EC2 测试部署 CloudFormation 堆栈

我们将使用它 CloudFormation 来创建我们的测试资源,这样 AWS DevOps Agent 就可以正确地跟踪和调查它们。

  1. 导航到 CloudFormation

    1. 在 AWS 控制台中,搜索 “CloudFormation” 并选择 CloudFormation

    2. 选择创建堆栈 > 使用新资源(标准)

  2. 上传模板

    1. 创建一个名为的新本地文件 AWS-DevOpsAgent-ec2-test.yaml

    2. 将此 CloudFormation 模板复制并粘贴到文件中:

      1. AWSTemplateFormatVersion: '2010-09-09' Description: 'AWS DevOps Agent EC2 CPU Test Stack' Parameters: VpcId: Type: AWS::EC2::VPC::Id Description: ID of an existing VPC where the test instance will be launched. SubnetId: Type: AWS::EC2::Subnet::Id Description: ID of an existing subnet within the selected VPC. Choose a subnet that routes to an internet gateway if you plan to connect via SSH. MyIP: Type: String Description: Your current IP address for SSH access (find at https://whatismyipaddress.com) Default: '0.0.0.0/0' Resources: # Security Group for SSH access TestSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: AWS DevOps Agent beta testing security group VpcId: !Ref VpcId SecurityGroupIngress: - IpProtocol: tcp FromPort: 22 ToPort: 22 CidrIp: !Ref MyIP Description: SSH access from your IP Tags: - Key: Name Value: AWS-DevOpsAgent-Test-SG - Key: Purpose Value: AWS-DevOpsAgent-Testing # Key Pair for SSH access TestKeyPair: Type: AWS::EC2::KeyPair Properties: KeyName: AWS-DevOpsAgent-test-key KeyType: rsa Tags: - Key: Name Value: AWS-DevOpsAgent-Test-Key - Key: Purpose Value: AWS-DevOpsAgent-Testing # IAM Role for Session Manager access SSMInstanceRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: ec2.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore Tags: - Key: Name Value: AWS-DevOpsAgent-Test-SSMRole - Key: Purpose Value: AWS-DevOpsAgent-Testing # Instance profile wrapping the SSM role SSMInstanceProfile: Type: AWS::IAM::InstanceProfile Properties: Roles: - !Ref SSMInstanceRole # EC2 Instance for CPU testing TestInstance: Type: AWS::EC2::Instance Properties: InstanceType: t3.micro ImageId: '{{resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64}}' KeyName: !Ref TestKeyPair SubnetId: !Ref SubnetId SecurityGroupIds: - !GetAtt TestSecurityGroup.GroupId IamInstanceProfile: !Ref SSMInstanceProfile InstanceInitiatedShutdownBehavior: terminate UserData: Fn::Base64: !Sub | #!/bin/bash yum update -y yum install -y htop # Create the CPU stress test script cat > /home/ec2-user/cpu-stress-test.sh << 'EOF' #!/bin/bash echo "Starting AWS DevOpsAgent CPU Stress Test" echo "Time: $(date)" echo "Instance: $(curl -s http://169.254.169.254/latest/meta-data/instance-id)" echo "" # Get number of CPU cores CORES=$(nproc) echo "CPU Cores: $CORES" echo "" echo "Starting stress test (5 minutes)..." echo "This will generate >70% CPU usage to trigger CloudWatch alarm" echo "" # Create CPU load using yes command echo "Starting CPU load processes..." for i in $(seq 1 $CORES); do (yes > /dev/null) & CPU_PID=$! echo "Started CPU load process $i (PID: $CPU_PID)" echo $CPU_PID >> /tmp/cpu_test_pids done # Auto-cleanup after 5 minutes (sleep 300 && echo "Stopping CPU load processes..." && kill $(cat /tmp/cpu_test_pids 2>/dev/null) 2>/dev/null && rm -f /tmp/cpu_test_pids) & echo "" echo "CPU load processes started for 5 minutes" echo "Check CloudWatch for alarm trigger in 3-5 minutes" EOF chmod +x /home/ec2-user/cpu-stress-test.sh chown ec2-user:ec2-user /home/ec2-user/cpu-stress-test.sh # Create auto-shutdown script (safety mechanism) cat > /home/ec2-user/auto-shutdown.sh << 'SHUTDOWN_EOF' #!/bin/bash echo "Auto-shutdown scheduled for 2 hours from now: $(date)" sleep 7200 echo "Auto-shutdown executing at: $(date)" sudo shutdown -h now SHUTDOWN_EOF chmod +x /home/ec2-user/auto-shutdown.sh nohup /home/ec2-user/auto-shutdown.sh > /home/ec2-user/auto-shutdown.log 2>&1 & echo "AWS DevOpsAgent test setup completed at $(date)" > /home/ec2-user/setup-complete.txt Tags: - Key: Name Value: AWS-DevOpsAgent-Test-Instance - Key: Purpose Value: AWS-DevOpsAgent-Testing # CloudWatch Alarm for CPU utilization CPUAlarm: Type: AWS::CloudWatch::Alarm Properties: AlarmName: AWS-DevOpsAgent-EC2-CPU-Test AlarmDescription: AWS-DevOpsAgent beta test - EC2 CPU utilization alarm MetricName: CPUUtilization Namespace: AWS/EC2 Statistic: Average Period: 60 EvaluationPeriods: 1 Threshold: 70 ComparisonOperator: GreaterThanThreshold Dimensions: - Name: InstanceId Value: !Ref TestInstance TreatMissingData: notBreaching Outputs: InstanceId: Description: EC2 Instance ID for testing Value: !Ref TestInstance SecurityGroupId: Description: Security Group ID Value: !GetAtt TestSecurityGroup.GroupId AlarmName: Description: CloudWatch Alarm Name Value: !Ref CPUAlarm SSHCommand: Description: SSH command to connect to instance Value: !Sub 'ssh -i "AWS-DevOpsAgent-test-key.pem" ec2-user@${TestInstance.PublicDnsName}'
    3. 在 CloudFormation 控制台中,选择上传模板文件

    4. 选择 “选择文件”

    5. 选择该AWS-DevOpsAgent-ec2-test.yaml文件

    6. 选择下一步

  3. 配置堆栈

    1. 堆栈名称AWS-DevOpsAgent-EC2-Test

    2. 参数:

      1. VpcId:从下拉列表中选择现有的 VPC。

      2. SubnetId:在您选择的 VPC 内选择一个子网。对于 SSH 访问,子网必须路由到互联网网关,并且该实例必须关联公有 IPv4 地址。否则,SSHCommand输出将为空,SSH 连接将无法成功。

      3. myIP:保留为默认值0.0.0.0/0(如果需要,您可以稍后对其进行保护)

    3. 选择下一步

  4. 配置堆栈选项

    1. 保留默认值,选择 “下一步”

  5. 审核和创建

    1. 检查我承认这 AWS CloudFormation 可能会创建 IAM 资源

    2. 选择提交

  6. 等待完成

    1. 创建堆栈需要 3-5 分钟

    2. 状态将从更改CREATE_IN_PROGRESSCREATE_COMPLETE

    3. 重要:您的 EC2 实例现在是 AWS DevOpsAgent 可以跟踪的 CloudFormation 堆栈的一部分!

可选:安全 SSH 访问(仅当您计划连接到实例时)

如果你只想运行自动测试,请跳过此步骤

  1. 找到安全组

    1. 在 AWS 控制台中,转到CloudFormation并选择AWS-DevOpsAgent-EC2-Test堆栈

    2. 打开 输出” 选项卡并复制SecurityGroupId(开头为sg-)的值

    3. 前往 EC2 安全组,将 ID 粘贴到搜索栏以打开安全组

  2. 更新 SSH 规则

    1. 选择安全组 → 入站规则选项卡 → 编辑入站规则

    2. 查找 SSH 规则(端口 22)

    3. 将来源从您0.0.0.0/0的 IP 更改为:[YOUR_IP]/32

    4. 从那里获取你的 IP https://whatismyipaddress.com

    5. 选择 Save rules (保存规则)

第 2 步:等待自动测试执行

  1. 自动执行测试

    • CPU 压力测试将在实例启动 5 分钟自动开始

    • 无需手动干预-只需等待,测试完全在后台运行

  2. 监控测试

    • 实例自动启动并准备测试

    • 该脚本将运行 5 分钟并生成 > 70% 的 CPU 使用率

    • CloudWatch 警报总共应在 8-10 分钟内触发(5 分钟延迟 + 3-5 分钟警报)

  3. 可选:手动重新运行(用于其他测试):

    • 连接到您的实例:EC2 控制台 → 连接 AWS-DevOpsAgent-Test-Instance 会话管理器

    • 再次运行压力测试:./cpu-stress-test.sh

    • 非常适合多 AWS DevOpsAgent次测试响应

测试选项 B:Lambda 错误率测试

第 1 步:为 Lambda 测试部署 CloudFormation 堆栈

  1. 导航到 CloudFormation

    1. 在 AWS 控制台中,转到 CloudFormation

    2. 选择创建堆栈 使用新资源(标准)

  2. 上传模板

    1. 创建一个名为的新本地文件 AWS-DevOpsAgent-lambda-test.yaml

    2. 将此 CloudFormation 模板复制并粘贴到文件中:

      1. AWSTemplateFormatVersion: '2010-09-09' Description: 'AWS DevOpsAgent Lambda Error Test Stack' Resources: # IAM Role for Lambda function LambdaExecutionRole: Type: AWS::IAM::Role Properties: RoleName: AWS-DevOpsAgentLambdaTestRole AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole Tags: - Key: Name Value: AWS-DevOpsAgent-Lambda-Test-Role - Key: Purpose Value: AWS-DevOpsAgent-Testing # Lambda function that generates errors TestLambdaFunction: Type: AWS::Lambda::Function Properties: FunctionName: AWS-DevOpsAgent-test-lambda Runtime: python3.12 Handler: index.lambda_handler Role: !GetAtt LambdaExecutionRole.Arn Code: ZipFile: | import json import random import time from datetime import datetime def lambda_handler(event, context): print(f"AWS DevOpsAgent Test Lambda - {datetime.now()}") print(f"Event: {json.dumps(event)}") # Intentionally generate errors for testing error_scenarios = [ "Simulated database connection timeout", "Test API rate limit exceeded", "Intentional validation error for AWS DevOpsAgent testing" ] # Always throw an error for testing purposes error_message = random.choice(error_scenarios) print(f"Generating test error: {error_message}") # This will create a Lambda error that CloudWatch will detect raise Exception(f"AWS DevOpsAgent Test Error: {error_message}") Description: AWS DevOpsAgent beta test function - intentionally generates errors Timeout: 30 Tags: - Key: Name Value: AWS-DevOpsAgent-Test-Lambda - Key: Purpose Value: AWS-DevOpsAgent-Testing # CloudWatch Alarm for Lambda errors LambdaErrorAlarm: Type: AWS::CloudWatch::Alarm Properties: AlarmName: AWS-DevOpsAgent-Lambda-Error-Test AlarmDescription: AWS-DevOpsAgent beta test - Lambda error rate alarm MetricName: Errors Namespace: AWS/Lambda Statistic: Sum Period: 60 EvaluationPeriods: 1 Threshold: 0 ComparisonOperator: GreaterThanThreshold Dimensions: - Name: FunctionName Value: !Ref TestLambdaFunction TreatMissingData: notBreaching Outputs: LambdaFunctionName: Description: Lambda Function Name for testing Value: !Ref TestLambdaFunction LambdaFunctionArn: Description: Lambda Function ARN Value: !GetAtt TestLambdaFunction.Arn AlarmName: Description: CloudWatch Alarm Name Value: !Ref LambdaErrorAlarm TestCommand: Description: AWS CLI command to test the function Value: !Sub 'aws lambda invoke --function-name ${TestLambdaFunction} --payload "{\"test\":\"AWS DevOpsAgent validation\"}" response.json'
    3. 在 CloudFormation 控制台中,选择上传模板文件

    4. 选择 “选择文件”

    5. 选择该AWS-DevOpsAgent-lambda-test.yaml文件

    6. 选择下一步

  3. 配置堆栈

    1. 堆栈名称AWS-DevOpsAgent-Lambda-Test

    2. 选择下一步

  4. 配置堆栈选项

    1. 保留默认值,选择下一步

  5. 审核和创建

    1. 检查我承认这 AWS CloudFormation 可能会创建 IAM 资源

    2. 选择提交

  6. 等待完成

    1. 创建堆栈需要 2-3 分钟

    2. 状态将更改为 CREATE_COMPLETE

第 2 步:触发 Lambda 错误

  1. 导航到 Lambda 控制台

    1. 前往 AWS Lambda 控制台

    2. 找到你的函数 AWS-DevOpsAgent-test-lambda

  2. 测试该函数

    1. 选择 “测试” 选项卡

    2. 选择创建新活动

    3. 活动名称AWS-DevOpsAgent-test-event

    4. 使用这个 JSON 负载:

      1. { "test": "AWS DevOpsAgent validation", "timestamp": "2024-01-01T00:00:00Z" }
    5. 选择保存

  3. 生成错误

    1. 选择 “测试” 按钮 3 次(每次之间等待 10 秒)

    2. 每项测试都会产生故意错误

    3. CloudWatch 警报应在 2-3 分钟内触发

    4. AWS DevOpsAgent现在应该能够通过接下来要设置的操作员应用程序中的调查来检测警报。

验证 AWS DevOps 代理检测

第 1 步:健全性检查 CloudWatch 警报(可选)

此步骤用于确保上述测试现在处于警报状态。

对于 EC2 测试:

  • 在 CloudWatch 控制台中,转到警报

  • 开始压力测试后等待 3-5 分钟

  • 您的警报应显示处于警报状态

  • 如果还是 “正常”:再等待 2-3 分钟(CloudWatch 指标可能会延迟)

对于 Lambda 测试:

  • 检查AWS-DevOpsAgent-Lambda-Error-Test警报

  • 在运行测试后的 2-3 分钟内显示警报状态

第 2 步:启动 AWS DevOps 特工调查

  1. 打开你的AWS DevOps 代理 AgentSpace

  2. 选择管理员访问权限。这将在新窗口中打开 DevOps Agent Space Web 应用程序

  3. 选择 “开始调查” 按钮

  4. 填写以下表格:

    1. 调查详情:描述你想进行的调查。尽可能包括有关调查目标、要探索的领域或相关信息的详细信息。

    2. 调查起点:描述你想从哪些信息开始调查。你可以提及警报、指标、日志片段或其他任何内容,为 DevOps Agent 提供一个起点。在这种情况下,请提供您刚刚创建的警报的摘要。

    3. 事件发生日期和时间(首选 ISO 8601): YYYY-MM-DDTHH:MMZ

    4. 为你的调查命名:示例:Oncall_investigation_1:2025-10-27

    5. AWS 事件的账户 ID

    6. 事件发生的地区

    7. 优先级 - AWS DevOpsAgent 允许同时进行 2 次调查。优先级允许您定义调查的执行顺序。

  5. 选择 “调查” 以启动调查。

  6. 选择控制面板中列出的调查。您将进入调查详细信息屏幕,在那里您可以查看 DevOps 代理正在采取的详细步骤。

预期结果

EC2 测试结果:

  • 检测 EC2 CPU 警报

  • 确定根本原因:“CPU 压力测试工作负载”

  • 显示时间表:压力测试 → CPU 峰值 → 警报

  • 提供监控和扩展建议

Lambda 测试结果:

  • 检测 Lambda 错误率峰值

  • 确定根本原因:“故意的测试异常”

  • 显示时间表:函数调用 → 错误 → 警报

  • 为错误处理和监控提供建议

清理说明

清理测试 A(EC2 测试)

自动清理

  • 实例将在 2 小时后自动终止(内置于 CloudFormation 模板中)

手动清理(立即)

  1. 删除 CloudFormation 堆栈

    1. 转到 CloudFormation 控制台

    2. 选择AWS-DevOpsAgent-EC2-Test堆栈

    3. 选择 Delete(删除)

    4. 确认删除

    5. 这将自动删除所有资源:EC2 实例、安全组、密钥对和 CloudWatch 警报

清理测试 B(Lambda 测试)

  1. 删除 CloudFormation 堆栈

    1. 转到 CloudFormation 控制台

    2. 选择AWS-DevOpsAgent-Lambda-Test堆栈

    3. 选择 Delete(删除)

    4. 确认删除

    5. 这将自动删除所有资源:Lambda 函数、IAM 角色和警报 CloudWatch

问题排查

常见问题

“无法连接到 EC2 实例”

  • 检查安全组:确保 SSH(端口 22)对您的 IP 开放

  • 检查密钥权限:运行 chmod 400 AWS-DevOpsAgent-test-key.pem

  • 验证公有 IP:必须为实例分配公有 IP

  • 等待实例:确保实例处于 “正在运行” 状态

“警报未触发”

  • 等待指标: CloudWatch 指标可能需要 2-5 分钟才能出现

  • 检查 CPU 负载:SSH 到实例并运行top以验证 CPU > 70%

  • 验证压力测试:运行ps aux | grep yes以查看负载进程是否正在运行

  • 延长等待时间:首次触发警报有时最多需要 7-8 分钟

测试验证

在以下情况下,您的 AWS DevOp 代理测试成功:

技术验证

  • 调查精度:EC2 测试的结果应正确表明警报是由于 CPU 负载触发的。Lambda 测试的结果应表明这是故意失败。

  • 时间轴精度:显示的事件顺序正确

  • 推荐质量:提供切实可行的建议