Skip to content

Commit e4f96d0

Browse files
authored
Merge pull request aws-samples#1609 from awsrahulrsr/main
2 parents 3f5f32c + ab105cc commit e4f96d0

4 files changed

Lines changed: 216 additions & 0 deletions

File tree

kinesis-lambda-terraform/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# AWS Kinesis Data Streams to AWS Lambda
2+
3+
This pattern creates an AWS Kinesis Data Stream, a stream consumer, and an AWS Lambda function. When data is added to the stream, the Lambda function is invoked.
4+
5+
Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/kinesis-to-lambda-terraform](https://serverlessland.com/patterns/kinesis-to-lambda-terraform)
6+
7+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
8+
9+
## Requirements
10+
11+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed
15+
16+
## Deployment Instructions
17+
18+
1. Clone the project to your local working directory
19+
20+
```sh
21+
git clone https://github.com/aws-samples/serverless-patterns/
22+
```
23+
24+
2. Change the working directory to this pattern's directory
25+
26+
```sh
27+
cd serverless-patterns/kinesis-lambda-terraform
28+
```
29+
30+
1. From the command line, initialize terraform to to downloads and installs the providers defined in the configuration:
31+
```
32+
terraform init
33+
```
34+
1. From the command line, apply the configuration in the main.tf file:
35+
```
36+
terraform apply
37+
```
38+
1. During the prompts:
39+
40+
- Enter yes
41+
42+
## Testing
43+
44+
Use the Amazon Kinesis Data Generator for testing. The easiest way to use this tool is to use the [hosted generator](https://awslabs.github.io/amazon-kinesis-data-generator/web/producer.html) and follow the [setup instructions](https://awslabs.github.io/amazon-kinesis-data-generator/web/help.html).
45+
46+
After you have the generator configured, you should have a custom URL to generate data for your Kinesis data stream. In your configuration steps, you created a username and password. Log in to the generator using those credentials.
47+
48+
When you are logged in, you can generate data for your stream test.
49+
50+
1. Choose the region you deployed the application to
51+
2. For Stream/delivery stream, select your stream.
52+
3. For Records per second, keep the default value of 100.
53+
4. On the Template 1 tab, name the template Sensor1.
54+
5. Use the following template:
55+
```JSON
56+
{
57+
"sensorId": {{random.number(50)}},
58+
"currentTemperature": {{random.number(
59+
{
60+
"min":10,
61+
"max":150
62+
}
63+
)}},
64+
"status": "{{random.arrayElement(
65+
["OK","FAIL","WARN"]
66+
)}}"
67+
}
68+
```
69+
6. Choose Send Data.
70+
7. After several seconds, choose Stop Sending Data.
71+
72+
## Cleanup
73+
74+
1. Delete all created resources by terraform
75+
```bash
76+
terraform destroy
77+
```
78+
2. During the prompts:
79+
* Enter yes
80+
3. Confirm all created resources has been deleted
81+
```bash
82+
terraform show
83+
```
84+
85+
----
86+
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"title": "Kinesis Data Streams With Lambda Consumer",
3+
"description": "When data is loaded into Kinesis data stream, the configured lambda function is invoked.",
4+
"language": "",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern creates a Kinesis Data Stream and lambda function (consumer) with event source mapping. ",
11+
"When the data is loaded into the stream, the lambda function is invoked. "
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/kinesis-lambda-terraform",
17+
"templateURL": "serverless-patterns/kinesis-lambda-terraform",
18+
"projectFolder": "kinesis-lambda-terraform",
19+
"templateFile": "main.tf"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Kinesis Data Streams with Lambda consumers",
26+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html"
27+
},
28+
{
29+
"text": "Lambda event source mapping",
30+
"link": "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping.html"
31+
}
32+
]
33+
},
34+
"deploy": {
35+
"text": [
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the GitHub repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"Delete the stack: <code>terraform destroy</code>."
47+
]
48+
},
49+
"authors": [
50+
{
51+
"name": "Rahul Sringeri",
52+
"image": "",
53+
"bio": "Technical Account Manager at AWS for strategic accounts.",
54+
"linkedin": "",
55+
"twitter": ""
56+
}
57+
]
58+
}

kinesis-lambda-terraform/main.tf

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4.22"
6+
}
7+
}
8+
9+
required_version = ">= 0.14.9"
10+
}
11+
12+
provider "aws" {
13+
region = "us-east-1" # Change to your desired region
14+
}
15+
resource "aws_kinesis_stream" "sample_stream" {
16+
name = "sample-stream"
17+
shard_count = 1
18+
retention_period = 24
19+
}
20+
resource "aws_lambda_function" "sample_lambda" {
21+
filename = "sample_lambda.zip" # Path to your Lambda code ZIP file
22+
function_name = "sample-lambda"
23+
role = aws_iam_role.lambda_role.arn
24+
handler = "index.handler"
25+
runtime = "nodejs14.x" # Change to your preferred runtime
26+
}
27+
resource "aws_iam_role" "lambda_role" {
28+
name = "lambda-role"
29+
assume_role_policy = jsonencode({
30+
Version = "2012-10-17",
31+
Statement = [
32+
{
33+
Action = "sts:AssumeRole",
34+
Effect = "Allow",
35+
Principal = {
36+
Service = "lambda.amazonaws.com"
37+
}
38+
}
39+
]
40+
})
41+
}
42+
43+
resource "aws_lambda_event_source_mapping" "sample_mapping" {
44+
event_source_arn = aws_kinesis_stream.sample_stream.arn
45+
function_name = aws_lambda_function.sample_lambda.arn
46+
starting_position = "LATEST"
47+
}
48+
49+
output "kinesis_data_stream" {
50+
value = aws_kinesis_stream.sample_stream.arn
51+
description = "Kinesis data stream with shards"
52+
}
53+
54+
output "consumer_function" {
55+
value = aws_lambda_function.sample_function.arn
56+
description = "Consumer Function function name"
57+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//Sample lambda code
2+
3+
exports.handler = async (event) => {
4+
console.log('Received Kinesis records:', JSON.stringify(event, null, 2));
5+
// Process each Kinesis record
6+
event.Records.forEach((record) => {
7+
const payload = Buffer.from(record.kinesis.data, 'base64').toString('utf-8');
8+
console.log('Record payload:', payload);
9+
// Add your processing logic here
10+
});
11+
return {
12+
statusCode: 200,
13+
body: 'Processed Kinesis records successfully',
14+
};
15+
};

0 commit comments

Comments
 (0)