如何在!Sub 中使用!FindInMap | Cloudformation 的用户数据部分

问题描述

如何在用户数据部分使用 !FindInMap。 在以下用户数据中,我想用映射数据更新 MainSshKey。

Mappings:
    AccountToStage:
        "123456789012":
            StageName: Beta
    Beta:
      us-east-1:
        MainSshKey: ssh-rsa AAAAB3NzaC
        AdminSshKey: ssh-rsa AAAAB3NzaC1

用户数据:

        Fn::Base64: !Sub |
          #cloud-config
          users:
          - name: main
            ssh_authorized_keys:
            - ${MainSshKey}
          - name: admin
            ssh_authorized_keys:
            - ${AdminSshKey}

这是我试过的

          #cloud-config
          users:
          - name: main
            ssh_authorized_keys: 
            - ${MainSshKey}
            - MainSshKey: !FindInMap
                - !FindInMap
                  - AccountToStage
                  - !Ref "AWS::AccountId"
                  - StageName
               - !Ref "AWS::Region"
               - MainSshKey
          - name: admin
            ssh_authorized_keys:
            - ${AdminSshKey}

Cloudformation 无法解决此问题。

注意:如果我将 MainSshKey 定义为参数,并且它工作正常,则使用 FindInMap 似乎不起作用

非常感谢任何指针

解决方法

您必须使用 Sub 的列表形式。所以你的可能应该是这样的。请注意,您可能会修复所有缩进并进一步更改以使其正常工作。不过,Sub 的列表形式是解决问题的关键。


        Fn::Base64: !Sub 
           - |
             #cloud-config
             users:
             - name: main
               ssh_authorized_keys:
               - ${SubMainSshKey}
             - name: admin
               ssh_authorized_keys:
               - ${AdminSshKey}
           - SubMainSshKey: !FindInMap
                - !FindInMap
                  - AccountToStage
                  - !Ref "AWS::AccountId"
                  - StageName
                - !Ref "AWS::Region"
                - MainSshKey

另外,这是不正确的用户数据,所以我不确定您想要实现什么。

,

我能够使用以下方法解决这个问题,加号告诉它之后保留换行符。 Arg1 告诉它用 Fn::FindInMap 的结果替换 arg0 中的 MainSshKey。

         Fn::Base64: !Sub
          - |+
            #cloud-config
            users:
            - name: main
              ssh_authorized_keys:
              - ${MainSshKey}
            - name: admin
              ssh_authorized_keys:
              - ${AdminSshKey}
          - MainSshKey:
              Fn::FindInMap:
                - !FindInMap
                  - AccountToStage
                  - !Ref "AWS::AccountId"
                  - StageName
                - !Ref "AWS::Region"
                - MainSshKey

此外,如果您需要替换两个值(例如:MainSshKey 和 AdminSshKey),您可以使用下面相同的列表子构造示例。

         Fn::Base64: !Sub
          - |+
            #cloud-config
            users:
            - name: main
              ssh_authorized_keys:
              - ${MainSshKey}
            - name: admin
              ssh_authorized_keys:
              - ${AdminSshKey}
          - MainSshKey:
              Fn::FindInMap:
                - !FindInMap
                  - AccountToStage
                  - !Ref "AWS::AccountId"
                  - StageName
                - !Ref "AWS::Region"
                - MainSshKey
            AdminSshKey:
              Fn::FindInMap:
                - !FindInMap
                  - AccountToStage
                  - !Ref "AWS::AccountId"
                  - StageName
                - !Ref "AWS::Region"
                - AdminSshKey