EKS 工作人员是否可以承担预先存在的 IAM 角色



我们运行一个 EKS 集群,该集群是根据以编程方式构建的 Cloudformation 模板构建的。目前,工作线程模板接近 https://github.com/awslabs/amazon-eks-ami/blob/master/amazon-eks-nodegroup.yaml,但我们的 Resources.NodeInstanceRole.ManagedPolicyArns 有一些额外的用户制作策略。

我们希望创建一个附加了任何策略的 IAM 角色,然后让 EKS 工作人员代入此角色,而不是将托管策略添加到此列表中。问题是我们无法在 Cloudformation 模板中找到执行此操作的方法。

我认为模板的相关部分如下:

  NodeInstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Path: "/"
      Roles:
        - !Ref NodeInstanceRole
  NodeInstanceRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service: ec2.amazonaws.com
            Action: sts:AssumeRole
      Path: "/"
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy
        - arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy
        - arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly

与其构建一个新的IAM::Role (NodeInstanceRole(,我想应该有一种方法可以在NodeInstanceProfile中引用现有角色的ARN。尝试以下列方式更改 NodeInstanceProfile 会导致以下错误:

  NodeInstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Path: "/"
      Roles:
        - "arn:aws:iam::xxx:role/yyy"
The specified value for roleName is invalid. It must contain only alphanumeric characters and/or the following: +=,.@_- (Service: AmazonIdentityManagement; Status Code: 400; Error Code: ValidationError; Request ID: xxx)

事实证明,解决方案非常简单:NodeInstanceProfile 需要角色名称,而不是 ARN。因此,在Cloudformation模板的最终版本中,我引用的代码块被简化为:

NodeInstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Path: "/"
      Roles:
        - yyy

最新更新