Cloudformation 中有没有办法让 IAM 策略使用条件资源

Is there a way in Cloudformation to make IAM policies use conditional Resources

我有一堆云形成模板,其中包含用于警报的条件资源。只有产品堆栈才能创建这些资源。我需要在堆栈中创建的 IAM 策略来反映这些条件资源。到目前为止,我还没有找到一种方法来做到这一点。我曾尝试在单独的政策文件中使用条件:但它似乎忽略了它。

我会检查 Fn::If 内部函数。它对这样的东西真的很有用。例如,如果我有一个 ShouldGenerateBucket 条件和两个存储桶,将始终创建 constant-bucket 和可能创建的 conditional-bucket,我可以在我的策略中使用它,例如:

Type: "AWS::IAM::Policy"
Properties: 
  PolicyName: "RoleAccess"
  PolicyDocument: 
    Version: "2012-10-17"
    Statement: 
      -
        Effect: "Allow"
        Action: "s3:*""
        Resource:
          - arn:aws:s3:::constant-bucket
          - !If
            - ShouldGenerateBucket
            - arn:aws:s3:::conditional-bucket
            - !Ref AWS::NoValue

如果ShouldGenerateBuckettrue,这将添加额外的资源resource,否则忽略它。