Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kinesis-lambda-terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lambda.zip
6 changes: 3 additions & 3 deletions kinesis-lambda-terraform/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AWS Kinesis Data Streams to AWS Lambda
# Amazon Kinesis Data Streams to AWS Lambda
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Fix to correct service name.


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.
This pattern creates an AWS Kinesis Data Streams, a stream consumer, and an AWS Lambda function. When data is added to the stream, the Lambda function is invoked.

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)

Expand Down Expand Up @@ -83,4 +83,4 @@ When you are logged in, you can generate data for your stream test.
```

----
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
58 changes: 52 additions & 6 deletions kinesis-lambda-terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.22"
version = "~> 5.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Older version does not support Node.js 22 runtime. So I updated the version to v5.

}
}

Expand All @@ -17,12 +17,20 @@ resource "aws_kinesis_stream" "sample_stream" {
shard_count = 1
retention_period = 24
}

data "archive_file" "lambda_zip_file" {
type = "zip"
source_file = "${path.module}/src/app.js"
output_path = "${path.module}/lambda.zip"
}

resource "aws_lambda_function" "sample_lambda" {
filename = "sample_lambda.zip" # Path to your Lambda code ZIP file
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: The error occurred.

╷
│ Error: reading ZIP file (sample_lambda.zip): open sample_lambda.zip: no such file or directory
│ 
│   with aws_lambda_function.sample_lambda,
│   on main.tf line 20, in resource "aws_lambda_function" "sample_lambda":
│   20: resource "aws_lambda_function" "sample_lambda" {
│ 
╵

filename = data.archive_file.lambda_zip_file.output_path
source_code_hash = data.archive_file.lambda_zip_file.output_base64sha256
function_name = "sample-lambda"
role = aws_iam_role.lambda_role.arn
handler = "index.handler"
runtime = "nodejs16.x" # Change to your preferred runtime
handler = "app.handler"
runtime = "nodejs22.x" # Change to your preferred runtime
}
resource "aws_iam_role" "lambda_role" {
name = "lambda-role"
Expand All @@ -40,6 +48,44 @@ resource "aws_iam_role" "lambda_role" {
})
}

resource "aws_iam_policy" "lambda_kinesis_policy" {
name = "lambda-kinesis-policy"

policy = jsonencode(
{
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"kinesis:GetRecords",
"kinesis:GetShardIterator",
"kinesis:DescribeStream",
"kinesis:DescribeStreamSummary",
"kinesis:ListShards",
"kinesis:ListStreams"
],
Resource = aws_kinesis_stream.sample_stream.arn
},
{
Effect = "Allow",
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
Resource = "arn:aws:logs:*:*:*"
}
]
}
)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: The error occurred.

╷
│ Error: creating Lambda Event Source Mapping (arn:aws:kinesis:us-east-1:000000000000:stream/sample-stream): operation error Lambda: CreateEventSourceMapping, https response error StatusCode: 400, RequestID: 8dc4328e-eea3-4e1e-b3e5-601eca619082, InvalidParameterValueException: Cannot access stream arn:aws:kinesis:us-east-1:000000000000:stream/sample-stream. Please ensure the role can perform the GetRecords, GetShardIterator, DescribeStream, DescribeStreamSummary, ListShards, and ListStreams Actions on your stream.
│ 
│   with aws_lambda_event_source_mapping.sample_mapping,
│   on main.tf line 51, in resource "aws_lambda_event_source_mapping" "sample_mapping":
│   51: resource "aws_lambda_event_source_mapping" "sample_mapping" {
│ 
╵


resource "aws_iam_role_policy_attachment" "lambda_kinesis_policy_attachment" {
role = aws_iam_role.lambda_role.name
policy_arn = aws_iam_policy.lambda_kinesis_policy.arn
}

resource "aws_lambda_event_source_mapping" "sample_mapping" {
event_source_arn = aws_kinesis_stream.sample_stream.arn
function_name = aws_lambda_function.sample_lambda.arn
Expand All @@ -52,6 +98,6 @@ output "kinesis_data_stream" {
}

output "consumer_function" {
value = aws_lambda_function.sample_function.arn
value = aws_lambda_function.sample_lambda.arn
description = "Consumer Function function name"
}
}