AWS Cloudformation嵌套的内部函数未进行评估

时间:2018-05-03 08:50:14

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

这是我正在编写的cloudformation模板的一部分,并使用Fn::FindInMap函数收到错误:

Parameters:
  VpcStackName:
    Description: >-
      Name of an active CloudFormation VPC stack that contains the networking
      resources, such as the subnet and security group, that will be used in
      this stack.
    Type: String
    MinLength: 1
    MaxLength: 255
    AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$'
    Default: wordpress-dev-vpc

Mappings:
  Instance:
    development:
      AllocatedStorage: 20
      DBInstanceClass: db.t2.micro
    production:
      AllocatedStorage: 25
      DBInstanceClass: db.m3.medium

Resources:
  DBInstance:
      Type: AWS::RDS::DBInstance
      DeletionPolicy: Snapshot
      Properties:
        Engine: MariaDB
        StorageType: gp2
        MasterUsername: !Ref MasterUsername
        MasterUserPassword: !Ref MasterUserPassword
        AllocatedStorage:
          Fn::FindInMap:
            - Instance
            - Fn::ImportValue:
                Fn::Sub: '${VpcStackName}-Environment'
            - AllocatedStorage
        DBInstanceClass:
          Fn::FindInMap:
            - Instance
            - Fn::ImportValue:
                Fn::Sub: '${VpcStackName}-Environment'
            - DBInstanceClass

在另一个堆栈中,我正在导出${VpcStackName}-Environment,如下所示:

Outputs:
  Environment:
    Description: Environment type of this stack
    Value: !Ref Environment
    Export:
      Name: !Sub '${AWS::StackName}-Environment'

尝试使用Fn::FindInMap函数时,出现此错误:

An error occurred (ValidationError) when calling the ValidateTemplate operation: Template error: every Fn::FindInMap object requires three parameters, the map name, map key and the attribute for return value

有什么建议吗?

根据https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html处的文档,Fn::FindInMap函数中支持的函数为Fn::FindInMapRef。那还有另一种方法吗?例如,将Fn::ImportValue: !Sub '${VpcStackName}-Environment'的值存储在临时变量中?

1 个答案:

答案 0 :(得分:1)

根据this文档,Fn::FindInMap函数仅可用于以下功能:

  • Fn::FindInMap
  • 参考

因此Fn::ImportValueFn::Sub将不会被评估。