跳至内容

如何创建 IAM 策略以显式授予在带有标签的指定 VPC 中创建和管理 EC2 实例的权限?

2 分钟阅读
0

我想创建一个 AWS Identity and Access Management (IAM) 策略,显式授予 IAM 身份在虚拟私有云 (VPC) 中管理 Amazon Elastic Compute Cloud (Amazon EC2) 实例的权限。

简短描述

IAM 策略必须限制 IAM 身份管理带有标签的 VPC 中实例的权限。

Amazon EC2 提供部分资源级权限,但您可以使用多种服务级别操作、资源和条件。要控制对 EC2 资源的访问权限,请使用资源标签

解决方法

创建策略,授予在 VPC 中启动实例的权限,但限制 IAM 身份管理实例的权限。要限制实例的管理,请使用 ec2:ResourceTag 策略条件。

创建托管策略以附加到启动您的实例的 IAM 身份

完成以下步骤:

  1. 打开 IAM 控制台

  2. 选择 Policies(策略),然后选择 Create policy(创建策略)。

  3. 选择 JSON 选项卡,然后输入以下自定义策略:

    {  "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "GrantIAMPassRoleOnlyForEC2",
          "Action": [
            "iam:PassRole"
          ],
          "Effect": "Allow",
          "Resource": [
            "arn:aws:iam::account_number:role/role_name",
            "arn:aws:iam::account_number:role/role_nameE"
          ],
          "Condition": {
            "StringEquals": {
              "iam:PassedToService": "ec2.amazonaws.com"
            }
          }
        },
        {
          "Sid": "ReadOnlyEC2WithNonResource",
          "Action": [
            "ec2:Describe*",
            "iam:ListInstanceProfiles"
          ],
          "Effect": "Allow",
          "Resource": "*"
        },
        {
          "Sid": "ModifyingEC2WithNonResource",
          "Action": [
            "ec2:CreateKeyPair",
            "ec2:CreateSecurityGroup"
          ],
          "Effect": "Allow",
          "Resource": "*"
        },
        {
          "Sid": "RunInstancesWithTagRestrictions",
          "Effect": "Allow",
          "Action": "ec2:RunInstances",
          "Resource": [
            "arn:aws:ec2:region:a:instance/*",
            "arn:aws:ec2:region:account_number:volume/*"
          ],
          "Condition": {
            "StringEquals": {
              "aws:RequestTag/TAG-KEY": "tag_value"
            }
          }
        },
        {
          "Sid": "RemainingRunInstancePermissionsNonResource",
          "Effect": "Allow",
          "Action": "ec2:RunInstances",
          "Resource": [
            "arn:aws:ec2:REGION::image/*",
            "arn:aws:ec2:REGION::snapshot/*",
            "arn:aws:ec2:region:account_number:network-interface/*",
            "arn:aws:ec2:region:account_number:key-pair/*",
            "arn:aws:ec2:region:account_number:security-group/*"
          ]
        },
        {
          "Sid": "EC2RunInstancesVpcSubnet",
          "Effect": "Allow",
          "Action": "ec2:RunInstances",
          "Resource": "arn:aws:ec2:region:account_number:subnet/*",
          "Condition": {
            "StringEquals": {
              "ec2:Vpc": "arn:aws:ec2:region:account_number:vpc/VPC-ID"
            }
          }
        },
        {
          "Sid": "EC2VpcNonResourceSpecificActions",
          "Effect": "Allow",
          "Action": [
            "ec2:DeleteNetworkAcl",
            "ec2:DeleteNetworkAclEntry",
            "ec2:DeleteRoute",
            "ec2:DeleteRouteTable",
            "ec2:AuthorizeSecurityGroupEgress",
            "ec2:AuthorizeSecurityGroupIngress",
            "ec2:RevokeSecurityGroupEgress",
            "ec2:RevokeSecurityGroupIngress",
            "ec2:DeleteSecurityGroup",
            "ec2:CreateNetworkInterfacePermission",
            "ec2:CreateRoute",
            "ec2:UpdateSecurityGroupRuleDescriptionsEgress",
            "ec2:UpdateSecurityGroupRuleDescriptionsIngress"
          ],
          "Resource": "*",
          "Condition": {
            "StringEquals": {
              "ec2:Vpc": "arn:aws:ec2:region:account_number:vpc/VPCID"
            }
          }
        },
        {
          "Sid": "AllowInstanceActionsTagBased",
          "Effect": "Allow",
          "Action": [
            "ec2:RebootInstances",
            "ec2:StopInstances",
            "ec2:TerminateInstances",
            "ec2:StartInstances",
            "ec2:AttachVolume",
            "ec2:DetachVolume",
            "ec2:AssociateIamInstanceProfile",
            "ec2:DisassociateIamInstanceProfile",
            "ec2:GetConsoleScreenshot",
            "ec2:ReplaceIamInstanceProfileAssociation"
          ],
          "Resource": [
            "arn:aws:ec2:region:account_number:instance/*",
            "arn:aws:ec2:region:account_number:volume/*"
          ],
          "Condition": {
            "StringEquals": {
              "ec2:ResourceTag/TAG-KEY": "Tag_key"
            }
          }
        },
        {
          "Sid": "AllowCreateTagsOnlyLaunching",
          "Effect": "Allow",
          "Action": [
            "ec2:CreateTags"
          ],
          "Resource": [
            "arn:aws:ec2:region:account_number:instance/*",
            "arn:aws:ec2:region:account_number:volume/*"
          ],
          "Condition": {
            "StringEquals": {
              "ec2:CreateAction": "RunInstances"
            }
          }
        }
      ]
    }

    **注意:**请将 account_numberregiontag_keytag_valueVPC-IDrole_name 参数替换为您的值。

  4. 选择 Review policy(查看策略),然后输入名称。

  5. 选择 Create policy(创建策略)。
    **注意:**您必须使用环境中的特定资源替换某些项。有关更多信息,请参阅使用 Amazon 资源名称 (ARN) 识别 AWS 资源

将策略附加到组

完成以下步骤:

  1. 打开 IAM 控制台
  2. 在导航窗格中,选择 User groups(用户组),然后选择组名称。
    **注意:**最佳做法是将策略附加到用户组,然后将用户添加到相应的组。
  3. 选择 Add permissions(添加权限),然后选择 Attach policies(附加策略)。
  4. 输入策略的名称,然后选择您的策略。
  5. 选择 Attach Policy(附加策略)。

当您附加自定义策略时,IAM 身份可以访问 AWS 管理控制台。然后,IAM 身份可以打开 EC2 控制面板并启动 EC2 实例。IAM 身份必须指定子网、VPC 和标签才能启动 EC2 实例。

ec2:ResourceTags 条件会限制以下操作:

  • 启动实例。
  • 停止实例。
  • 重启实例。
  • 终止实例。
  • 将卷附加到实例。
  • 将卷与实例分离。
  • 断开 IAM 实例配置文件与该实例的关联。
  • 关联实例配置文件。
  • 替换该实例的 IAM 实例配置文件关联。
  • 获取该实例的控制台屏幕截图。

策略会限制对 VPC 的以下操作:

  • 删除安全组。
  • 创建和删除路由。
  • 删除路由表。
  • 删除网络访问控制列表(网络 ACL)。
  • 删除网络 ACL 条目。
  • 授权或撤销安全组的入站和出站规则。
  • 创建网络接口权限。
  • 更新入站和出站规则的安全组描述。

相关信息

PrincipalTag、ResourceTag、RequestTag 和 TagKeys 条件键如何控制 IAM 策略中的访问权限?

IAM 策略元素: 变量和标签

友好名称和路径

AWS 官方已更新 5 个月前