【发布时间】:2021-02-15 15:05:42
【问题描述】:
我正在尝试创建一个 IAM 角色/策略,以使我的 EC2 实例能够列出和附加 EBS 卷(通过调用 aws cli 的脚本)。我希望此策略仅允许列出/附加具有特定标签的 EBS 卷。
我注意到,当我在下面的策略中设置Resources: "*" 而没有设置Conditions 时,脚本能够列出/附加卷。
但是,一旦我介绍了下面的策略,AWS cli 就会引发以下错误:
./aws ec2 describe-volumes
An error occurred (UnauthorizedOperation) when calling the DescribeVolumes operation: You are not authorized to perform this operation.
这是我目前在 terraform 中定义的 IAM 政策:
resource "aws_iam_role" "web_role" {
name = "web_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_instance_profile" "web_profile" {
name = "web_profile"
role = aws_iam_role.web_role.name
}
resource "aws_iam_role_policy" "web_disk_policy" {
name = "web_disk_policy"
role = aws_iam_role.web_role.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:AttachVolume",
"ec2:DetachVolume",
"ec2:DescribeVolumes"
],
"Resource": [
"arn:aws:ec2:*:*:instance/*",
"arn:aws:ec2:*:*:volume/*"
],
"Condition": {
"StringEquals": {
"ec2:ResourceTag/app": "web"
}
}
}
]
}
EOF
}
我的 EC2 实例是使用以下内容创建的:
resource "aws_instance" "web_vm" {
...
iam_instance_profile = aws_iam_instance_profile.web_profile.name
...
tags = {
app = "web"
}
}
和创建的磁盘:
resource "aws_ebs_volume" "ebs-volume-1" {
availability_zone = "us-west-2a"
size = 10
tags = {
app = "web"
}
}
【问题讨论】:
标签: amazon-web-services amazon-ec2 terraform amazon-iam