7

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 ?

2 Answers 2

5

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

1
  • 1
    AWS documentation does not include starting / and trailing / slashes Commented Oct 16, 2018 at 5:15
1

This worked for me in YAML format

Parameters:
  MyEmail:
    Description: Email for notifications
    Type: String
    AllowedPattern: '[^@]+@[^@]+\.[^@]+'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.