Skip to content

Commit d8be15b

Browse files
authored
Build Lambda Layer and Sample app with Terraform support (#41)
*Description of changes:* This PR includes 3 major changes 1. AWS Lambda Layer implementation for supporting Application Signals in AWS Lambda. 2. Lambda Sample App with AWS SDK to demonstrate Application Signals telemetry data generated by Lambda Layer. 3. Terraform script for deploy the sample app with Lambda layer built from the local package. *Test* 1. Traces <img width="1433" alt="image" src="https://github.com/user-attachments/assets/f2c0f44c-3370-4312-92ac-8fabb2281091"> By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent da31402 commit d8be15b

File tree

20 files changed

+491
-0
lines changed

20 files changed

+491
-0
lines changed

lambda-layer/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# AWS Distro Lambda Layer for NodeJS
2+
3+
This package includes the following three components
4+
5+
1. AWS Lambda Layer implementation for supporting Application Signals in AWS Lambda.
6+
2. Lambda Sample App with AWS SDK to demonstrate Application Signals telemetry data generated by Lambda Layer.
7+
3. Terraform script for deploy the sample app with Lambda layer built from the local package.
8+
9+
## Scripts to build everything
10+
11+
Run the following command in the root director to build both NodeJS Lambda Layer and Sample App.
12+
13+
**Note**: Make sure to install NodeJS 16+ to build the layer. Lower version of NodJS runtime is not supported by AWS Lambda
14+
anymore.
15+
16+
1. `./lambda-layer/build.sh`
17+
18+
## Steps to Build AWS Lambda Layer
19+
20+
1. Run the following command in the root director
21+
* `./scripts/build_and_install_distro.sh`
22+
2. Run to the following steps in `${root}/lambda-layer/packages/layer` director
23+
24+
```any
25+
npm install
26+
npm run compile
27+
```
28+
29+
## Steps to Deploy this Lambda function
30+
31+
Go to `${root}/lambda-layer/sample-apps/aws-sdk` director,
32+
33+
1. run `npm install`.
34+
2. run `terraform init` and `terraform apply`.
35+
36+
The lambda function(`aws-opentelemetry-distro-nodejs`) will be initialized and the URL for an API Gateway invoking the Lambda
37+
will be displayed at the end. Send a request to the URL in a browser or using curl to execute the function. Then,
38+
navigate to Lambda function's Monitoring page.
39+
You will see a log stream with an event time and Traces data corresponding to when you issued the request,
40+
open it and you can find
41+
information about the exported spans in the log stream.

lambda-layer/build.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
SOURCEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
6+
echo "Source directory: ${SOURCEDIR}"
7+
8+
# Navigate to the root project directory
9+
cd "${SOURCEDIR}/.."
10+
11+
# Install dependencies and compile all projects in the repository
12+
echo "Installing dependencies and compiling projects..."
13+
npm install
14+
npm run compile
15+
16+
# Build Lambda SDK layer
17+
cd ${SOURCEDIR}/packages/layer || exit
18+
rm -rf build
19+
rm -rf node_modules
20+
npm install || exit
21+
22+
# Build sample apps
23+
cd ${SOURCEDIR}/sample-apps/aws-sdk || exit
24+
rm -rf build
25+
rm -rf node_modules
26+
npm install || exit
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
"env": {
3+
"mocha": true,
4+
"node": true
5+
},
6+
...require('../../eslint.config.js')
7+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "@aws/aws-distro-opentelemetry-node-autoinstrumentatio-sdk-layer",
3+
"version": "0.0.1",
4+
"description": "Lambda Layer including AWS Distro OpenTelemetry SDK for supporting Amazon Application Signals",
5+
"repository": "aws-observability/aws-otel-js-instrumentation",
6+
"author": {
7+
"name": "Amazon Web Services",
8+
"url": "http://aws.amazon.com"
9+
},
10+
"homepage": "https://github.com/aws-observability/aws-otel-js-instrumentation/tree/main/aws-distro-opentelemetry-node-autoinstrumentation#readme",
11+
"license": "Apache-2.0",
12+
"engines": {
13+
"node": ">=16"
14+
},
15+
"publishConfig": {
16+
"access": "public"
17+
},
18+
"scripts": {
19+
"clean": "rimraf build/*",
20+
"lint": "eslint . --ext .ts",
21+
"lint:fix": "eslint . --ext .ts --fix",
22+
"prepare": "npm run compile",
23+
"compile": "tsc -p .",
24+
"postcompile": "copyfiles -F -e 'node_modules/@aws/aws-distro-opentelemetry-node-autoinstrumentation/node_modules/**' 'node_modules/**' build/workspace/nodejs && copyfiles -f 'scripts/*' build/workspace && copyfiles -f 'build/src/*' build/workspace && cd build/workspace && bestzip ../layer.zip *"
25+
},
26+
"keywords": [
27+
"awsdistroopentelemetry",
28+
"opentelemetry",
29+
"awslambda",
30+
"nodejs",
31+
"tracing",
32+
"profiling",
33+
"instrumentation"
34+
],
35+
"dependencies": {
36+
"@aws/aws-distro-opentelemetry-node-autoinstrumentation": "file:../../../aws-distro-opentelemetry-node-autoinstrumentation",
37+
"@opentelemetry/api": "1.9.0",
38+
"@opentelemetry/auto-instrumentations-node": "0.48.0",
39+
"@opentelemetry/auto-configuration-propagators": "0.2.0",
40+
"@opentelemetry/exporter-metrics-otlp-grpc": "0.52.1",
41+
"@opentelemetry/exporter-metrics-otlp-http": "0.52.1",
42+
"@opentelemetry/instrumentation": "0.52.1",
43+
"@opentelemetry/id-generator-aws-xray": "1.2.2",
44+
"@opentelemetry/propagator-aws-xray": "1.25.1",
45+
"@opentelemetry/core": "1.25.1",
46+
"@opentelemetry/sdk-trace-base": "1.25.1",
47+
"@opentelemetry/semantic-conventions": "1.25.1",
48+
"@opentelemetry/resources": "1.25.1",
49+
"@opentelemetry/exporter-trace-otlp-proto": "0.52.1",
50+
"@opentelemetry/exporter-zipkin": "1.25.1",
51+
"@opentelemetry/sdk-metrics": "1.25.1"
52+
}
53+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
export NODE_OPTIONS="${NODE_OPTIONS} --require /opt/wrapper.js"
3+
export LAMBDA_RESOURCE_ATTRIBUTES="cloud.region=$AWS_REGION,cloud.provider=aws,faas.name=$AWS_LAMBDA_FUNCTION_NAME,faas.version=$AWS_LAMBDA_FUNCTION_VERSION,faas.instance=$AWS_LAMBDA_LOG_STREAM_NAME,aws.log.group.names=$AWS_LAMBDA_LOG_GROUP_NAME";
4+
export OTEL_NODE_ENABLED_INSTRUMENTATIONS="http,aws-lambda,aws-sdk"
5+
6+
# - Set Application Signals configuration
7+
if [ "${OTEL_AWS_APPLICATION_SIGNALS_ENABLED}" = "true" ]; then
8+
export OTEL_METRICS_EXPORTER="none";
9+
export OTEL_LOGS_EXPORTER="none";
10+
fi
11+
12+
# - Append Lambda Resource Attributes to OTel Resource Attribute List
13+
if [ -z "${OTEL_RESOURCE_ATTRIBUTES}" ]; then
14+
export OTEL_RESOURCE_ATTRIBUTES=$LAMBDA_RESOURCE_ATTRIBUTES;
15+
else
16+
export OTEL_RESOURCE_ATTRIBUTES="$LAMBDA_RESOURCE_ATTRIBUTES,$OTEL_RESOURCE_ATTRIBUTES";
17+
fi
18+
19+
# - Set the service name
20+
if [ -z "${OTEL_SERVICE_NAME}" ]; then
21+
export OTEL_SERVICE_NAME=$AWS_LAMBDA_FUNCTION_NAME;
22+
fi
23+
if [[ $OTEL_RESOURCE_ATTRIBUTES != *"service.name="* ]]; then
24+
export OTEL_RESOURCE_ATTRIBUTES="service.name=${AWS_LAMBDA_FUNCTION_NAME},${OTEL_RESOURCE_ATTRIBUTES}"
25+
fi
26+
27+
exec "$@"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
require('@aws/aws-distro-opentelemetry-node-autoinstrumentation/register')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../tsconfig.base",
3+
"compilerOptions": {
4+
"rootDir": ".",
5+
"outDir": "build"
6+
},
7+
"include": [
8+
"src/**/*.ts",
9+
"test/**/*.ts"
10+
]
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
"env": {
3+
"mocha": true,
4+
"node": true
5+
},
6+
...require('../../eslint.config.js')
7+
}

0 commit comments

Comments
 (0)