Getting Invalid event pattern error while creating cloudformation stack.

0

Unable to create event filter with dynamic value, I am trying to use PerformaceTenantHostName parameter in event filter but getting invalid filter pattern error.

AWSTemplateFormatVersion : 2010-09-09
Parameters:
  PerformaceTenantHostName:
    Type: CommaDelimitedList
    Default: "beta1, Alpha1"
    Description: The tenant host name list
    
Resources:

  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Policies:
        - PolicyName: allowLambdaLogs
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
            - Effect: Allow
              Action:
              - logs:*
              Resource: arn:aws:logs:*:*:*
        - PolicyName: allowSqs
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
            - Effect: Allow
              Action:
              - sqs:ReceiveMessage
              - sqs:DeleteMessage
              - sqs:GetQueueAttributes
              - sqs:ChangeMessageVisibility
              Resource: !GetAtt MyQueue.Arn

  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          exports.handler = async (sqsEvent) => {
            console.log(JSON.stringify(sqsEvent));
          }
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Runtime: nodejs18.x
      Timeout: 60
      MemorySize: 512

  LambdaFunctionEventSourceMapping:
    Type: AWS::Lambda::EventSourceMapping
    Properties:
      BatchSize: 10
      Enabled: true
      EventSourceArn: !GetAtt MyQueue.Arn
      FunctionName: !GetAtt LambdaFunction.Arn
      FilterCriteria:
        Filters:
            - Pattern: '{ "body" : { "TenantHostName" : [ { "anything-but":  **${Ref PerformaceTenantHostName}** } ] }}'
  MyQueue:
    Type: AWS::SQS::Queue
    Properties:
      DelaySeconds: 0
      VisibilityTimeout: 120

It that possible to use parameter in event filter.

  • Try with the following syntax,

    Pattern: '{ "body" : { "TenantHostName" : [ { "anything-but": [ {"Ref": "PerformaceTenantHostName"} ] } ] } }'

Ashwani
asked 10 months ago361 views
1 Answer
3
Accepted Answer

Hi Ashwani,

This is not the nice way of doing but you may find it useful if you really want to make it as dynamic parameterization. Here is what you may consider doing it to achieve the end result:

1. First change:

      PerformaceTenantHostName:
          Type: String
         Default: '["Beta1","Alpha1"]'
         Description: The tenant host name list

2. Second Change

  Pattern: !Sub '{ "body" : { "TenantHostName" : [ { "anything-but":  ${PerformaceTenantHostName} } ] }}'

I was able to do it this way, with same end result. Attaching snapshots for your reference:

- When parameters values were hardcoded as below:

   - Pattern: '{ "body" : { "TenantHostName" : [ { "anything-but": ["Beta1","Alpha1"] } ] }}'

Enter image description here

- When parameters were passed in Sub functions as below:

  - Pattern: !Sub '{ "body" : { "TenantHostName" : [ { "anything-but":  ${PerformaceTenantHostName} } ] }}'

Enter image description here

profile pictureAWS
EXPERT
answered 10 months ago
  • Thanks above solution working fine. Can we use SSM parameter after creating inside Pattern ? Eg.

    • Pattern: !Sub '{ "body" : { "TenantHostName" : [ { "anything-but": {{ssm:PerformaceTenantHostName}} } ]}}'
  • Let me check on this. I'll update you here in sometime.

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.

Guidelines for Answering Questions