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 回答
3
已接受的回答

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
专家
Tasio
已回答 2 年前
profile picture
专家
已审核 1 个月前
  • 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
专家
Uri
已回答 2 年前
profile picture
专家
已审核 1 个月前
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
已回答 2 年前
  • 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.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则