DEV Community

terrierscript
terrierscript

Posted on

8 1

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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)