Trigger cloudwatch event from lambda

0

How to trigger cloudwatch event from lambda upon creation of a record in database. My use case is I want to run a cloudwatch event that is scheduled for 5 mins for one time. As soon as a record is created in DynamoDB by Lambda, this cronjob should be triggered by same lambda and which will invoke target function after 5 mins as scheduled in cronjob.

  • Can you kindly describe your use case in more detail?

3 Answers
3
Accepted Answer

Another approach that you can use, as this seems to be some type of workflow, is to have a Step Functions state machine that writes on the DynamoDB table, then waits for 5 minutes, and finally executes the Lambda function.

profile pictureAWS
EXPERT
Tasio
answered 2 years ago
profile picture
EXPERT
reviewed 23 days ago
  • I opted this approach because somewhere in our workflow I might need more then 5 mins delay say 30 mins. so state machines are best choice here. SQS max limit is 15 mins. so accept this answer. thanks

2

If I understand correctly, you would like to trigger an operation that will happen 5 minutes after you make a change to a DynamoDB table.

I would recommend against the pattern you suggested using CloudWatch events (EventBridge) cron jobs as they are meant for ongoing tasks, not a one time task. Even if you specify a cron job that will only fire once, the rule will remain. You will need to later delete the Rule, otherwise you will get to the max number of rules very quickly.

I recommend that when you make the change to the table you will send a message to an SQS delay queue. You will also have a Lambda function triggering from that queue.

If I misunderstood your use case, please update the post.

profile pictureAWS
EXPERT
Uri
answered 2 years ago
profile picture
EXPERT
reviewed 23 days ago
0

This is a perfect use case for Serverless Application Model (SAM), which is way to build serverless applications using Infrastructure as Code (IaC), using CloudFormation.

Once you've installed the SAM CLI, use the sample templates to build a Hello World SAM app.

Put your lambda code into the app folder, in app.py.

Then, replaced the template.yaml file with the below. Modify the cron entry to the schedule you want. Modify the DyanmoDB schema to match yours.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  SAM: trigger Lambda from DDB insert

Globals:
  Function:
    Timeout: 40
    MemorySize: 1024
    Runtime: python3.9
    Architectures:
        - x86_64
    ReservedConcurrentExecutions: 1

Resources:
  LambdaFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: app/
      Handler: app.lambda_handler
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref DDBTable 
      Events:
        MyEvent:
          Type: Schedule
          Properties:
            Schedule: cron(*/5 * * * ? *)

  DDBTable:
    Type: AWS::DynamoDB::Table
    Properties: 
      AttributeDefinitions: 
        - AttributeName: area
          AttributeType: S
      KeySchema: 
        - AttributeName: area
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES

then run: sam build && sam deploy

AWS
answered 2 years ago
  • If I understand the question correctly, the requirement is to trigger a Lambda function once five minutes after an item has been put in the DynamoDB table.

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