LoginSignup
8
4

More than 5 years have passed since last update.

parcelだけでAWS Lambda向けのTypeScriptをビルドする

Posted at

AWS Lambda FunctionでTypeScriptを使いたい場合、通常Serverless frameworkやapexを使うのが一般的だろう。
しかし、ちょこっと使いたいだけの場合に重苦しい。

もしかしたらParcelだけでいけるのでは?と思ったらいけたのでメモ

手順

まずはTypeScriptインストール

yarn add -D typescript

parcelも入れる。zip化するとアップロードも楽になるのでpluginも入れる

yarn add -D parcel parcel-plugin-zip

そしてfunction本体。今回はhandler.tsという名前にしておく

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

これだけでもうビルドはできる。下記のようなコマンドだ

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

それぞれ引数は下記のようなことをしている

  • --target=node で対象をnodeに
  • --bundle-node-modulesでnode_modulesからの読み込み物を追加
  • sourcemapは不要なので--no-source-map
  • -o index.jsで出漁対象をindex.jsにへこう
  • --global=handler でhandlerに外からアクセスできる形でビルド(ちゃんと検証してないけどもしかしたら不要かも?)

これを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 --region=ap-northeast-1",
  "deploy": "yarn build; yarn deploy:aws",
}

上記ではawsコマンドを使ってコードをアップロードするところまでやっている(ここについては今回説明は割愛する)

あとはyarn deployでビルドまでできる

参考

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4