Amazon web services 在Fn::if中使用多个Fn::Sub

Amazon web services 在Fn::if中使用多个Fn::Sub,amazon-web-services,amazon-cloudformation,Amazon Web Services,Amazon Cloudformation,当前正在尝试将Fn::if与多个Fn::SUB一起使用,但当前仅转换第一个Fn::SUB。我尝试使用多个方法来创建它,但是这是我能够生成的唯一一个不会导致语法错误的方法 Resource: - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame - !Sub a

当前正在尝试将Fn::if与多个Fn::SUB一起使用,但当前仅转换第一个Fn::SUB。我尝试使用多个方法来创建它,但是这是我能够生成的唯一一个不会导致语法错误的方法

Resource:
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
 - Fn::If:
   - USRegion
   - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
     !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
     !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
     !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
   - !Ref AWS::NoValue
试用

Resource:
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
 - Fn::If:
   - USRegion
   - - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
     - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
     - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
     - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
   - !Ref AWS::NoValue
我在另一个问题中看到了这一点,但仍然没有骰子

我该如何更改它以转换每个值,而不是仅转换第一个值

编辑:对于这里的上下文,我已经添加了多个arn,它们不是if语句的一部分

我找到了两个目前最好的解决方案

 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame3
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame4
 - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame5
 - Fn::If:
   - USRegion
   - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame1
   - !Ref AWS::NoValue
 - Fn::If:
   - USRegion
   - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame2
   - !Ref AWS::NoValue

我认为应该(小心凹痕):

如果
USRegion
为true,则返回一个列表。在最初的尝试中,您希望创建一个列表列表


但是您需要重新考虑使用
AWS::NoValue
,假设这是某种IAM策略
Resource
位于IAM策略语句中,因此在
USRegion
为false的情况下,您无法在没有资源的情况下制定IAM策略。

看起来您正在生成IAM策略。您的第二次尝试是使用列表中的列表生成这样的结果

Resource:
 - - arn1
   - arn2
   - arn3
   - arn4
您的模板应如下所示:

 Resource:
   "Fn::If":
     - USRegion
     - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
       !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
       !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
       !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
     - []

或者,如果无法将资源设置为空数组,请将if语句移到更高的位置,以便生成整个策略语句。

遗憾的是,如果没有“-Fn::if:”我只收到一个“YAML格式不正确”。啊,Fn::if需要加引号。不幸的是,这只是产生了与我原来的问题相同的结果,它创建了一个包含所有4行的列表项,而不是4个列表项。下面的答案是最接近正确的。这似乎可以解决单个列表的问题,但如果在“if”语句之外有其他列表项,则会收到错误。我在此上下文中找到的唯一解决方案就是在语句的两侧都包含常量(保持不变的资源),或者将if语句限制为单个列表项(似乎无法添加到更高级别的列表)。@ION谢谢,是的,您不能混合列表。谢谢你让我知道。很高兴它成功了:-)
Resource:
 - - arn1
   - arn2
   - arn3
   - arn4
 Resource:
   "Fn::If":
     - USRegion
     - !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
       !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
       !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
       !Sub arn:aws:ecr:${AWS::Region}:${AWS::AccountId}:repository/reponame
     - []