amazon-web-services - 如何仅列出给定 CloudFormation 堆栈中的 EC2 实例?

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

我可以在下面的命令中使用什么来满足以下条件的 $QUERY:

aws ec2 describe-instances --query $QUERY
  1. 仅打印 aws:cloudformation:stack-name 标记等于 test-stack 的实例。
  2. 仅打印每个实例的 InstanceId 属性。
  3. 不诉诸管道、for 循环或其他 shell 花哨的东西。

最佳答案

这里需要使用一些参数:

查询

--query ( docs ) 仅检索 InstanceId

按堆栈名称标签过滤

--filter ( docs ) 用于排除未用堆栈名称标记的实例

  • tag-key - The key of a tag assigned to the resource. This filter is independent of the tag-value filter. For example, if you use both the filter "tag-key=Purpose" and the filter "tag-value=X", you get any resources assigned both the tag key Purpose (regardless of what the tag's value is), and the tag value X (regardless of what the tag's key is). If you want to list only resources where Purpose is X, see the tag :key =value filter.

  • tag-value - The value of a tag assigned to the resource. This filter is independent of the tag-key filter.

格式

--output ( docs ) 仅返回您查询的值(因此没有引号或 json/table 绒毛)

The text format organizes the AWS CLI's output into tab-delimited lines. It works well with traditional Unix text tools such as grep, sed, and awk, as well as Windows PowerShell.

像这样使用这些参数:

aws ec2 describe-instances \                                                     
    --query "Reservations[*].Instances[*].InstanceId[]" \                        
    --filters "Name=tag-key,Values=aws:cloudformation:stack-name" "Name=tag-value,Values=test-stack" \
    --output=text

返回:

i-sd64f52a  i-das5d64a  i-sad56d4

关于amazon-web-services - 如何仅列出给定 CloudFormation 堆栈中的 EC2 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26707073/

相关文章:

java - 对 AWS EC2/EBS 资源使用较长的 ID

aws-cloudformation - StreamEnabled 表属性导致 Serverless 失败

amazon-s3 - 如何创建 s3 存储桶、具有 S3 完全访问权限的 IAM 用户以及如何将用户凭据传递到我的应用程序?

aws-cloudformation - 如何通过 s3 对象创建事件上的 CloudWatch 事件触发器将存储桶/键名称传递给 Fargate 作业?

.net - 如何使用 Mono 为 AWS Lambda 函数创建 .NET 函数代码 zip 文件?

java - 如何从本地计算机使用 IAM 角色访问 S3

python - 使用 Amazon Linux 的 Python 2.6.9 上的 mysql.connector 错误

amazon-web-services - Terraform AWS CloudTrail 配置失败

c# - ServiceStack.Aws.DynamoDb : Is there async APIs?

amazon-web-services - AWS elastic beanstalk eb init 命令孟买区域不可用