【问题标题】:How to get recent Amazon Linux image in all regions?如何获取所有地区最近的 Amazon Linux 镜像?
【发布时间】:2018-07-11 13:55:41
【问题描述】:
#!/bin/bash
aws ec2 describe-images \
--owners self amazon \
--filters "Name=root-device-type,Values=ebs" \
--query 'Images[*].[ImageId,CreationDate]' \
| sort -k2 -r \
| head -n1

我编写了一个脚本来使用 AWS CLI 获取最新的 Amazon Linux 映像。当我运行此脚本时,我会在我的默认区域 eu-west-1 中获得最新的 Amazon Linux 映像。如何修改代码以获取所有区域的最新图像。

【问题讨论】:

    标签: amazon-web-services amazon-ec2 aws-cli


    【解决方案1】:

    --region <region_name> 添加到您的 CLI 命令中。

    类似的东西

    aws ec2 describe-images --region eu-west-2 \
    --owners self amazon \
    --filters "Name=root-device-type,Values=ebs" \
    --query 'Images[*].[ImageId,CreationDate]' \
    | sort -k2 -r \
    | head -n1
    

    您可以使用aws ec2 describe-regions 命令获取区域列表并针对每个区域运行查询,而不是硬编码区域名称。

    https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-regions.html

    执行aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text,输出为

    ap-south-1
    eu-west-3
    eu-west-2
    eu-west-1
    ap-northeast-3
    ap-northeast-2
    ap-northeast-1
    sa-east-1
    ca-central-1
    ap-southeast-1
    ap-southeast-2
    eu-central-1
    us-east-1
    us-east-2
    us-west-1
    us-west-2
    

    现在遍历每个区域并执行describe-images CLI 命令。

    【讨论】:

    • 谢谢,但在提供多个区域时出现错误例如:--region eu-west-1,us-east-1 "Invalid endpoint"
    • 你不能给多个区域。您必须按区域执行命令。
    • 谢谢!我试试看
    猜你喜欢
    • 2021-10-10
    • 1970-01-01
    • 2011-09-03
    • 2020-06-06
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多