Skip to content

Does CloudFormation validate AllowedPattern against the supplied parameter value or a trimmed copy at stack execution?

0

We have a CloudFormation stack with a String parameter that carries a PEM-formatted public key. We added an AllowedPattern constraint (with a ConstraintDescription) so that a value ending in trailing whitespace (space, tab, newline) or containing a carriage return fails parameter validation. The intent is to reject untrimmed keys at deployment time. The constraint is deployed and verified present on the stack.

Read-only testing with ValidateTemplate shows behavior consistent with the validator checking a trimmed copy of the value: with a template whose parameter default is a short marker string and AllowedPattern anchored to match exactly that string (for example \AA\z, matching only the single character A), ValidateTemplate accepts default values of A followed by a newline, A followed by a space, and A followed by a tab, while rejecting an ordinary mismatch and rejecting an internal carriage return under our deployed pattern. ValidateTemplate does not execute a deployment, so it does not establish behavior for a supplied parameter at execution.

The Parameters documentation says that for a String parameter the AllowedPattern "must match the entire parameter value provided", but does not say whether the value is trimmed or normalized before matching, and does not distinguish ValidateTemplate from execution-time validation.

Question: when a stack create, update, or change-set execution supplies a parameter value (not the template default), does CloudFormation validate AllowedPattern against the value exactly as supplied, or against a trimmed/normalized copy? If trimmed, a trailing-whitespace constraint can never fire at execution; confirmation either way lets us decide whether the constraint is a real control or needs replacing (for example with a deploy-time check outside CloudFormation).

asked 13 days ago59 views

1 Answer
0

CloudFormation validates the AllowedPattern constraint against the parameter value exactly as supplied, without trimming or normalization. When you supply a parameter value during stack creation, update, or change set execution, CloudFormation checks the AllowedPattern regular expression against the literal string you provide.

The behavior you observed with ValidateTemplate is specific to that API operation and does not reflect how parameter validation works during actual stack operations. ValidateTemplate performs only basic template syntax checking and does not execute the full parameter validation logic that runs during CreateStack, UpdateStack, or CreateChangeSet operations.

During actual stack operations, CloudFormation's pre-deployment validation runs multiple checks against your template and target environment. Property syntax validation—which includes AllowedPattern checking—runs on all CreateStack, UpdateStack, and CreateChangeSet operations. This validation operates in FAIL mode, meaning it stops the stack operation before any resources are provisioned if validation detects errors.

Your AllowedPattern constraint designed to reject values with trailing whitespace or carriage returns should function as intended during real deployments. If a user supplies a parameter value with trailing spaces, tabs, or newlines, CloudFormation will validate that exact string against your pattern. Since your pattern is anchored to reject trailing whitespace, the validation will fail and prevent the stack operation from proceeding.

Therefore, your constraint is a real control that will enforce your whitespace requirements at execution time. You do not need to replace it with an external deploy-time check.
Sources
Ship infrastructure faster with CloudFormation and CDK pre-deployment validation on every stack operation | AWS DevOps & Developer Productivity Blog
Validate stack deployments - AWS CloudFormation
AWS::SSM::Parameter - AWS CloudFormation
CloudFormation template Parameters syntax - AWS CloudFormation

answered 13 days ago

EXPERT

reviewed 13 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.