CloudFormation:如何在Mappings中使用AWS :: AccountId?

4

我有一个类似这样的映射:

Mappings:
  AccountToParams:
    aws-1234567890:
      sshSecurityGroup: sg-abcedf12

我希望能够通过AccountId来检索我的变量,但是这一步卡在了“验证”环节。

SecurityGroups:
    - !FindInMap [AccountToParams, !Sub "aws-${AWS::AccountId}", sshSecurityGroup]

错误是指

16/08/2017, 16:36:18 - Template contains errors.: Template error: 
every Fn::FindInMap object requires three parameters, 
the map name, map key and the attribute for return value

目标是使某些配置由账户(因此是环境)驱动。但我似乎不能使用accountId作为映射中的键,否则AWS不会满意,因为它不包含字母数字字符。


你没有 a 映射。在那个 YAML 文档中,你有四 (4) 个映射。 - Anthon
2个回答

10

将地图更改为:

Mappings:
  AccountToParams:
    "1234567890":
      sshSecurityGroup: sg-abcedf12

请使用!Ref代替!Sub:
SecurityGroupIds:
    - !FindInMap [AccountToParams, !Ref "AWS::AccountId", sshSecurityGroup]

使用FN::Join将“aws”字符串添加到帐户ID中,以便在堆栈的后续部分中需要时使用。

perfect, simple and concise - Stephane Maarek
使用数字作为映射键,您不会遇到此错误吗?模板格式错误:[/Mappings/TargetAzMap] 映射键必须是字符串;而接收到的是数字 [xxxxx]。 - zhy2002

0

这个有效

Mappings: 

  AccountToParams:
    "123456789012":
      sshSecurityGroup: sg-abcdef12
  
Resources: 
 
      SecurityGroups: !FindInMap 
            - AccountToParams
            - !Ref 'AWS::AccountId'
            - sshSecurityGroup

1
您的回答可以通过附加支持信息来改进。请[编辑]以添加更多细节,例如引用或文档,以便他人可以确认您的答案是否正确。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接