2

Stack :

  • Serverless, version:

    Framework Core: 2.71.0 (local)
    Plugin: 5.5.4
    SDK: 4.3.2
    Components: 3.18.2
    
  • Php 7.2

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,

4
  • Manually filter for this condition in the Lambda function. Alternatively, have a second, calculated table that is written to only on that last day of the month and add DynamoDB stream trigger to that table. Commented Mar 16, 2022 at 13:47
  • Thank's @jarmod , I have already included the verification in the code of my lambda. It's just that I don't want my DynamoDB table trgger the lamda on every INSERT for nothing :/ Commented Mar 16, 2022 at 14:02
  • 1
    The problem is always going to be that your condition is not straightforward. Even though Lambda now supports event filtering, you need to perform a runtime calculation which is always going to require some compute (e.g. another Lambda function). Perhaps you could modify your table so that an additional 'lastDayOfMonth' attribute is written when it's the last day of the month, and then filter on that. Commented Mar 16, 2022 at 14:33
  • Thank's again so much @jarmod ! I use this smart solution :) ! Commented Mar 16, 2022 at 15:40

1 Answer 1

1

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 !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.