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
posta 10 mesi fa384 visualizzazioni
1 Risposta
3
Risposta accettata

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
ESPERTO
con risposta 10 mesi fa
  • 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.

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande