我的目标是使用带boto python API的lambda读取AWS ec2实例的详细信息。具体来说,我想读出ec2实例的属性launch_time。
执行时,出现以下错误:
'EC2' object has no attribute 'Instance': AttributeError
示例:
Python代码:
import boto3
ec2 = boto3.client('ec2')
instance = ec2.Instance('id')
launch_time = instance.launch_time
政策:
{
"Effect": "Allow",
"Action": [
"ec2:Instance"
],
"Resource": [
"arn:aws:ec2:${var.region}:${var.account}:instance/*"
]
}
答案 0 :(得分:0)
根据文档
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#instance
必须初始化资源而不是客户端。
资源
printf()
代替客户
ec2 = boto3.resource('ec2')
其他检查政策
ec2 = boto3.client('ec2')
DescribeInstances而不是Instances。还有通配符资源,DescribeInstances不支持特定的信息。
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-policies-ec2-console.html#ex-read-only
Amazon EC2 ec2:Describe * API操作不支持资源级 权限,因此您无法控制用户使用哪些资源 可以在控制台中查看。因此,*通配符在 以上语句的资源元素。有关更多信息 您可以将哪些ARN与Amazon EC2 API操作一起使用,请参阅 Amazon EC2 API操作支持的资源级别权限。