Skip to content

Commit 6f11da6

Browse files
committed
Amend source pattern: msk-lambda-iam-go-sam
1 parent 474df3b commit 6f11da6

3 files changed

Lines changed: 76 additions & 71 deletions

File tree

msk-lambda-iam-go-sam/README.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@ This pattern is an example of a Lambda function that consumes messages from an A
44

55
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.
66

7-
8-
```bash
9-
.
10-
├── Makefile <-- Make to automate build
11-
├── README.md <-- This instructions file
12-
├── HandlerKafka <-- Source code for a lambda function
13-
│ ├── main.go <-- Lambda function code
14-
│ └── main_test.go <-- Unit tests
15-
└── template.yaml
16-
```
7+
- HandlerKafka - Code for the application's Lambda function.
8+
- events - Invocation events that you can use to invoke the function.
9+
- template.yaml - An AWS SAM template that defines the application's AWS resources.
1710

1811
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.
1912

@@ -25,7 +18,6 @@ Important: this application uses various AWS services and there are costs associ
2518
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
2619
* [Git installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
2720
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
28-
* [Docker installed](https://www.docker.com/community-edition)
2921
* [Golang](https://golang.org)
3022
* 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.
3123

@@ -41,39 +33,50 @@ To use the AWS SAM CLI, you need the following tools.
4133
In this example we use the built-in `sam build` to automatically download all the dependencies and package our build target.
4234
Read more about [SAM Build here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-build.html)
4335

44-
4536
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
4637
```
4738
git clone https://github.com/aws-samples/serverless-patterns.git
4839
```
4940
1. Change directory to the pattern directory:
5041
```
51-
cd msk-lambda-iam-python-sam
42+
cd msk-lambda-iam-go-sam
5243
```
5344
54-
1. The `sam build` command is wrapped inside of the `Makefile`.
45+
This solution uses the AWS Lambda [custom runtime](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) `provided.al2` which is the prefered way to run Go applications in Lambda. For more information, see [https://aws.amazon.com/blogs/compute/migrating-aws-lambda-functions-from-the-go1-x-runtime-to-the-custom-runtime-on-amazon-linux-2/](https://aws.amazon.com/blogs/compute/migrating-aws-lambda-functions-from-the-go1-x-runtime-to-the-custom-runtime-on-amazon-linux-2/).
5546
56-
```shell
57-
make
58-
```
59-
60-
AWS Lambda Golang runtime requires a flat folder with the executable generated on build step. SAM will use `CodeUri` property to know where to look up for the application:
47+
The AWS SAM template configures the `provided.al2` runtime and builds the Go function using the following syntax.
6148
6249
```yaml
6350
...
64-
FirstFunction:
65-
Type: AWS::Serverless::Function
66-
Properties:
67-
CodeUri: hello_world/
68-
...
51+
HelloWorldGoKafkaFunction:
52+
Type: AWS::Serverless::Function
53+
Metadata:
54+
BuildMethod: go1.x
55+
Properties:
56+
CodeUri: HandlerKafka/
57+
Handler: bootstrap
58+
Runtime: provided.al2
6959
```
60+
You can also use the managed Go.1 runtime by amending the AWS SAM template:
7061

71-
1. To deploy your application for the first time, run the following in your shell:
62+
AWS Lambda Golang runtime requires a flat folder with the executable generated on build step. SAM will use `CodeUri` property to know where to look up for the application:
7263

73-
```bash
74-
sam deploy --guided
64+
```yaml
65+
...
66+
HelloWorldGoKafkaFunction:
67+
Type: AWS::Serverless::Function
68+
Properties:
69+
CodeUri: HandlerKafka/
70+
Handler: handler
71+
Runtime: go1.x
7572
```
7673
74+
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
75+
```
76+
sam build
77+
sam deploy --guided
78+
```
79+
7780
1. During the prompts:
7881
* **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.
7982
* **AWS Region**: The AWS region you want to deploy your app to.

msk-lambda-iam-go-sam/example-pattern.json

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
{
1+
{
22
"title": "AWS Lambda function subscribed to an Amazon MSK topic using IAM auth",
33
"description": "Creates a Lambda function that uses an Amazon MSK topic as an event source with IAM authentication.",
44
"language": "Go",
55
"level": "200",
66
"framework": "SAM",
77
"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 "
15-
]
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. ",
12+
"If you don't already have an MSK cluster, 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."
15+
]
1616
},
1717
"gitHub": {
18-
"template": {
19-
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/msk-lambda-go-sam",
20-
"templateURL": "serverless-patterns/msk-lambda-go-sam",
21-
"projectFolder": "msk-lambda-go-sam",
22-
"templateFile": "template.yml"
23-
}
18+
"template": {
19+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/msk-lambda-iam-go-sam",
20+
"templateURL": "serverless-patterns/msk-lambda-iam-go-sam",
21+
"projectFolder": "msk-lambda-iam-go-sam",
22+
"templateFile": "template.yaml"
23+
}
2424
},
2525
"resources": {
26-
"bullets": [
27-
{
28-
"text": "Amazon MSK Cluster pattern",
29-
"link": "https://serverlessland.com/patterns/msk-cfn-sasl-lambda"
30-
},
31-
{
32-
"text": "Using AWS Lambda with Amazon MSK",
33-
"link": "https://docs.aws.amazon.com/lambda/latest/dg/with-msk.html"
34-
},
35-
{
36-
"text": "AWS CloudFormation Provisioned MSK cluster reference",
37-
"link": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-msk-cluster.html"
38-
},
39-
{
40-
"text": "AWS CloudFormation Serverless MSK cluster reference",
41-
"link": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-msk-serverlesscluster.html"
42-
}
43-
]
26+
"bullets": [
27+
{
28+
"text": "Amazon MSK cluster pattern",
29+
"link": "https://serverlessland.com/patterns/msk-cfn-sasl-lambda"
30+
},
31+
{
32+
"text": "Using AWS Lambda with Amazon MSK",
33+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/with-msk.html"
34+
},
35+
{
36+
"text": "AWS CloudFormation Provisioned MSK cluster reference",
37+
"link": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-msk-cluster.html"
38+
},
39+
{
40+
"text": "AWS CloudFormation Serverless MSK cluster reference",
41+
"link": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-msk-serverlesscluster.html"
42+
}
43+
]
4444
},
4545
"deploy": {
46-
"text": [
47-
"sam deploy --guided"
48-
]
46+
"text": [
47+
"sam deploy --guided"
48+
]
4949
},
5050
"testing": {
51-
"text": [
52-
"See the GitHub repo for detailed testing instructions."
53-
]
51+
"text": [
52+
"See the GitHub repo for detailed testing instructions."
53+
]
5454
},
5555
"cleanup": {
56-
"text": [
57-
"Delete the template: <code>sam delete</code>."
58-
]
56+
"text": [
57+
"Delete the template: <code>sam delete</code>."
58+
]
5959
},
6060
"authors": [
6161
{

msk-lambda-iam-go-sam/template.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ Globals:
1313
Resources:
1414
HelloWorldGoKafkaFunction:
1515
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
16+
Metadata:
17+
BuildMethod: go1.x
1618
Properties:
1719
CodeUri: HandlerKafka/
18-
Handler: handler
19-
Runtime: go1.x
20+
Handler: bootstrap
21+
Runtime: provided.al2
2022
Architectures:
2123
- x86_64
2224
Events:

0 commit comments

Comments
 (0)