Mitsukk
Mitsukk

Reputation: 321

Trigger lambda by DynamoDB Streams when object dateAttribute equal X

Stack :

A DynamoDB stream with specific criteria should trigger a lambda, my serverless function works:

  createStatementFiles:
    handler: lambda/statement.php
    timeout: 899 # 14min 59s
    layers:
      - arn:aws:lambda:#{AWS::Region}:<account_id>:layer:php-73:1
    role: CreateStatementFilesRole
    reservedConcurrency: 10
    events:
      - stream:
          type: dynamodb
          arn: arn:aws:dynamodb:eu-west-3:<account_id>:table/<table_name>/stream/<stream_id>
          filterPatterns:
            - eventName: [INSERT]
              dynamodb:
                NewImage:
                  __partitionKey:
                    S: [statementBalance]

What I absolutely want is for the DynamoDB stream to trigger the lambda if and only if the date of statementBalance = the last day of the month from the month of statementBalance, example:

$currentDate = '2022-10-25';
$statementBalance->getDate() = '2022-02-10';
$lastDayOfTheMonth = '2022-02-28', not equal last day of the month of $currentDate (2022-10-31)

Here is a diagram of what I would like to have:

enter image description here

If you have other solutions for my need which prevents the triggering of the lambda each time there is an INSERT of statementBalance, I am interested.

In advance, thank you very much for your help,

Upvotes: 2

Views: 835

Answers (1)

Mitsukk
Mitsukk

Reputation: 321

I add a new attribute for the StatementBalance object; "(bool) lastDayOfMonth"

This attribute will trigger the Lambda if and only if lastDayOfMonth = true

  createStatementFiles:
    handler: lambda/statement.php
    timeout: 899 # 14min 59s
    layers:
      - arn:aws:lambda:#{AWS::Region}:<account_id>:layer:php-73:1
    role: CreateStatementFilesRole
    reservedConcurrency: 10
    events:
      - stream:
          type: dynamodb
          arn: ${fetchStreamARN:${opt:stage}-${opt:client}-Project}
          filterPatterns:
            - eventName: [INSERT]
              dynamodb:
                NewImage:
                  __partitionKey:
                    S: [statementBalance]
                  lastDayOfMonth:
                    N: [1]

Thank's to @jarmod !

Upvotes: 1

Related Questions