Skip to content

Commit 1d3930b

Browse files
authored
chore: added local lambda test app (#314)
1 parent 2238fba commit 1d3930b

File tree

10 files changed

+156
-0
lines changed

10 files changed

+156
-0
lines changed

lambda-test/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

lambda-test/Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM public.ecr.aws/lambda/nodejs:22
2+
3+
# Copy function code
4+
COPY . ${LAMBDA_TASK_ROOT}
5+
# Uncomment if you want to install new agent
6+
#RUN npm install
7+
8+
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
9+
ENV NODE_OPTIONS='--inspect=0.0.0.0'
10+
CMD [ "app.handler" ]

lambda-test/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Local Lambda Tests
2+
This application uses a lambda runtime container to simulate running a Node.js function in Lambda with(out) the New Relic Node.js agent.
3+
4+
## Setup
5+
6+
```sh
7+
cp env.sample .env
8+
# If you want to change the type of handler invoked, change `FUNCTION_MODE`
9+
# Supported values: `async`, `context`, `cb`, `streaming`.
10+
# If you want to run lambda without agent set `NEW_RELIC_ENABLED` to `false`
11+
# Build container
12+
docker build -t lambda-test .
13+
```
14+
15+
## Run test
16+
17+
Start container and make a request to it.
18+
19+
```sh
20+
docker run -p 9900:8080 -p 9229:9229 --env-file .env lambda-test:latest
21+
```
22+
23+
```sh
24+
curl -XPOST "http://localhost:9900/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
25+
```
26+
27+
## Notes
28+
If you want to install the latest agent and not rely on a locally installed agent, uncomment the following line from Dockerfile.
29+
30+
```sh
31+
#RUN npm install
32+
```

lambda-test/app.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
const newrelic = require('newrelic')
3+
const mode = process.env.FUNCTION_MODE
4+
const agentEnabled = process.env.NEW_RELIC_ENABLED
5+
const asyncHandler = require('./handlers/async')
6+
const cbHandler = require('./handlers/cb')
7+
const contextHandler = require('./handlers/context')
8+
const streamHandler = require('./handlers/streaming')
9+
10+
let lambdaHandler
11+
12+
console.log('RUNNING LAMBDA IN MODE', mode)
13+
14+
switch(mode) {
15+
case 'async':
16+
lambdaHandler = asyncHandler
17+
break;
18+
case 'cb':
19+
lambdaHandler = cbHandler
20+
break;
21+
case 'context':
22+
lambdaHandler = contextHandler
23+
break;
24+
case 'streaming':
25+
lambdaHandler = streamHandler
26+
break;
27+
default:
28+
lambdaHandler = asyncHandler
29+
}
30+
31+
if (agentEnabled === 'true') {
32+
module.exports.handler = newrelic.setLambdaHandler(lambdaHandler)
33+
} else {
34+
module.exports.handler = lambdaHandler
35+
}

lambda-test/env.sample

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
NEW_RELIC_LOG=stdout
2+
NEW_RELIC_LOG_ENABLED=true
3+
NEW_RELIC_LOG_LEVEL=trace
4+
NEW_RELIC_APP_NAME=lambda-test
5+
NEW_RELIC_SERVERLESS_MODE_ENABLED=true
6+
NEW_RELIC_ACCOUNT_ID=1
7+
# Allowed values `async`, `context`, `cb`, or `streaming`
8+
FUNCTION_MODE=async
9+
# set next to false if you want to run without agent
10+
NEW_RELIC_ENABLED=true

lambda-test/handlers/async.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
module.exports = async function lambdaHandler() {
3+
const req = await new Promise((resolve) => {
4+
resolve({"hello": "world"})
5+
})
6+
return req
7+
}

lambda-test/handlers/cb.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict'
2+
module.exports = function lambdaHandler(event, context, cb) {
3+
cb(null, { "hello": "world"})
4+
}

lambda-test/handlers/context.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict'
2+
module.exports = function lambdaHandler(event, context, cb) {
3+
return context.done(null, { "hello": "world"})
4+
}

lambda-test/handlers/streaming.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
module.exports = awslambda.streamifyResponse(
3+
async (event, responseStream, context) => {
4+
const httpResponseMetadata = {
5+
statusCode: 200,
6+
headers: {
7+
'Content-Type': 'text/html',
8+
'X-Custom-Header': 'Example-Custom-Header'
9+
}
10+
}
11+
12+
responseStream = awslambda.HttpResponseStream.from(responseStream, httpResponseMetadata)
13+
14+
responseStream.write('<html>')
15+
responseStream.write('<p>First write2!</p>')
16+
17+
responseStream.write('<h1>Streaming h1</h1>')
18+
await new Promise(r => setTimeout(r, 1000))
19+
responseStream.write('<h2>Streaming h2</h2>')
20+
await new Promise(r => setTimeout(r, 1000))
21+
responseStream.write('<h3>Streaming h3</h3>')
22+
await new Promise(r => setTimeout(r, 1000))
23+
24+
const loremIpsum1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vitae mi tincidunt tellus ultricies dignissim id et diam. Morbi pharetra eu nisi et finibus. Vivamus diam nulla, vulputate et nisl cursus, pellentesque vehicula libero. Cras imperdiet lorem ante, non posuere dolor sollicitudin a. Vestibulum ipsum lacus, blandit nec augue id, lobortis dictum urna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae Morbi auctor orci eget tellus aliquam, non maximus massa porta. In diam ante, pulvinar aliquam nisl non, elementum hendrerit sapien. Vestibulum massa nunc, mattis non congue vitae, placerat in quam. Nam vulputate lectus metus, et dignissim erat varius a.'
25+
responseStream.write(`<p>${loremIpsum1}</p>`)
26+
await new Promise(r => setTimeout(r, 1000))
27+
28+
responseStream.write('<p>DONE!</p>')
29+
responseStream.end()
30+
31+
return {
32+
isBase64Encoded: false,
33+
statusCode: 200,
34+
headers: { responseHeader: 'NewRelic-Test-Header' },
35+
body: 'a valid response string'
36+
}
37+
}, { highWaterMark: 1000}
38+
)

lambda-test/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "lambda-test",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"author": "",
9+
"license": "ISC",
10+
"description": "",
11+
"dependencies": {
12+
"newrelic": "^12.15.0"
13+
}
14+
}

0 commit comments

Comments
 (0)