How to Trigger an AWS Lambda from Kinesis

In this post we are going to use the AWS CDK to build an AWS Lambda Function that triggers from an AWS Kinesis Stream.

All of the code can be found in this repository.

Setup

We need to run a few commands to setup our CDK app.

mkdir how-…


This content originally appeared on DEV Community and was authored by Alex Kates

In this post we are going to use the AWS CDK to build an AWS Lambda Function that triggers from an AWS Kinesis Stream.

All of the code can be found in this repository.

Setup

We need to run a few commands to setup our CDK app.

mkdir how-to-trigger-lambda-from-kinesis
cd how-to-trigger-lambda-from-kinesis
npx cdk init app --language typescript

This should give you the following directory structure.
Directory structure after running the CDK init command

Also make sure you have your AWS CLI configured. For more information follow the AWS CLI quickstart guide.

Create a Kinesis Stream

Install the Kinesis CDK package.

npm i @aws-cdk/aws-kinesis

Open lib/how-to-trigger-lambda-from-kinesis-stack.ts, add a new Kinesis stream, and deploy.

import * as cdk from '@aws-cdk/core';
import * as kinesis from '@aws-cdk/aws-kinesis';

export class HowToTriggerLambdaFromKinesisStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const stream = new kinesis.Stream(this, 'MyKinesisStream', {
      streamName: 'MyKinesisStream',
    });
  }
}

Nice! Let's deploy the stack.

npm run cdk deploy

Create a Lambda

Install the Lambda CDK package.

npm i @aws-cdk/aws-lambda

Deploying a Lambda function requires bootstrapping your CDK app which gives us an S3 bucket where our Lambda's source code will live. This is a one time operation.

npm run cdk bootstrap

Create src/index.js and paste the following code

exports.handler = async (event) => {
  event.Records.forEach((record) => {
    console.log('Record: %j', record);
  });
};

Open lib/how-to-trigger-lambda-from-kinesis-stack.ts, add a new Lambda function, and deploy.

import * as cdk from '@aws-cdk/core';
import * as kinesis from '@aws-cdk/aws-kinesis';
import * as lambda from '@aws-cdk/aws-lambda';

export class HowToTriggerLambdaFromKinesisStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const stream = new kinesis.Stream(this, 'MyKinesisStream', {
      streamName: 'MyKinesisStream',
    });

    const lambdaFunction = new lambda.Function(this, 'Function', {
      code: lambda.Code.fromAsset('src'),
      handler: 'index.handler',
      functionName: 'KinesisMessageHandler',
      runtime: lambda.Runtime.NODEJS_12_X,
    });
  }
}

Deploy again ...

npm run cdk deploy

You may need to confirm some IAM changes.
IAM changes after deploying stack with Lambda function

Create the Event Source

Install the Lambda Event Sources CDK package.

npm i @aws-cdk/aws-lambda-event-sources

Open lib/how-to-trigger-lambda-from-kinesis-stack.ts, add a new KinesisEventSource to the Lambda Function.

import * as cdk from '@aws-cdk/core';
import * as kinesis from '@aws-cdk/aws-kinesis';
import * as lambda from '@aws-cdk/aws-lambda';
import * as lambdaEventSources from '@aws-cdk/aws-lambda-event-sources';

export class HowToTriggerLambdaFromKinesisStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const stream = new kinesis.Stream(this, 'MyKinesisStream', {
      streamName: 'MyKinesisStream',
    });

    const lambdaFunction = new lambda.Function(this, 'Function', {
      code: lambda.Code.fromAsset('src'),
      handler: 'index.handler',
      functionName: 'KinesisMessageHandler',
      runtime: lambda.Runtime.NODEJS_12_X,
    });

    const eventSource = new lambdaEventSources.KinesisEventSource(stream, {
      startingPosition: lambda.StartingPosition.TRIM_HORIZON,
    });

    lambdaFunction.addEventSource(eventSource);
  }
}

One more deployment ...

npm run cdk deploy

One last time, you may need to approve IAM changes.

IAM changes approval after deploying the Kinesis event source

Testing

We are going to make use of the AWS CLI to test our stack.

First, we need the Stream name of our Kinesis queue, which you can get using the following command

aws kinesis list-streams

Next, using the Stream name from the previous command, use the AWS CLI to send a new message to MyKinesisStream. Please refer to the Kinesis docs for information about these arguments.

aws kinesis put-record \
  --data "aGVsbG8sIHdvcmxk" \
  --stream-name MyKinesisStream \
  --partition-key pk1

Verify that the Lambda executed by looking in CloudWatch. Find the LogGroup named /aws/lambda/KinesisMessageHandler and open up the latest LogStream. You should see some log messages that look similar to this.

CloudWatch results after sending a new Kinesis record

Clean Up

Don't forget to delete your stack when you are finished!

npm run cdk destroy

Thanks for reading! If you found this useful, please follow me here
https://dev.to/thealexkates
https://twitter.com/thealexkates


This content originally appeared on DEV Community and was authored by Alex Kates


Print Share Comment Cite Upload Translate Updates
APA

Alex Kates | Sciencx (2021-07-14T13:03:17+00:00) How to Trigger an AWS Lambda from Kinesis. Retrieved from https://www.scien.cx/2021/07/14/how-to-trigger-an-aws-lambda-from-kinesis/

MLA
" » How to Trigger an AWS Lambda from Kinesis." Alex Kates | Sciencx - Wednesday July 14, 2021, https://www.scien.cx/2021/07/14/how-to-trigger-an-aws-lambda-from-kinesis/
HARVARD
Alex Kates | Sciencx Wednesday July 14, 2021 » How to Trigger an AWS Lambda from Kinesis., viewed ,<https://www.scien.cx/2021/07/14/how-to-trigger-an-aws-lambda-from-kinesis/>
VANCOUVER
Alex Kates | Sciencx - » How to Trigger an AWS Lambda from Kinesis. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/14/how-to-trigger-an-aws-lambda-from-kinesis/
CHICAGO
" » How to Trigger an AWS Lambda from Kinesis." Alex Kates | Sciencx - Accessed . https://www.scien.cx/2021/07/14/how-to-trigger-an-aws-lambda-from-kinesis/
IEEE
" » How to Trigger an AWS Lambda from Kinesis." Alex Kates | Sciencx [Online]. Available: https://www.scien.cx/2021/07/14/how-to-trigger-an-aws-lambda-from-kinesis/. [Accessed: ]
rf:citation
» How to Trigger an AWS Lambda from Kinesis | Alex Kates | Sciencx | https://www.scien.cx/2021/07/14/how-to-trigger-an-aws-lambda-from-kinesis/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.