【问题标题】:CFn: Use !If to add multiple Principals to a statementCFn:使用 !If 将多个主体添加到语句中
【发布时间】:2022-01-26 13:22:32
【问题描述】:

我有以下模板定义了一个不起作用的 IAM 策略:

      RoleName: 'ABCRole'
      AssumeRolePolicyDocument:
        Statement:
          - Action: ['sts:AssumeRole']
            Effect: Allow
            Principal:
              AWS:
              - 1234567890 # Some AWS account
              - !If
                - !And
                  - !Condition Condition1
                  - !Condition Condition2
                - arn:aws:iam::11111111111111:role/ABCDE_Role # first role
                - arn:aws:iam::22222222222222:role/ABCDE_Role # second role (different account number)
                - !Ref AWS:NoValue

我正在努力实现这一目标:当Condition1Condition2 都为真时,我将能够附加arn:aws:iam::11111111111111:role/ABCDE_Rolearn:aws:iam::22222222222222:role/ABCDE_Role 作为两个额外的主体。否则,什么也不做——让1234567890 作为唯一的主体。

请注意,arn:aws:iam::11111111111111:role/ABCDE_Rolearn:aws:iam::22222222222222:role/ABCDE_Role 仅与 aws 帐户不同,所以也许我可以使用 !Sub 替换帐号?有点像:

for account in [11111111111111, 22222222222222]:
 !Sub arn:aws:iam::${account}:role/ABCDE_Role 

我应该如何修改上面的模板?提前谢谢!

【问题讨论】:

  • 您当前的模板有什么问题?有什么错误吗?
  • !If 抱怨格式。我刚刚了解到!If 的第一个参数必须是Condition,所以可能使用嵌套的!And 是错误的。
  • 这是一个,但还有更多错误。你能发布实际的错误消息吗?

标签: amazon-web-services yaml amazon-cloudformation


【解决方案1】:

我认为是因为 !If 条件所期望的格式,按照aws 的文档,格式为:

!If [condition_name, value_if_true, value_if_false]

如果你检查你的模板,你有四个元素,而不是三个。

另外,伪参数是(带双:):

AWS::NoValue

因此,当条件为 True 时添加您需要的两个帐户的一种可能解决方案是尝试添加一个新条件,该条件将您已有的条件 1 和条件 2 与 !And 函数结合起来,如下所示:

Conditions:
  Condition1: your condition
  Condition2: your condition
  ConditionCombined: !And [!Condition Condition1, !Condition Condition2]
Resources:
  Role1:
    Type: AWS::IAM::Role
    Properties: 
      RoleName: 'ABCRole'
      AssumeRolePolicyDocument:
        Statement:
          - Action: ['sts:AssumeRole']
            Effect: Allow
            Principal:
              AWS:
              - 1234567890 # Some AWS account
              - !If
                - ConditionCombined
                - arn:aws:iam::11111111111111:role/ABCDE_Role # first role
                - !Ref AWS::NoValue
              - !If
                - ConditionCombined
                - arn:aws:iam::22222222222222:role/ABCDE_Role # second role (different account number)
                - !Ref AWS::NoValue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    相关资源
    最近更新 更多