AWS CDK Lambda Scheduler Write to DynamoDB

The following stack will be a simple example of a triggered schedular that will call a lambda function that writes to a DynamoDB table. The stack, seen below, contains 3 main elements. Starting at the bottom;

  • The first element comes from Event Bridge, this is the scheduler itself and can be configured to trigger an event on a regular basis.
  • Second element is our triggered lambda function, this will be called each time the even bride event fires.
  • Finally at the top we have the DynamoDB table, a simple table that is used in this case to show the stack is working.

If you want to see a practical use for this stack, check out the Serverless website monitoring stack.

If you need a function that runs for longer than 15 minutes, this can be done with multiple lambda function calls or checkout Fargate Schedular

To create each of the three main components of this stack the code in CDK is quite simple.

Firstly, we create the DynamoDB table. This table will have a simple partition key with the name ‘ID’. For all example stacks I use pay per request billing mode and removal policy to destroy the table when the stack is taken down. Finally, we can specify the table name to easily find it.

    // The DynamoDB table we will be written to by the lambda function
    const dynamoTable = new dynamodb.Table(this, 'DynamoTable', {
      partitionKey: {name:'ID', type: dynamodb.AttributeType.STRING},
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
      removalPolicy: RemovalPolicy.DESTROY,
      tableName: 'lambda-schedular-dynamo-table'
    });

Next, we have the lambda function infrastructure code. A Nodejs function is used and the entry set as our lambda function code with the name of the handler being main. timeout for this could be really short but as an example it is just set as 3 seconds, this is the max the lambda would run for before a forced exit. In the environment section 2 parameters are passed into the lambda, the important one here is the table name, this lets the lambda know what table to write its data to.

    // Function called by schedular to Monitor the site
    const triggeredEventFn = new lambdaNode.NodejsFunction(this, 'TriggerEventHandler', {
      runtime: lambda.Runtime.NODEJS_14_X,
      entry: 'lambda/writeToDynamo.ts',
      handler: 'main',
      timeout: Duration.seconds(3),
      bundling: {
        externalModules: [
        ],
        nodeModules: [
        ],
        minify: true
      },
      environment: {
        "tableName": dynamoTable.tableName,
        region: process.env.CDK_DEFAULT_REGION!
      }
    });

Finally, we can create the scheduler itself. The event bridge is setup as a simple rule with a rule name and schedule, the schedule in this case is done with a rate expression but can also be made with a cron expression.

The other two parts are then also hooked together. The lambda function is given write access to the dynamo table. A target, of the lambda function, is also set for the event.

    // Schedular from event bridge
    const eventBridgeSchedular = new events.Rule(this, 'EventBridgeSchedular', {
      ruleName: 'eventBridgeSchedular',
      schedule: events.Schedule.rate(Duration.minutes(1))
    });

    // Give the lambda permissions to write to the dynamo table
    dynamoTable.grantWriteData(triggeredEventFn);
    // Add the lambda as a target of the schedular event
    eventBridgeSchedular.addTarget(new targets.LambdaFunction(triggeredEventFn));

The Lambda function itself is made up of a simple function that can write a random number and the date to a dynamo table

import { Context, Callback } from 'aws-lambda';
import {DynamoDB} from 'aws-sdk';

export async function main(event: any, _context: Context, callback: Callback) {

    try {
        // Write a random number to the dynamo table
        const documentClient = new DynamoDB.DocumentClient();
        const params: DynamoDB.DocumentClient.PutItemInput = {
            TableName : process.env.tableName!,
            Item: {
                ID: Math.floor(Math.random() * Math.floor(10000000)).toString(),
                time: Date.now().toString(),
            }
        }
        const data = await documentClient.put(params).promise();
        console.log('Added ', data);
        return;
    } catch (error) {
        console.log('error', error)
    }
}

On deploying this stack, the event schedular will start to write to the dynamo table, the results can be seen by scanning the table in the AWS console.

Checkout the project on GitHub
Checkout all example stacks



Categories: AWS

Tags: , , , , ,

2 replies

Trackbacks

  1. AWS CDK Fargate Schedular Write to DynamoDB – greenchapel.dev
  2. AWS CDK Costs to Slack – greenchapel.dev

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: