Skip to content

Commit

Permalink
lambda local
Browse files Browse the repository at this point in the history
  • Loading branch information
ksaifullah committed Nov 27, 2023
1 parent 325d48c commit bbd243d
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 10 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM public.ecr.aws/lambda/nodejs:20

# Copy function code
COPY dist ${LAMBDA_TASK_ROOT}/dist
COPY package.json package-lock.json ${LAMBDA_TASK_ROOT}

# Install NPM dependencies for function
RUN npm install --omit=dev

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
# CMD [ "dist/addition.handler" ]
7 changes: 7 additions & 0 deletions auto/lambda-local-logs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -euo pipefail

cd $(dirname $0)/..

docker-compose -f compose.local.yaml logs -f
10 changes: 10 additions & 0 deletions auto/lambda-local-start
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -euo pipefail

cd $(dirname $0)/..

docker-compose run --rm install
docker-compose run --rm compile
docker-compose -f compose.local.yaml build
docker-compose -f compose.local.yaml up -d
7 changes: 7 additions & 0 deletions auto/lambda-local-stop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -euo pipefail

cd $(dirname $0)/..

docker-compose -f compose.local.yaml down
15 changes: 15 additions & 0 deletions compose.local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.9'
services:
# curl -XPOST "http://localhost:9001/2015-03-31/functions/function/invocations" -d '{"a":2,"b":3}'
addition:
build: .
ports:
- '9001:8080'
command: dist/addition.handler

# curl -XPOST "http://localhost:9002/2015-03-31/functions/function/invocations" -d '{"a":5,"b":3}'
subtraction:
build: .
ports:
- '9002:8080'
command: dist/subtraction.handler
4 changes: 4 additions & 0 deletions docker-compose.yml → compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ services:
test:
<<: *base
command: npm test

compile:
<<: *base
command: npm run compile
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"homepage": "https://github.com/brainstation-au/aws-lambda-container-nodejs#readme",
"devDependencies": {
"@types/aws-lambda": "^8.10.129",
"@types/eslint": "^8.44.7",
"@types/jest": "^29.5.10",
"@types/node": "^20.10.0",
Expand Down
18 changes: 18 additions & 0 deletions src/addition.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Callback, Context } from 'aws-lambda';
import { handler } from './addition';

describe('addition', () => {
describe('handler', () => {
test('2 and 3 adds up to 5', async () => {
await expect(handler({a: 2, b: 3}, {} as Context, {} as Callback)).resolves.toEqual(5);
});

test('invalid event structure', async () => {
await expect(handler({a: 2}, {} as Context, {} as Callback)).rejects.toThrow();
});

test('invalid data types', async () => {
await expect(handler({a: 'x', b: 'y'}, {} as Context, {} as Callback)).rejects.toThrow();
});
});
});
10 changes: 10 additions & 0 deletions src/addition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Handler } from 'aws-lambda';

export const handler: Handler = async (event) => {
if ('a' in event && typeof event['a'] == 'number'
&& 'b' in event && typeof event['b'] == 'number') {
return event['a'] + event['b'];
} else {
throw new Error('event doesn\'t satisfy the type { a: number, b: number }');
}
};
9 changes: 0 additions & 9 deletions src/index.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/subtraction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Callback, Context } from 'aws-lambda';
import { handler } from './subtraction';

describe('subtraction', () => {
describe('handler', () => {
test('2 take away 3 returns -1', async () => {
await expect(handler({a: 2, b: 3}, {} as Context, {} as Callback)).resolves.toEqual(-1);
});

test('invalid event structure', async () => {
await expect(handler({a: 2}, {} as Context, {} as Callback)).rejects.toThrow();
});

test('invalid data types', async () => {
await expect(handler({a: 'x', b: 'y'}, {} as Context, {} as Callback)).rejects.toThrow();
});
});
});
10 changes: 10 additions & 0 deletions src/subtraction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Handler } from 'aws-lambda';

export const handler: Handler = async (event) => {
if ('a' in event && typeof event['a'] == 'number'
&& 'b' in event && typeof event['b'] == 'number') {
return event['a'] - event['b'];
} else {
throw new Error('event doesn\'t satisfy the type { a: number, b: number }');
}
};

0 comments on commit bbd243d

Please sign in to comment.