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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay