HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

AllowedPattern for emails in Cloudformation template

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
templatecloudformationforemailsallowedpattern

Problem

I am struggling with specifying a regular expression for email parameter of Cloudformation :

Parameters:
  MyEmail :
    Type: String
    Description: Email for notifications
    AllowedPattern: /[^\s@]+@[^\s@]+\.[^\s@]+/
    ConstraintDescription: You should enter a valid email


As you see, I tried /[^\s@]+@[^\s@]+\.[^\s@]+/ and it won't work.

What is the regular expression for emails in Cloudformation template ?

aws regex aws-cloudformation

Solution

Important

Since AWS CloudFormation templates use the JSON syntax for specifying objects and data, you will need to add an additional backslash to any backslash characters in your regular expression, or JSON will interpret these as escape characters.

For example, if you include a \d in your regular expression to match a digit character, you will need to write it as \\d in your JSON template.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-regexes.html

The very same applies to your \s expressions. Use \\s and it works.

EDIT:

Looking back the question I started to wonder the missing quotation marks on your definition. I found out the expessions are yaml format that is the second way to define things in aws.

If you should follow the yaml syntax, the right way to give string values is like this:

AllowedPattern = '/[^\s@]+@[^\s@]+\.[^\s@]+/'


https://aws.amazon.com/blogs/aws/aws-cloudformation-update-yaml-cross-stack-references-simplified-substitution/

EDIT:

Use quotes if your value includes special characters, (e.g. :, {, }, [, ], ,, &, *, #, ?, |, -, , =, !, %, @, ).

https://stackoverflow.com/questions/19109912/do-i-need-quotes-for-strings-in-yaml

Code Snippets

AllowedPattern = '/[^\s@]+@[^\s@]+\.[^\s@]+/'

Context

StackExchange DevOps Q#2914, answer score: 5

Revisions (0)

No revisions yet.