Posted in: Aws云上云维
System Operations on AWS – Managing Resources (Windows) 教程
登陆到CommandHost实例,通过AWS CLI来管理各个资源
1. 通过Tags来管理资源
1.1 登陆到CommandHost实例
1.2 找出development实例
1.2.1 打开PowerShell窗口
1.2.2 找出所有tag名为Project,值为ERPSystem
1
|
aws ec2 describe-instances --filter "Name=tag:Project,Values=ERPSystem" |
1.2.3 使用–query,减少上面命令的输出内容,只显示InstanceId
1
|
aws ec2 describe-instances --filter "Name=tag:Project,Values=ERPSystem" --query 'Reservations[*].Instances[*].InstanceId' |
1.2.4 在输出内容中包含多个选定值
1
|
aws ec2 describe-instances --filter "Name=tag:Project,Values=ERPSystem" --query 'Reservations[*].Instances[*].{ID:InstanceId,AZ:Placement.AvailabilityZone}' |
1.2.5 在输出内容中包含Project的tag值
1
|
aws ec2 describe-instances --filter "Name=tag:Project,Values=ERPSystem" --query 'Reservations[*].Instances[*].{ID:InstanceId,AZ:Placement.AvailabilityZone,Project:Tags[?Key==`Project`] | [0].Value}' |
1.2.6 在输出内容中包含Environment和Version的tag值
1
|
aws ec2 describe-instances --filter "Name=tag:Project,Values=ERPSystem" --query 'Reservations[*].Instances[*].{ID:InstanceId,AZ:Placement.AvailabilityZone,Project:Tags[?Key==`Project`] | [0].Value,Environment:Tags[?Key==`Environment`] | [0].Value,Version:Tags[?Key==`Version`] | [0].Value}' |
1.2.7 添加格外的过滤条件,只显示Environment的tag值为development的内容
1
|
aws ec2 describe-instances --filter "Name=tag:Project,Values=ERPSystem" "Name=tag:Environment,Values=development" --query 'Reservations[*].Instances[*].{ID:InstanceId,AZ:Placement.AvailabilityZone,Project:Tags[?Key==`Project`] | [0].Value,Environment:Tags[?Key==`Environment`] | [0].Value,Version:Tags[?Key==`Version`] | [0].Value}' |
1.3 修改development实例的tag名为version的值
1.3.1 运行powershell脚本来修改version值
1
|
C:\temp\change-resource-tags.ps1 |
1.3.2 检查version值是否修改
1
|
aws ec2 describe-instances --filter "Name=tag:Project,Values=ERPSystem" "Name=tag:Environment,Values=development" --query 'Reservations[*].Instances[*].{ID:InstanceId,AZ:Placement.AvailabilityZone,Project:Tags[?Key==`Project`] | [0].Value,Environment:Tags[?Key==`Environment`] | [0].Value,Version:Tags[?Key==`Version`] | [0].Value}' |
2. 通过Tag停止和启动EC2实例
2.2.1 停止所有Project名为ERPSystem,Environment为development的实例
1
|
c:\temp\stopinator.ps1 -tags "Project=ERPSystem;Environment=development" |
2.2.2 启动所有Project名为ERPSystem,Environment为development的实例
1
|
c:\temp\stopinator.ps1 -tags "Project=ERPSystem;Environment=development" -start |
3. Terminate不符合要求的实例
3.1.1 删除Environment标签
3.1.4 运行terminate-instances.ps1脚本
1
|
c:\temp\terminate-instances.ps1 -region us-west-2 -subnetid subnet-d67568a1 |