Lambda的自定义角色如何与EC2角色策略一起使用?

时间:2019-08-14 23:26:13

标签: amazon-web-services aws-lambda amazon-iam aws-sam

以下是为使用SAM模板编写的lambda函数(some-role-serv-LogicalID-GDGGGGGBMW2)创建的自定义执行角色(AWS::Serverless::Function):

{
  "permissionsBoundary": {
    "permissionsBoundaryArn": "arn:aws:iam::111222333444:policy/some-permission-boundary",
    "permissionsBoundaryType": "Policy"
  },
  "roleName": “some-role-serv-LogicalID-GDGGGGGBMW2”,
  "policies": [
    {
      "document": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Action": "sqs:*",
            "Resource": "arn:aws:sqs:us-east-1:111222333444:someq*",
            "Effect": "Allow"
          },
          {
            "Action": [
              "logs:CreateLogGroup",
              "logs:CreateLogStream",
              "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:us-east-1:111222333444:log-group:*",
            "Effect": "Allow"
          }
        ]
      },
      "name": "lambda-policy",
      "type": "inline"
    }
  ],
  "trustedEntities": [
    "lambda.amazonaws.com"
  ]
}

some-permission-boundary在哪里:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:us-east-1:111222333444:log-group:*"
            ],
            "Effect": "Allow",
        },
        {
            "Action": [
                "sqs:DeleteMessage",
                "sqs:ReceiveMessage",
                "sqs:SendMessage",
                "sqs:ListDeadLetterSourceQueues",
                "sqs:GetQueueAttributes",
                "sqs:GetQueueUrl"
            ],
            "Resource": [
                "arn:aws:sqs:us-east-1:111222333444:someq*"
            ],
            "Effect": "Allow",
        }
    ]
}

在SAM模板中为

some-role-serv-LogicalID-GDGGGGGBMW2分配了权限边界(some-permission-boundary

Lambda函数采用以下SAM模板语法承担自定义角色:

Role: !GetAtt LogicalID.Arn

在部署过程中,

lambda是通过EC2内的docker容器创建的(使用sam deploy

EC2假定在下面还有一个附加的角色策略:

   {
        "Condition": {
            "StringEquals": {
                "iam:PermissionsBoundary": "arn:aws:iam::111222333444:policy/some-permission-boundary"
            }
        },
        "Action": [
            "iam:CreateRole",
            "iam:AttachRolePolicy",
            "iam:PutRolePolicy",
            "iam:DetachRolePolicy",
            "iam:GetRolePolicy"
        ],
        "Resource": [
            "arn:aws:iam::111222333444:role/some-role*"
        ],
        "Effect": "Allow"
    }

该EC2策略应确保没有以下属性的任何自定义角色(例如some-role-serv-LogicalID-GDGGGGGBMW2):

PermissionsBoundary: !Sub "arn:aws:iam::${AWS::AccountId}:policy/some-permission-boundary"

不应允许创建角色some-role-serv-LogicalID-GDGGGGGBMW2

创建堆栈时出现以下错误:

enter image description here

堆栈创建成功,但是

1) 为什么sam deploy命令会收到此错误?

2)

EC2策略是否禁止创建没有权限边界(some-role-serv-LogicalID-GDGGGGGBMW2)的自定义角色(some-permission-boundary)?符合预期...

1 个答案:

答案 0 :(得分:1)

该错误表明您的EC2实例(正在调用sam deploy操作的实体没有执行iam:GetRolePolicy的权限,在这里确实是这种情况。

问题是,尽管您可以在这种情况下限制其他4种操作

"Condition": {
    "StringEquals": {
        "iam:PermissionsBoundary": "arn:aws:iam::111222333444:policy/some-permission-boundary"
    }
}

您不能对GetRolePolicy做同样的事情。此操作不能受该条件限制,否则其效果将无效。适用于此操作的唯一服务级别条件是iam:ResourceTag

如果您转到管理控制台并尝试创建此类IAM策略,则可以看到此警告是由于您的情况与iam:GetRolePolicy操作的组合而引起的。

  

此策略定义了一些不符合要求的操作,资源或条件   提供权限。要授予访问权限,策略必须采取措施   具有适用的资源或条件。

解决方案是将您的陈述一分为二。首先,该条件将限制创建没有必需权限边界的IAM角色以及除上述iam:GetRolePolicy之外的其他IAM操作。然后,您应该创建仅包含iam:GetRolePolicy且没有该条件的第二条语句。

    {
        "Condition": {
            "StringEquals": {
                "iam:PermissionsBoundary": "arn:aws:iam::111222333444:policy/some-permission-boundary"
            }
        },
        "Action": [
            "iam:CreateRole",
            "iam:AttachRolePolicy",
            "iam:PutRolePolicy",
            "iam:DetachRolePolicy"
        ],
        "Resource": [
            "arn:aws:iam::111222333444:role/some-role*"
        ],
        "Effect": "Allow"
    }

   {
        "Action": [
            "iam:GetRolePolicy"
        ],
        "Resource": [
            "arn:aws:iam::111222333444:role/some-role*"
        ],
        "Effect": "Allow"
    }

回答第二个问题。是的,您可以将iam:PermissionsBoundary条件键与iam:CreateRole一起使用,以防止创建没有特定权限边界的角色。