通过Boto关联的IAM角色过滤ec2实例

编程入门 行业动态 更新时间:2024-10-26 14:30:35
本文介绍了通过Boto关联的IAM角色过滤ec2实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在AWS上有一些与同一IAM角色相关联的实例.我正在寻找编写返回这些实例的代码. 基于此文档: docs.aws.amazon/AWSEC2/latest/APIReference/API_DescribeInstances.html ,我看到有可用的过滤器iam-instance-profile.arn.我只是不确定我将如何使用它,或者那是否是我应该使用的.

I have a few instances on AWS that are associated with the same IAM Role. I'm looking to write a code that returns these instances. Based from this document: docs.aws.amazon/AWSEC2/latest/APIReference/API_DescribeInstances.html, I see that there is an available filter iam-instance-profile.arn. I'm just not sure how I would go about using that or if that is what I should be using.

这是一个实例,其中实例通过标签过滤.

This is an example where instances are filtered by tags.

conn = boto.ec2.connect_to_region('ap-southeast-1') reservations = conn.get_all_instances(filters={"tag:Name": "my-instance-1"}); for reservation in reservations: instance = reservation.instances[0]

除了使用IAM角色作为过滤器外,我想做类似的事情.

I'd like to do something similar except with an IAM Role as the filter.

另一件事-conn.get_all_instances(filters={"tag:Name": "my-instance-1"});上面的示例返回保留.我想获取实例,而不必通过预订获取实例.例如:conn.get_only_instances(instance_ids=['i-cc186913'])返回实例. 拥有过滤器(IAM角色)并仅返回实例(不通过预订获取实例)的最佳方法是什么?

Another thing - the example above conn.get_all_instances(filters={"tag:Name": "my-instance-1"});returns reservations. I'd like to get the instances without having to get them through reservations. For example: conn.get_only_instances(instance_ids=['i-cc186913']) returns me the instance. What is the best way where I could have a filter (IAM Role) and return ONLY instances (not getting them through reservations)?

推荐答案

为该角色传递实例配置文件ARN(可以从IAM仪表板获取或构造该角色).示例:

Pass your instance profile ARN for that role (which you can get from IAM dashboard or you can construct it). Example:

conn.get_only_instances(filters={"iam-instance-profile.arn": "arn:aws:iam::<your-account-number>:instance-profile/<your-role-name>"})

它将返回实例列表,您可以遍历该实例.

It will return a list of instance and you can loop through it.

>>> conn.get_only_instances(filters={"iam-instance-profile.arn":"arn:aws:iam::123456781221:instance-profile/stackoverflowRole"}) [Instance:i-8ba223ab]

更多推荐

通过Boto关联的IAM角色过滤ec2实例

本文发布于:2023-08-07 14:10:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   角色   Boto   IAM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!