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 usesdevelopment(lowercase), it'll fail to find the value. - Missing/incomplete mapping entries: As we saw, your
DBInstanceClassvalue isn't finished. Make sure all entries in yourMappingsblock are complete and valid (e.g.,db.t2.microinstead of the truncated version). - Incorrect nested function structure: If you're nesting other intrinsic functions (like
!Refor!Sub) insideFn::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
- Validate your template first: Use the AWS CLI command
aws cloudformation validate-template --template-body file://your-template.yamlto catch syntax errors early. - Double-check mapping keys: Verify that the value you're using to look up the mapping (e.g., your
Environmentparameter) exactly matches the key in theMappingsblock (case included). - 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
相关产品推荐
相关产品推荐

