【问题标题】:CloudFormation: How to use AWS::AccountId in Mappings?CloudFormation:如何在映射中使用 AWS::AccountId?
【发布时间】:2018-01-24 04:27:22
【问题描述】:

我有一个如下所示的映射:

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) 个映射。

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


【解决方案1】:

把地图改成:

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

并使用!Ref 而不是!Sub

SecurityGroupIds:
    - !FindInMap [AccountToParams, !Ref "AWS::AccountId", sshSecurityGroup]

如果需要进一步降低堆栈,请使用 FN::Join 将“aws”字符串添加到帐户 ID。

【讨论】:

  • 完美、简洁、简洁
  • 您没有因为使用数字作为映射键而收到此错误?模板格式错误:[/Mappings/TargetAzMap] 映射键必须是字符串;改为收到数字 [xxxxx]
【解决方案2】:

这行得通

Mappings: 

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

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2020-03-30
  • 2021-04-30
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2017-02-14
  • 1970-01-01
相关资源
最近更新 更多