powershell - 用于通过关联的 IAM 角色限制实例的 AWS IAM 策略

标签 powershell amazon-web-services amazon-iam aws-powershell

我想做的(继续我之前问的问题: How can I filter AWS Instances by IAM role in powershell and get the private ip address of that instance? )是获取具有特定 IAM 角色的实例的私有(private) IP 地址。我有一个完美运行的代码:

$filter = New-Object Amazon.EC2.Model.Filter -Property @{Name = "iam-instance-profile.arn"; Value = "arn:aws:iam::123456789012:instance-profile/TestRole"} 
$ec2 = @(Get-EC2Instance -Filter $filter)
$ec2instances = $ec2.instances 
$ipaddress = $ec2instances.privateipaddress

但是,现在我不想在代码中进行过滤,而是想创建一个 IAM 策略,限制用户只能获取有关具有特定 IAM 角色的实例的信息。因此,如果他们尝试 get-ec2instance(例如),它应该只返回相关实例的信息,而不是帐户中的所有实例。

这是我的 IAM 政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "ec2:DescribeInstances"
            ],
            "Effect": "Allow",
            "Resource": [
                "*"
            ],
            "Condition": {
                "ArnEquals": {
                    "ec2:InstanceProfile": "arn:aws:iam::12356789102:instance-profile/TestRole"
                }
            }
        }
    ]
}

但是,当我在 Powershell 上运行 get-ec2instance 时,我被告知我无权执行该操作。我认为这可能是因为 get-ec2instance 仅适用于所有实例,但我不确定。

非常感谢您的帮助,谢谢!

最佳答案

到目前为止,您还没有任何选项可以限制 IAM 用户查看特定的 EC2 实例。

仅存在一个 API 调用 ec2-describe-instances,这表明需要拥有所有实例的所有权限或没有权限。

关于powershell - 用于通过关联的 IAM 角色限制实例的 AWS IAM 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35216910/

相关文章:

Powershell 等待进程

amazon-web-services - 用于更新现有启动模板的 Terraform 配置

django - Elastic Beanstalk 找不到任何平台

powershell - Powershell-使用文件作为参数创建快捷方式

powershell - 我无法使用 Powershell StartsWith 函数

javascript - 当使用 aws amplify on react 调用 api 网关时,我该如何获取状态码?

linux - 如何从亚马逊EC2实例下载网站文件和数据库?

mongodb - AWS EC2 与 MongoDB Atlas 的连接失败,找不到用户

amazon-web-services - 如何在 Cloudformation 中编写基于资源的策略

powershell - 重命名文件夹,如果它存在于 Powershell 中,则与现有文件夹合并