- Más nuevo
- Más votos
- Más comentarios
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.
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
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.
Hey, can you pls share a tutorial for this. I am unable to perform this by https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/sqs-examples-send-receive-messages.html
This tutorial demonstrates how to create a Lambda function that consumes messages from SQS. You will also need to send the message to SQS. You can configure the queue itself to be a delayed queue by specifying "Delivery delay" when you create or update the queue, or you can add the delay on every message you send to the queue by specifying the DelaySeconds parameter.
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
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.
Contenido relevante
- OFICIAL DE AWSActualizada hace un año
- OFICIAL DE AWSActualizada hace 2 años
- OFICIAL DE AWSActualizada hace 2 años
Can you kindly describe your use case in more detail?