AWS CLI

Aws Cli

AWS CLI
Page content

AWS CLI 配置

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

AWS CLI 常用命令

# 通过metadata在某实例中获取自己的instanceid,nic-id等信息
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
instanceId=`curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id`

# cli获取网卡id
[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)}'
eni-0afdd844399fb33fc

# cli获取网卡tag
[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}}'

[root@ip-172-29-97-112 aws]# aws ec2 describe-instances --instance-ids i-0642534a5a9a4a312

# 查看网卡情况
[root@ip-172-29-97-112 aws]# aws ec2 describe-network-interfaces --network-interface-id eni-0afdd844399fb33fc

# 返回某实例的ami-id,instance-id,Tags,eni-id
[root@ip-172-29-97-112 aws]# aws ec2 describe-instances --instance-id i-0642534a5a9a4a312 --query 'Reservations[*].Instances[*].[ImageId,InstanceId,Tags[*], NetworkInterfaces[*].NetworkInterfaceId]'
[
    [
        [
            "ami-086e81277c25719ad",
            "i-0642534a5a9a4a312",
            [
                {
                    "Value": "suo.li,ming.tao",
                    "Key": "Tel"
                },
                {
                    "Value": "cake-console",
                    "Key": "Name"
                }
            ],
            [
                "eni-0afdd844399fb33fc"
            ]
        ]
    ]
]

# 以表格的形式输出
[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
--------------------------------------------------
|                DescribeInstances               |
+------------------------+-----------------------+
|         ImageId        |      InstanceId       |
+------------------------+-----------------------+
|  ami-086e81277c25719ad |  i-0642534a5a9a4a312  |
+------------------------+-----------------------+
||              NetworkInterfaceId              ||
|+----------------------------------------------+|
||  eni-0afdd844399fb33fc                       ||
|+----------------------------------------------+|
||                     Tags                     ||
|+------------+---------------------------------+|
||     Key    |              Value              ||
|+------------+---------------------------------+|
||  Tel       |  suo.li,ming.tao                ||
||  Name      |  cake-console                   ||
|+------------+---------------------------------+|

# 查看所有停止的机器
# 80:stopped
# filter Name: 
  #instance-state-code - 0 (pending), 16 (running), 32 (shutting-down), 48 (terminated), 64 (stopping), and 80 (stopped).
  #instance-state-name - (pending | running | shutting-down | terminated | stopping | stopped ).
~# aws ec2 describe-instance-status --filter Name=instance-state-code,Values=80 --include-all-instances  --output table

# 查看某种类型实例的具体参数
~# aws ec2 describe-instance-types --filter Name=instance-type,Values=c4.xlarge,c4.2xlarge  --output yaml

# 查看某种或某几种类型的实例
~# aws ec2 describe-instances --filter Name=instance-type,Values=c4.xlarge,c4.2xlarge --query 'Reservations[*].Instances[*].{InstanceId:InstanceId}'  --output yaml

# 获取lambda函数信息
~# aws lambda get-function --function-name initCakeWeb

# log groups
~# aws logs describe-log-groups --log-group-name-prefix /aws/lambda/initCakeWe

# log streams
~# aws logs describe-log-streams --log-group-name /aws/lambda/initCakeWeb --log-stream-name-prefix 2020

# EC2 AutoScaling 
# asg-name=jelly-web
# asg-name=jelly-web-long
~# 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
# 查看lifecycle hook
~# aws autoscaling describe-lifecycle-hooks --auto-scaling-group-name cake-autoscale-test
# 查看autoScaling instances
~# aws autoscaling describe-auto-scaling-instances --query 'AutoScalingInstances[*].{AutoScalingGroupName:AutoScalingGroupName,InstanceId:InstanceId,InstanceType:InstanceType,LifecycleState:LifecycleState}' --max-items 20
~# aws autoscaling describe-auto-scaling-instances --query 'AutoScalingInstances[*].{AutoScalingGroupName:AutoScalingGroupName,InstanceId:InstanceId,InstanceType:InstanceType,LifecycleState:LifecycleState}' --output table
# 查看autoScaling group关联的target group
~# aws autoscaling describe-load-balancer-target-groups --auto-scaling-group-name