DEV Community

terrierscript
terrierscript

Posted on

Build AWS Lambda function with typescript only use parcel-bundler

In general, we need like tools like serveless or apex when use typescript on AWS Lambda.
But it's so heavy for light use.

I found way to build with only parcel-bundler

First, install typescript

yarn add -D typescript

and install parcel and parcel-plugin-zip

yarn add -D parcel parcel-plugin-zip

Create handler.ts

export const handler = async (): Promise<any> => {
  console.log('Hello World!');
  return {};
}

And we build this function for aws with below command.

$ yarn parcel build handler.ts --target=node --global handler -o index.js --bundle-node-modules --no-source-maps

Finally, setup build command to package.json.

// package.json
"scripts": {
  "build": "yarn parcel build handler.ts --target=node --global handler -o index.js --bundle-node-modules --no-source-maps",
  "deploy:aws": "aws lambda update-function-code --function-name my-special-function --zip-file fileb://./dist.zip",
  "deploy": "yarn build; yarn deploy:aws",
}

We can now build and deploy lambda function with yarn deploy (may need setup aws credentials) !

References

Oldest comments (0)