我正在尝试做的事情(继续我之前提出的一个问题:如何在 powershell 中按 IAM 角色过滤 AWS 实例并获取该实例的私有 IP 地址?)是获取具有特定属性的实例的私有 IP 地址IAM 角色。而且我有一个完美运行的代码:
$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 Role 的实例的信息。因此,如果他们尝试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"
}
}
}
]
}
但是,当我get-ec2instance
在 Powershell 上运行时,我被告知我无权执行该操作。我认为这可能是因为get-ec2instance
仅适用于所有实例,但我不确定。
我将不胜感激,谢谢!