You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS CloudFormation嵌套内置函数未生效及Fn::FindInMap报错问题

Fixing Fn::FindInMap Errors & Unevaluated Nested Functions in CloudFormation

Alright, let's work through your CloudFormation template issues step by step. First, let's address the obvious red flag in the snippet you shared: your DBInstanceClass value in the Instance mapping is cut off (db.t2.mic...)—that's definitely going to cause a syntax error right off the bat. Let's fix that first, then dive into the Fn::FindInMap and nested function problems.

Common Fn::FindInMap Error Causes

Most issues with this function boil down to one of these:

  • Mismatched keys: CloudFormation is case-sensitive, so if you're referencing a key like Development (capitalized) but your mapping uses development (lowercase), it'll fail to find the value.
  • Missing/incomplete mapping entries: As we saw, your DBInstanceClass value isn't finished. Make sure all entries in your Mappings block are complete and valid (e.g., db.t2.micro instead of the truncated version).
  • Incorrect nested function structure: If you're nesting other intrinsic functions (like !Ref or !Sub) inside Fn::FindInMap, a formatting mistake will prevent the function from evaluating properly.

Fixing Unevaluated Nested Functions

Nested intrinsic functions need to follow CloudFormation's strict YAML/JSON structure. Let's look at a corrected version of your template with proper nesting:

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
  # Add an Environment parameter to map to your Instance mapping
  Environment:
    Type: String
    AllowedValues: [development, production]
    Default: development

Mappings:
  Instance:
    development:
      AllocatedStorage: 20
      DBInstanceClass: db.t2.micro  # Fixed the truncated value
    production:
      AllocatedStorage: 50
      DBInstanceClass: db.t3.small

Resources:
  MyWordPressDB:
    Type: AWS::RDS::DBInstance
    Properties:
      # Correct usage of Fn::FindInMap with nested !Ref
      AllocatedStorage: !FindInMap [Instance, !Ref Environment, AllocatedStorage]
      DBInstanceClass: !FindInMap [Instance, !Ref Environment, DBInstanceClass]
      # Example of nested !Sub + !ImportValue (common with VPC stack references)
      DBSubnetGroupName: !ImportValue
        Fn::Sub: "${VpcStackName}-DBSubnetGroup"
      VPCSecurityGroups:
        - !ImportValue
          Fn::Sub: "${VpcStackName}-DBSecurityGroup"
      # Add other required RDS properties here (like Engine, MasterUsername, etc.)

Key Formatting Rules for Nested Functions:

  • When using short-form functions (like !FindInMap, !Ref), pass nested functions as elements in the function's array argument.
  • For long-form functions (like Fn::FindInMap), ensure each nested function is a separate element in the function's array:
    AllocatedStorage:
      Fn::FindInMap:
        - Instance
        - !Ref Environment
        - AllocatedStorage
    
  • Never wrap functions in quotes—this tells CloudFormation to treat them as literal strings instead of evaluating them.

Troubleshooting Steps

  1. Validate your template first: Use the AWS CLI command aws cloudformation validate-template --template-body file://your-template.yaml to catch syntax errors early.
  2. Double-check mapping keys: Verify that the value you're using to look up the mapping (e.g., your Environment parameter) exactly matches the key in the Mappings block (case included).
  3. Test with hardcoded values: If you're still having issues, temporarily replace nested functions with hardcoded values (e.g., !FindInMap [Instance, development, AllocatedStorage]) to confirm the mapping itself works, then reintroduce the nested functions one by one.

内容的提问来源于stack exchange,提问作者GMaster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:40:41