AWS EC2 CLI

最後更新: 2022-05-05

目錄

 


EC2 CLI

 

查看現有 instances

aws ec2 describe-instances [--instance-ids i-ID]      # All info.

filters & query

Server-side filtering (--filters)

aws ec2 describe-instances \
    --filters Name=instance-type,Values=m5.large

aws ec2 describe-instances \
    --filters "Name=tag-key,Values=Owner"

aws ec2 describe-instances \
    --filters "Name=tag-value,Values=my-team"

Client-side filtering (--query)

# 所有 InstanceId

aws ec2 describe-instances \
    --query Reservations[*].Instances[*].[InstanceId] --output text

aws ec2 describe-instances \
    --query "Reservations[*].Instances[*].[InstanceId, State.Name]" --o text

aws ec2 describe-instances \
    --query 'Reservations[*].Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Tags:Tags[*]}'

其中一個 Tag

Tags[?Key==`Name`].Value

i.e.

Attachments[?State==`attached`]
Volumes[?Size < `20`]

Start instance

aws ec2 start-instances --instance-ids <value>

 * Every time you start your instance, Amazon EC2 charges a one-minute minimum for instance usage

Stop instance

aws ec2 stop-instances --instance-ids <value> [--force]

--force

 Forces the instances to stop. The instances do not have an opportunity to flush file system caches

 * You are charged for Amazon EBS volume usage

Reboot

aws reboot-instances --instance-ids <value>

 * When you reboot an instance, it keeps its public DNS name (IPv4),
    private and public IPv4 address, and  any data on its instance store volumes.

 * Rebooting an instance doesn't start a new instance billing period
    (with a minimum one-minute charge), unlike stopping and starting your instance.

Doc

http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html

 


describe-images

 

查看 ubuntu 的 AMI owner id

https://ubuntu.com/server/docs/cloud-images/amazon-ec2

099720109477

常用 owner id

  • ubuntu 099720109477
  •  

找出 ubuntu 22.04 的 AMI

# ubuntu

aws ec2 describe-images --owners 099720109477 \
    --filter "Name=name,Values=ubuntu/images/*22.04*" "Name=architecture,Values=x86_64"

# ubuntu-pro (要收 Software 錢)

aws ec2 describe-images --owners 099720109477  \
    --filter "Name=name,Values=ubuntu-pro*" "Name=architecture,Values=x86_64"

Sort the output by CreationDate <-  Built-in Functions

原因: 來身 output 並沒有 sorting

"Canonical, Ubuntu, 22.04 LTS, amd64 jammy image build on 2022-12-06"
"Canonical, Ubuntu, 22.04 LTS, amd64 jammy image build on 2023-03-03"
...

--query "sort_by(Images, &CreationDate)[-1].[ImageId,CreationDate,Description]"

--query = JSON-based client-side filtering

{
    "Images": [
        {..},
        {..}
    ]
}

Built-in Functions

sort_by(array elements, expression->number|expression->string expr)

i.e.

sort_by(Images, &CreationDate)

expression (denoted by &expression)

A reference to that expression is provided to the function being called.

i.e.

aws ec2 describe-images --owner amazon \
--filters 'Name=name,Values=amzn2-ami-ecs-hvm-*' \
"Name=architecture,Values=x86_64" > ami.json

jq '.Images| sort_by(.CreationDate) | .[-1] | .CreationDate, .ImageId, .ImageLocation' ami.json

 


EIP

 

# 獲得一粒 EIP

aws ec2 allocate-address

{
    "PublicIp": "x.x.x.x",
    "AllocationId": "eipalloc-XXX",
    "PublicIpv4Pool": "amazon",
    "NetworkBorderGroup": "ap-east-1",
    "Domain": "vpc"
}

# 查看 EIP 資料

aws ec2 describe-addresses

{
    "Addresses": [
        {
            "PublicIp": "x.x.x.x",
            "AllocationId": "eipalloc-XXX",
            "Domain": "vpc",
            "PublicIpv4Pool": "amazon",
            "NetworkBorderGroup": "ap-east-1"
        }
    ]
}

# 將 EIP 合到 Instance

aws ec2 associate-address --instance-id i-XXX --allocation-id eipalloc-XXX

# 將 EIP 合到 NIC

aws ec2 associate-address --allocation-id eipalloc-XXX --network-interface-id eni-XXX

# 從 Instance 取出 EIP

aws ec2 disassociate-address --association-id XXX

# 釋放 EIP

aws ec2 release-address --allocation-id eipalloc-XXX