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
已提問 10 個月前檢視次數 384 次
1 個回答
3
已接受的答案

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
專家
已回答 10 個月前
  • 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.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南