Skip to content

Commit 01cade1

Browse files
committed
fix: NodeJS sample
1 parent 3af180d commit 01cade1

File tree

15 files changed

+156
-158
lines changed

15 files changed

+156
-158
lines changed

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cdk.out
2+
build
Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,56 @@
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+
## 🛠️ Manual Deployment via AWS Console
26+
27+
If you'd prefer to deploy manually:
28+
29+
1. **Install dependencies**
30+
`npm install`
31+
32+
2. **Build the Lambda function artifact**
33+
`npm run build`
34+
35+
3. **Create a new AWS Lambda function**
36+
- Runtime: Select the latest `nodejs.x`
37+
38+
4. **Upload the artifact**
39+
- File location: `build/function.zip`
40+
41+
5. **Set the following environment variables**
42+
```
43+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/
44+
OTEL_TRACES_EXPORTER=logging
45+
OTEL_METRICS_EXPORTER=logging
46+
OTEL_LOG_LEVEL=INFO
47+
OTEL_TRACES_SAMPLER=always_on
48+
AWS_LAMBDA_EXEC_WRAPPER=/opt/otel-handler
49+
```
50+
6. **Attach the Node.js instrumentation layer**
51+
- Refer to the latest ARN in the OpenTelemetry Lambda releases, ie:
52+
https://github.com/open-telemetry/opentelemetry-lambda/releases/tag/layer-nodejs%2F0.14.0
53+
54+
7. **Attach the OpenTelemetry Collector layer**
55+
- Refer to the ARN in the release notes, ie:
56+
https://github.com/open-telemetry/opentelemetry-lambda/releases/tag/layer-collector%2F0.15.0
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');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "npm run build && npx ts-node bin/app.ts"
3+
}

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

Lines changed: 0 additions & 53 deletions
This file was deleted.

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
APIGatewayProxyEvent,
3+
APIGatewayProxyResult,
4+
Context,
5+
} from 'aws-lambda';
6+
7+
import { STSClient, GetCallerIdentityCommand } from '@aws-sdk/client-sts';
8+
9+
const sts = new STSClient({});
10+
11+
export const handler = async (
12+
_event: APIGatewayProxyEvent,
13+
_context: Context
14+
): Promise<APIGatewayProxyResult> => {
15+
console.info('Serving lambda request.');
16+
17+
try {
18+
const result = await sts.send(new GetCallerIdentityCommand({}));
19+
20+
const response: APIGatewayProxyResult = {
21+
statusCode: 200,
22+
body: JSON.stringify({
23+
message: 'Caller identity retrieved successfully',
24+
identity: {
25+
Account: result.Account,
26+
Arn: result.Arn,
27+
UserId: result.UserId,
28+
},
29+
}),
30+
};
31+
return response;
32+
} catch (error) {
33+
console.error('Error retrieving caller identity:', error);
34+
return {
35+
statusCode: 500,
36+
body: 'Internal Server Error',
37+
};
38+
}
39+
};

0 commit comments

Comments
 (0)