AWS
aws cli 명령 출력 제어 - Autoscaling 조회 using dictionary notation, 필터링...
woonizzooni
2020. 11. 9. 18:45
aws 명령의 default output format은 json임.
aws명령으로 이용하려는 서비스별 조회/생성/조회/업데이트/삭제 등의 동작을 할 수 있는데,
이중 'describe'명령 결과를 내 기호에 맞는 데이터와 형식으로 출력시켜보자.
o 기본 출력 예 (json)
$ aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names 이름-***
{
"AutoScalingGroups": [
{
"AutoScalingGroupName": "이름-***...",
"AutoScalingGroupARN": "arn:aws::........",
...
"MinSize": 2,
"MaxSize": 6,
"DesiredCapacity": 2,
"DefaultCooldown": 300,
"AvailabilityZones": [
"ap-northeast-2a",
"ap-northeast-2c"
],
"Instances": [
{
"InstanceId": "i-111111",
"InstanceType": "g4dn.xlarge",
"AvailabilityZone": "ap-northeast-2c",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
...
},
"ProtectedFromScaleIn": true
...
},
{
"InstanceId": "i-222222",
"InstanceType": "g4dn.xlarge",
"AvailabilityZone": "ap-northeast-2c",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
...
},
"ProtectedFromScaleIn": true
...
},
...
],
...
"CreatedTime": "2020-11-04T05:59:25.738Z",
...
o 기본 출력에서 'jq'로 원하는 데이터만 출력
$ aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names 이름-*** \
jq '.AutoScalingGroups[0].Instances'
[
{
"InstanceId": "i-111111",
"InstanceType": "g4dn.xlarge",
"AvailabilityZone": "ap-northeast-2c",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
...
},
"ProtectedFromScaleIn": true
},
{
"InstanceId": "i-222222",
"InstanceType": "g4dn.xlarge",
"AvailabilityZone": "ap-northeast-2c",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
...
},
"ProtectedFromScaleIn": true
}
]
o table 출력으로 변경
$ aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names 이름-*** --output table
------------------------------------------------------------
| DescribeAutoScalingGroups |
+----------------------------------------------------------+
|| AutoScalingGroup s ||
|+---------------------------+----------------------------+|
|| AutoScalingGroupARN | arn:aws::........ ||
|| AutoScalingGroupName | 이름-***... ||
|| CreatedTime | YYYY-mm-ddTHH:MM:SS.sssZ ||
|| DefaultCooldown | 300 ||
|| DesiredCapacity | 2 ||
|| MaxSize | 6 ||
|| MinSize | 2 ||
...
|+---------------------------+----------------------------+|
||| AvailabilityZones |||
||+------------------------------------------------------+||
||| ap-northeast-2a |||
||| ap-northeast-2c |||
||+------------------------------------------------------+||
||| Instances |||
||+--------------------------+---------------------------+||
||| AvailabilityZone | ap-northeast-2c |||
||| HealthStatus | Healthy |||
||| InstanceId | i-111111 |||
||| InstanceType | g4dn.xlarge |||
||| LifecycleState | InService |||
||| ProtectedFromScaleIn | True |||
||+--------------------------+---------------------------+||
|||| LaunchTemplate ||||
....
|||+----------------------------------------------------+|||
||| Instances |||
||+--------------------------+---------------------------+||
||| AvailabilityZone | ap-northeast-2c |||
||| HealthStatus | Healthy |||
||| InstanceId | i-222222 |||
||| InstanceType | g4dn.xlarge |||
||| LifecycleState | InService |||
||| ProtectedFromScaleIn | True |||
||+--------------------------+---------------------------+||
...
o table출력에서 원하는 데이터와 컬럼으로 필터링 / 재정의(?) (dictionary notation 활용)
$ aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names 이름-*** \
--output table \
--query 'AutoScalingGroups[*].Instances[*].{ID:InstanceId, STATUS:HealthStatus, PROTECTED:ProtectedFromScaleIn}';
----------------------------------------
| DescribeAutoScalingGroups |
+-------------+-------------+----------+
| ID | PROTECTED | STATUS |
+-------------+-------------+----------+
| i-111111 | True | Healthy |
| i-222222 | True | Healthy |
+-------------+-------------+----------+
[참고]
docs.aws.amazon.com/cli/latest/userguide/cli-usage-output.html
docs.aws.amazon.com/cli/latest/userguide/cli-usage-output.html#cli-usage-output-filter