You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: msk-lambda-iam-node-sam/README.md
+42-32Lines changed: 42 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,48 +1,55 @@
1
1
# Node.js Lambda Kafka Consumer with IAM Auth, using SAM
2
2
3
-
This pattern is an example of a Lambda function that will consume messages from an Managed Streaming for Kafka(MSK) topic, where the MSK Cluster has been configured to use IAM Authentication. This pattern assumes you already have an MSK Cluster with a Topic configured, if you need a sample pattern to deploy an MSK Cluster either in Provisioned or Serverless modes please see the [msk-cfn-sasl-lambda pattern](https://serverlessland.com/patterns/msk-cfn-sasl-lambda)
3
+
This pattern is an example of a Lambda function that consumes messages from an Amazon Managed Streaming for Kafka (Amazon MSK) topic, where the MSK Cluster has been configured to use IAM authentication. This pattern assumes you already have an MSK cluster with a topic configured, if you need a sample pattern to deploy an MSK cluster either in Provisioned or Serverless modes please see the [msk-cfn-sasl-lambda pattern](https://serverlessland.com/patterns/msk-cfn-sasl-lambda).
4
4
5
-
This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders.
5
+
This project contains source code and supporting files for a serverless application that you can deploy with the AWS Serverless Application Model (AWS SAM) CLI. It includes the following files and folders.
6
6
7
7
- HandlerKafka - Code for the application's Lambda function.
8
8
- events - Invocation events that you can use to invoke the function.
9
-
- template.yaml - A template that defines the application's AWS resources.
9
+
- template.yaml - An AWS SAM template that defines the application's AWS resources.
10
10
11
-
The application creates a Lambda function that will listen to Kafka messages on a topic linked to an MSK Cluster. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
11
+
The application creates a Lambda function that listens to Kafka messages on a topic of an MSK Cluster. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
12
12
13
13
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.
14
14
15
15
## Requirements
16
16
17
17
*[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.
18
18
*[AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* Create MSK cluster and topic that will be used for testing. It is important to create the topic before deploying the Lambda function, otherwise the event source mapping will stay disabled.
22
22
23
23
## Deploy the sample application
24
24
25
-
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
25
+
The AWS SAM CLI is a serverless tool for building and testing Lambda applications. It uses Docker to locally test your functions in an Amazon Linux environment that resembles the Lambda execution environment. It can also emulate your application's build environment and API.
26
26
27
-
To use the SAM CLI, you need the following tools.
27
+
To use the AWS SAM CLI, you need the following tools.
28
28
29
-
* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
30
-
* Node.js - [Install Node.js 18](https://nodejs.org/en/), including the NPM package management tool.
29
+
* AWS SAM CLI - [Install the AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
31
30
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
32
31
33
-
To build and deploy your application for the first time, run the following in your shell:
34
-
35
-
```bash
36
-
sam build
37
-
sam deploy --guided
38
-
```
32
+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts:
41
+
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
42
+
```
43
+
sam build
44
+
sam deploy --guided
45
+
```
41
46
47
+
1. During the prompts:
42
48
* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name.
43
49
* **AWS Region**: The AWS region you want to deploy your app to.
44
-
***Parameter MSKClusterName**: The name of the MSKCluster
45
-
***Parameter MSKClusterId**: The unique ID of the MSKCluster
50
+
* **Parameter MSKClusterName**: The name of the MSKCluster, eg. msk-test-cluster
51
+
52
+
* **Parameter MSKClusterId**: The unique ID of the MSKCluster, eg. a4e132c8-6ad0-4334-a313-123456789012-s2
46
53
* **Parameter MSKTopic**: The Kafka topic on which the lambda function will listen on
47
54
* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
48
55
* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command.
@@ -51,29 +58,36 @@ The first command will build the source of your application. The second command
51
58
* **SAM configuration file [samconfig.toml]**: Name of the configuration file to store configuration information locally
52
59
* **SAM configuration environment [default]**: Environment for storing deployment information locally
53
60
54
-
You should get a message "Successfully created/updated stack - <StackName> in <Region>" if all goes well
61
+
You should get a message "Successfully created/updated stack - <StackName> in <Region>" if all goes well.
62
+
63
+
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.
64
+
65
+
## How it works
66
+
67
+
This pattern creates a Lambda function along with a Lambda Event Source Mapping(ESM) resource. This maps a Kafka topic on an MSK Cluster as a trigger to a Lambda function. The ESM takes care of polling the Kafka topic and then invokes the Lambda function with a batch of messages.
55
68
56
69
## Test the sample application
57
70
58
-
Once the lambda function is deployed, send some Kafka messages on the topic that the lambda function is listening on, on the MSK server.
71
+
Once the Lambda function is deployed, send some Kafka messages to the topic that you configured in the Lambda function trigger.
59
72
60
73
Either send at least 10 messages or wait for 300 seconds (check the values of BatchSize: 10 and MaximumBatchingWindowInSeconds: 300 in the template.yaml file)
61
74
62
-
Then check Cloudwatch logs and you should see messages for the Cloudwatch Log Group with the name of the deployed Lambda function.
75
+
Then check Amazon CloudWatch logs and you should see messages in the CloudWatch Log Group with the name of the deployed Lambda function.
63
76
64
-
The lambda code parses the Kafka messages and outputs the fields in the Kafka messages to Cloudwatch logs
77
+
The Lambda code parses the Kafka messages and outputs the fields in the Kafka messages to CloudWatch logs.
65
78
66
-
A single lambda function receives a batch of messages. The messages are received as a map with each key being a combination of the topic and the partition, as a single batch can receive messages from multiple partitions.
79
+
A single Lambda function receives a batch of messages. The messages are received as a map with each key being a combination of the topic and the partition, as a single batch can receive messages from multiple partitions.
67
80
68
-
Each key has a list of messages. Each Kafka message has the following properties - Topic, Partition, Offset, TimeStamp, TimeStampType, Key and Value
81
+
Each key has a list of messages. Each Kafka message has the following properties - `Topic`, `Partition`, `Offset`, `TimeStamp`, `TimeStampType`, `Key`, and `Value`.
69
82
70
-
The Key and Value are base64 encoded and have to be decoded. A message can also have a list of headers, each header having a key and a value.
83
+
The `Key` and `Value` are base64 encoded and have to be decoded. A message can also have a list of headers, each header having a key and a value.
84
+
85
+
The code in this example prints out the fields in the Kafka message and also decrypts the key and the value and logs them to CloudWatch logs.
71
86
72
-
The code in this example prints out the fields in the Kafka message and also decrypts the key and the value and logs them in Cloudwatch logs.
73
87
74
88
### Local development
75
89
76
-
**Invoking function locally using sam local**
90
+
**You can invoke the function locally using `sam local`**
"title": "AWS Lambda function subscribed to an Amazon MSK topic using IAM auth",
3
+
"description": "Creates a Lambda function that uses an Amazon MSK topic as an event source with IAM authentication.",
4
+
"language": "Node.js",
5
+
"level": "200",
6
+
"framework": "SAM",
7
+
"introBox": {
8
+
"headline": "How it works",
9
+
"text": [
10
+
"This pattern provides a Lambda function along with an Event Source Mapping to a Kafka topic.",
11
+
"It requires that you already have an Amazon Managed Streaming for Kafka(Amazon MSK) cluster setup with a topic created. If you don't already have an MSK cluster ",
12
+
"you can use the example in this pattern https://serverlessland.com/patterns/msk-cfn-sasl-lambda (linked in the resources) to deploy a cluster.",
13
+
"This pattern works with either a Provisioned or Serverless MSK cluster as long as the cluster is configured to use IAM authentication. ",
14
+
"For detailed deployment instructions instructions see the README "
0 commit comments