AWS CLI

AWS CLI 配置

  • ~/.aws/configure
1[default]
2#region = cn-north-1
3region = cn-northwest-1
4output = json
  • ~/.aws/credentials
1[default]
2aws_access_key_id =  
3aws_secret_access_key =  
4aws_session_token =  

AWS CLI 常用命令

 1# 通过metadata在某实例中获取自己的instanceid,nic-id等信息
 2TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
 3instanceId=`curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id`
 4
 5# cli获取网卡id
 6[root@ip-172-29-97-112 aws]# aws ec2 describe-instances --instance-id $instanceId --query 'Reservations[*].Instances[*].[NetworkInterfaces[*].NetworkInterfaceId]' | awk -F'"' '/[a-zA-Z]/{print $(NF-1)}'
 7eni-0afdd844399fb33fc
 8
 9# cli获取网卡tag
10[root@ip-172-29-97-112 aws]# aws ec2 describe-network-interfaces --network-interface-id eni-0afdd844399fb33fc --query 'NetworkInterfaces[*].TagSet' | awk -F'"|:' '/[a-zA-Z]/{if($2 == "Key"){printf"Key is %s\n", $5}else{printf"Value is %s\n", $5}}'
11
12[root@ip-172-29-97-112 aws]# aws ec2 describe-instances --instance-ids i-0642534a5a9a4a312
13
14# 查看网卡情况
15[root@ip-172-29-97-112 aws]# aws ec2 describe-network-interfaces --network-interface-id eni-0afdd844399fb33fc
16
17# 返回某实例的ami-id,instance-id,Tags,eni-id
18[root@ip-172-29-97-112 aws]# aws ec2 describe-instances --instance-id i-0642534a5a9a4a312 --query 'Reservations[*].Instances[*].[ImageId,InstanceId,Tags[*], NetworkInterfaces[*].NetworkInterfaceId]'
19[
20    [
21        [
22            "ami-086e81277c25719ad",
23            "i-0642534a5a9a4a312",
24            [
25                {
26                    "Value": "suo.li,ming.tao",
27                    "Key": "Tel"
28                },
29                {
30                    "Value": "cake-console",
31                    "Key": "Name"
32                }
33            ],
34            [
35                "eni-0afdd844399fb33fc"
36            ]
37        ]
38    ]
39]
40
41# 以表格的形式输出
42[root@ip-172-29-97-112 aws]# aws ec2 describe-instances --instance-id i-0642534a5a9a4a312 --query 'Reservations[*].Instances[*].{ImageId:ImageId,InstanceId:InstanceId,Tags:Tags[*], NetworkInterfaceId:NetworkInterfaces[*].NetworkInterfaceId}' --output table
43--------------------------------------------------
44|                DescribeInstances               |
45+------------------------+-----------------------+
46|         ImageId        |      InstanceId       |
47+------------------------+-----------------------+
48|  ami-086e81277c25719ad |  i-0642534a5a9a4a312  |
49+------------------------+-----------------------+
50||              NetworkInterfaceId              ||
51|+----------------------------------------------+|
52||  eni-0afdd844399fb33fc                       ||
53|+----------------------------------------------+|
54||                     Tags                     ||
55|+------------+---------------------------------+|
56||     Key    |              Value              ||
57|+------------+---------------------------------+|
58||  Tel       |  suo.li,ming.tao                ||
59||  Name      |  cake-console                   ||
60|+------------+---------------------------------+|
61
62# 查看所有停止的机器
63# 80:stopped
64# filter Name: 
65  #instance-state-code - 0 (pending), 16 (running), 32 (shutting-down), 48 (terminated), 64 (stopping), and 80 (stopped).
66  #instance-state-name - (pending | running | shutting-down | terminated | stopping | stopped ).
67~# aws ec2 describe-instance-status --filter Name=instance-state-code,Values=80 --include-all-instances  --output table
68
69# 查看某种类型实例的具体参数
70~# aws ec2 describe-instance-types --filter Name=instance-type,Values=c4.xlarge,c4.2xlarge  --output yaml
71
72# 查看某种或某几种类型的实例
73~# aws ec2 describe-instances --filter Name=instance-type,Values=c4.xlarge,c4.2xlarge --query 'Reservations[*].Instances[*].{InstanceId:InstanceId}'  --output yaml
74
75# 获取lambda函数信息
76~# aws lambda get-function --function-name initCakeWeb
77
78# log groups
79~# aws logs describe-log-groups --log-group-name-prefix /aws/lambda/initCakeWe
80
81# log streams
82~# aws logs describe-log-streams --log-group-name /aws/lambda/initCakeWeb --log-stream-name-prefix 2020
83
84# EC2 AutoScaling 
85# asg-name=jelly-web
86# asg-name=jelly-web-long
87~# aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name ${asg-name}  --query 'AutoScalingGroups[*].{AutoScalingGroupName:AutoScalingGroupName,LaunchTemplate:LaunchTemplate,MinSize:MinSize,MaxSize:MaxSize,DesiredCapacity:DesiredCapacity,LoadBalancerNames:LoadBalancerNames,TerminationPolicies:TerminationPolicies,Tags:Tags}' --output table
88# 查看lifecycle hook
89~# aws autoscaling describe-lifecycle-hooks --auto-scaling-group-name cake-autoscale-test
90# 查看autoScaling instances
91~# aws autoscaling describe-auto-scaling-instances --query 'AutoScalingInstances[*].{AutoScalingGroupName:AutoScalingGroupName,InstanceId:InstanceId,InstanceType:InstanceType,LifecycleState:LifecycleState}' --max-items 20
92~# aws autoscaling describe-auto-scaling-instances --query 'AutoScalingInstances[*].{AutoScalingGroupName:AutoScalingGroupName,InstanceId:InstanceId,InstanceType:InstanceType,LifecycleState:LifecycleState}' --output table
93# 查看autoScaling group关联的target group
94~# aws autoscaling describe-load-balancer-target-groups --auto-scaling-group-name

Apt Update Error
Move Nic to Container