Skip to content

Commit 2d1c790

Browse files
authored
fix: NodeJS sample (#1840)
* fix: NodeJS sample * feat: add build sample to CI * fix: eslint * fix: lint * fix: typo * fix: logging -> console * fix: logging -> console * fix: put back terraform as a deployment option * fix: forgotten @aws-sdk/client-sts * lint: terraform * fix: PR comments * fix: typo * fix: packaging * fix: PR comment, removing dependency
1 parent 592c5b0 commit 2d1c790

File tree

16 files changed

+4648
-2748
lines changed

16 files changed

+4648
-2748
lines changed

.github/workflows/ci-nodejs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ jobs:
3737
working-directory: nodejs
3838
- run: npm test
3939
working-directory: nodejs
40+
- name: Build AWS SDK Sample
41+
run: npm run build
42+
working-directory: nodejs/sample-apps/aws-sdk

nodejs/package-lock.json

Lines changed: 4445 additions & 2679 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
build
2+
cdk.out
3+
bin
4+
lib

nodejs/sample-apps/aws-sdk/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ module.exports = {
44
"node": true
55
},
66
...require('../../eslint.config.js')
7-
}
7+
}

nodejs/sample-apps/aws-sdk/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cdk.out
2+
build

nodejs/sample-apps/aws-sdk/README.md

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,77 @@
1-
# AWS SDK Sample Application
1+
# 🧪 AWS SDK Sample Application with OpenTelemetry
22

3-
**This Sample App is a work-in-progress because it depends on the OpenTelemetry public layer. The public layer will not be published
3+
This sample demonstrates how to use the **AWS SDK** within a Lambda function with **OpenTelemetry (OTel)** capabilities. It performs a simple `STS GetCallerIdentity` call for demonstration purposes and outputs Otel telemetry data to logs.
44

5-
This sample application demonstrates usage of the AWS SDK. To try it out, make sure the collector and nodejs Lambda
6-
layers are built.
5+
Example log output:
6+
![Sample Log Output](./sampleLogOutput.png)
77

8-
In [collector](../../../collector), run `make package`.
9-
In [nodejs](../../), run `npm install`.
8+
---
109

11-
Then, run `terraform init` and `terraform apply`. The lambda function will be initialized and the URL for an API Gateway invoking the Lambda
12-
will be displayed at the end. Send a request to the URL in a browser or using curl to execute the function. Then,
13-
navigate to the function's logs [here](https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logStream:group=%252Faws%252Flambda%252Fhello-nodejs).
14-
You will see a log stream with an event time corresponding to when you issued the request - open it and you can find
15-
information about the exported spans in the log stream.
10+
## 🚀 Deploy Using AWS CDK
11+
12+
Follow these steps to deploy the demo stack via CDK:
13+
14+
1. **Install dependencies**
15+
`npm install`
16+
17+
2. **Bootstrap your AWS environment (if not done already)**
18+
`npx cdk bootstrap`
19+
20+
3. **Deploy the stack**
21+
`npx cdk deploy OtelSampleLambdaStack`
22+
23+
---
24+
25+
## 🚀 Deploy Using Terraform
26+
27+
Follow these steps to deploy the demo stack via Terraform:
28+
29+
1. **Install dependencies**
30+
`npm install`
31+
32+
2. **Build the Lambda function artifact**
33+
`npm run build`
34+
35+
3. **Move to deploy/wrapper folder**
36+
`cd deploy/wrapper`
37+
38+
4. **Terraform init**
39+
`terraform init`
40+
41+
5. **Terraform apply**
42+
`terraform apply`
43+
44+
---
45+
46+
## 🛠️ Manual Deployment via AWS Console
47+
48+
If you'd prefer to deploy manually:
49+
50+
1. **Install dependencies**
51+
`npm install`
52+
53+
2. **Build the Lambda function artifact**
54+
`npm run build`
55+
56+
3. **Create a new AWS Lambda function**
57+
- Runtime: Select the latest `nodejs.x`
58+
59+
4. **Upload the artifact**
60+
- File location: `build/function.zip`
61+
62+
5. **Set the following environment variables**
63+
```
64+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/
65+
OTEL_TRACES_EXPORTER=console
66+
OTEL_METRICS_EXPORTER=console
67+
OTEL_LOG_LEVEL=INFO
68+
OTEL_TRACES_SAMPLER=always_on
69+
AWS_LAMBDA_EXEC_WRAPPER=/opt/otel-handler
70+
```
71+
6. **Attach the Node.js instrumentation layer**
72+
- Refer to the latest ARN in the OpenTelemetry Lambda releases, ie:
73+
https://github.com/open-telemetry/opentelemetry-lambda/releases/tag/layer-nodejs%2F0.14.0
74+
75+
7. **Attach the OpenTelemetry Collector layer**
76+
- Refer to the ARN in the release notes, ie:
77+
https://github.com/open-telemetry/opentelemetry-lambda/releases/tag/layer-collector%2F0.15.0

nodejs/sample-apps/aws-sdk/bin/app.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
import * as cdk from 'aws-cdk-lib';
3+
import { OtelSampleLambdaStack } from '../lib/otel-sample-lambda-stack';
4+
5+
const app = new cdk.App();
6+
new OtelSampleLambdaStack(app, 'OtelSampleLambdaStack');

nodejs/sample-apps/aws-sdk/cdk.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "npm run build"
3+
}

nodejs/sample-apps/aws-sdk/deploy/wrapper/main.tf

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
locals {
2+
collector_layer_arn = "arn:aws:lambda:${data.aws_region.current.name}:${var.account_id}:layer:opentelemetry-collector-arm64-${var.collector_layer_version}:1"
3+
sdk_layer_arn = "arn:aws:lambda:${data.aws_region.current.name}:${var.account_id}:layer:opentelemetry-nodejs-${var.nodejs_layer_version}:1"
4+
}
5+
6+
data "aws_region" "current" {}
7+
18
module "hello-lambda-function" {
29
source = "terraform-aws-modules/lambda/aws"
3-
version = ">= 2.24.0"
10+
version = "7.21.0"
411

512
architectures = compact([var.architecture])
613
function_name = var.name
@@ -14,33 +21,20 @@ module "hello-lambda-function" {
1421
timeout = 20
1522

1623
layers = compact([
17-
var.collector_layer_arn,
18-
var.sdk_layer_arn
24+
local.collector_layer_arn,
25+
local.sdk_layer_arn
1926
])
2027

2128
environment_variables = {
2229
AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-handler"
23-
OTEL_TRACES_EXPORTER = "logging"
24-
OTEL_METRICS_EXPORTER = "logging"
30+
OTEL_TRACES_EXPORTER = "console"
31+
OTEL_METRICS_EXPORTER = "console"
2532
OTEL_LOG_LEVEL = "DEBUG"
2633
OTEL_EXPORTER_OTLP_ENDPOINT = "http://localhost:4318/"
2734
OTEL_TRACES_SAMPLER = "always_on"
2835
}
2936

3037
tracing_mode = var.tracing_mode
31-
32-
attach_policy_statements = true
33-
policy_statements = {
34-
s3 = {
35-
effect = "Allow"
36-
actions = [
37-
"s3:ListAllMyBuckets"
38-
]
39-
resources = [
40-
"*"
41-
]
42-
}
43-
}
4438
}
4539

4640
module "api-gateway" {

nodejs/sample-apps/aws-sdk/deploy/wrapper/variables.tf

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@ variable "name" {
44
default = "hello-nodejs-awssdk"
55
}
66

7-
variable "collector_layer_arn" {
7+
variable "account_id" {
88
type = string
9-
description = "ARN for the Lambda layer containing the OpenTelemetry collector extension"
10-
// TODO(anuraaga): Add default when a public layer is published.
11-
}
12-
13-
variable "sdk_layer_arn" {
14-
type = string
15-
description = "ARN for the Lambda layer containing the OpenTelemetry NodeJS Wrapper"
16-
// TODO(anuraaga): Add default when a public layer is published.
9+
description = "AWS account ID where the Lambda layers are published"
10+
default = "184161586896"
1711
}
1812

1913
variable "tracing_mode" {
@@ -25,12 +19,23 @@ variable "tracing_mode" {
2519
variable "architecture" {
2620
type = string
2721
description = "Lambda function architecture, valid values are arm64 or x86_64"
28-
default = "x86_64"
22+
default = "arm64"
2923
}
3024

3125
variable "runtime" {
3226
type = string
3327
description = "NodeJS runtime version used for sample Lambda Function"
34-
default = "nodejs18.x"
28+
default = "nodejs22.x"
3529
}
3630

31+
variable "collector_layer_version" {
32+
type = string
33+
description = "Collector layer version, see latest releases here: https://github.com/open-telemetry/opentelemetry-lambda/releases"
34+
default = "0_15_0"
35+
}
36+
37+
variable "nodejs_layer_version" {
38+
type = string
39+
description = "Node.js layer version, see latest releases here: https://github.com/open-telemetry/opentelemetry-lambda/releases"
40+
default = "0_14_0"
41+
}

0 commit comments

Comments
 (0)