【问题标题】:IAM policy for EBS volume with EC2 instance具有 EC2 实例的 EBS 卷的 IAM 策略
【发布时间】: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


    【解决方案1】:

    DescribeVolumesdoes not supportaws:ResourceTag/${TagKey} 条件,也不是其他任何条件。

    【讨论】:

      【解决方案2】:

      大多数针对许多资源的描述/列表类型操作与条件逻辑不兼容。 DescribeVolumes 不适用于条件,因此将其拆分为不同的语句。

      {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": [
                      "ec2:AttachVolume",
                      "ec2:DetachVolume"
                  ],
                  "Resource": [
                      "arn:aws:ec2:*:*:instance/*",
                      "arn:aws:ec2:*:*:volume/*"
                  ],
                  "Condition": {
                      "StringEquals": {
                        "ec2:ResourceTag/app": "web"
                      }
                  }
              },
              {
                  "Effect": "Allow",
                  "Action": "ec2:DescribeVolumes",
                  "Resource": "*"
              }
          ]
      }
      

      【讨论】:

        猜你喜欢
        • 2018-08-12
        • 2020-09-10
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 1970-01-01
        • 2019-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多