43
loading...
This website collects cookies to deliver better user experience
npm init -y
to initialize a new project. Next, we create a function that will act as our Lambda handler, in src/function.ts
:// src/function.ts
export const handler = async (event) => {
console.log(event);
return {
status: 200
};
};
npm install serverless serverless-esbuild --save-dev
serverless.yml
# serverless.yml
service: esbuild-demo
plugins:
- serverless-esbuild
provider:
name: aws
runtime: nodejs14.x
functions:
function:
handler: src/function.handler
npx serverless deploy
to get our app up and running in AWS - transpiled, three shaken & ready to rock. No additional configuration is necessary but you can of course choose to configure esbuilds behavior if needed. The transpile target is chosen automatically from the Lambda runtime from the Serverless provider setting, but it will also automatically discover and respect tsconfig.json
if you have it.npm install jest esbuild-jest @types/jest --save-dev
// jest.config.json
{
"transform": {
"^.+\\.(j|t)sx?$": "esbuild-jest"
}
}
// tests/function.test.ts
import { handler } from '../src/function';
describe('[function]', () => {
it('should return status 200', async () => {
const res = await handler(null)
expect(res).toEqual({
status: 200
});
});
});
npx jest
!