|
| 1 | +# Hot Reloading your Typescript Lambda with LocalStack & Webpack |
| 2 | + |
| 3 | +This is a simple example of how to hot reload your Typescript Lambda with Webpack with the help of LocalStack's Hot Reloading feature. |
| 4 | + |
| 5 | +## Pre-requisites |
| 6 | + |
| 7 | +* [LocalStack](https://docs.localstack.cloud/getting-started/installation) |
| 8 | +* [Docker](https://docs.docker.com/get-docker/) |
| 9 | +* [Node.js](https://nodejs.org/en/download/) |
| 10 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) |
| 11 | +* [`awslocal`](https://github.com/localstack/awscli-local) |
| 12 | +* [yarn](https://classic.yarnpkg.com/en/docs/install) |
| 13 | +* `jq` |
| 14 | + |
| 15 | +## Start LocalStack |
| 16 | + |
| 17 | +Start your LocalStack Docker container with the following command: |
| 18 | + |
| 19 | +```bash |
| 20 | +localstack start |
| 21 | +``` |
| 22 | + |
| 23 | +## Install dependencies |
| 24 | + |
| 25 | +Install the dependencies with the following command: |
| 26 | + |
| 27 | +```bash |
| 28 | +yarn install |
| 29 | +``` |
| 30 | + |
| 31 | +## Build the Lambda |
| 32 | + |
| 33 | +Build the Lambda with the following command: |
| 34 | + |
| 35 | +```bash |
| 36 | +npm run build |
| 37 | +``` |
| 38 | + |
| 39 | +Note that the `build` script is using Nodemon to watch for changes in the `src` directory and rebuild the Lambda. This would be useful when you are developing & testing your Lambda, while making changes to the code in real-time. |
| 40 | + |
| 41 | +With every change, you will see the following output: |
| 42 | + |
| 43 | +```bash |
| 44 | +yarn run build |
| 45 | +yarn run v1.22.19 |
| 46 | +$ yarn run clean && yarn run webpack --watch |
| 47 | +$ rimraf dist/* |
| 48 | +$ cross-env TS_NODE_PROJECT="tsconfig.webpack.json" webpack --mode production --watch |
| 49 | +asset api.js 1.4 KiB [emitted] [minimized] (name: api) |
| 50 | +./src/api.ts 1.42 KiB [built] [code generated] |
| 51 | +./src/util.ts 314 bytes [built] [code generated] |
| 52 | +webpack 5.75.0 compiled successfully in 602 ms |
| 53 | +[nodemon] 3.0.1 |
| 54 | +[nodemon] to restart at any time, enter `rs` |
| 55 | +[nodemon] watching path(s): dist/api.js |
| 56 | +[nodemon] watching extensions: js,mjs,cjs,json |
| 57 | +[nodemon] starting `node dist/api.js` |
| 58 | +[nodemon] clean exit - waiting for changes before restart |
| 59 | +asset api.js 1.4 KiB [emitted] [minimized] (name: api) |
| 60 | +./src/api.ts 1.42 KiB [built] [code generated] |
| 61 | +./src/util.ts 314 bytes [built] [code generated] |
| 62 | +webpack 5.75.0 compiled successfully in 455 ms |
| 63 | +[nodemon] restarting due to changes... |
| 64 | +[nodemon] starting `node dist/api.js` |
| 65 | +``` |
| 66 | + |
| 67 | +## Deploy the Lambda |
| 68 | + |
| 69 | +Deploy the Lambda with the following command: |
| 70 | + |
| 71 | +```bash |
| 72 | +awslocal lambda create-function \ |
| 73 | + --function-name localstack-example \ |
| 74 | + --runtime nodejs18.x \ |
| 75 | + --role arn:aws:iam::000000000000:role/lambda-ex \ |
| 76 | + --code S3Bucket="hot-reload",S3Key="$(PWD)/dist" \ |
| 77 | + --handler api.default |
| 78 | +``` |
| 79 | + |
| 80 | +Additionally, you can create a Lambda Function URL with the following command: |
| 81 | + |
| 82 | +```bash |
| 83 | +function_url=$(awslocal lambda create-function-url-config --function-name localstack-example --auth-type NONE | jq -r '.FunctionUrl') |
| 84 | +``` |
| 85 | + |
| 86 | +## Test the Lambda |
| 87 | + |
| 88 | +You can test the Lambda by sending `POST` requests to the Lambda Function URL: |
| 89 | + |
| 90 | +```bash |
| 91 | +curl -X POST \ |
| 92 | + -H "Content-Type: application/json" \ |
| 93 | + -d '{"name": "John", "age": 30}' \ |
| 94 | + "$function_url" |
| 95 | +``` |
| 96 | + |
| 97 | +The following output would be displayed: |
| 98 | + |
| 99 | +```bash |
| 100 | +{"payload":{"name":"John","age":30}} |
| 101 | +``` |
| 102 | + |
| 103 | +You can additionally test the Lambda with the following command: |
| 104 | + |
| 105 | +```bash |
| 106 | +curl -X GET "$function_url" |
| 107 | +``` |
| 108 | + |
| 109 | +This will return the following output: |
| 110 | + |
| 111 | +```bash |
| 112 | +{"error":"Only JSON payload is accepted"} |
| 113 | +``` |
| 114 | + |
| 115 | +## Hot Reload the Lambda |
| 116 | + |
| 117 | +Go to the `src` directory and make changes to the `api.ts` file. For example, change the following line: |
| 118 | + |
| 119 | +```typescript |
| 120 | +return errorResponse("Only JSON payloads are accepted", 406); |
| 121 | +``` |
| 122 | + |
| 123 | +Make the `errorResponse` function return `"Only JSON payload is accepted"` instead of `"Only JSON payloads are accepted"`. Save the file and run the last `curl` command again: |
| 124 | + |
| 125 | +```bash |
| 126 | +curl -X GET "$function_url" |
| 127 | +``` |
| 128 | + |
| 129 | +This will return the following output: |
| 130 | + |
| 131 | +```bash |
| 132 | +{"error":"Only JSON payload is accepted"} |
| 133 | +``` |
| 134 | + |
| 135 | +You can perform further changes to the `api.ts` file and test the Lambda in real-time. |
| 136 | + |
| 137 | +## How does it work? |
| 138 | + |
| 139 | +The Lambda Hot Reloading feature in LocalStack allows you to hot reload your Lambda code in real-time. In this sample, this is achieved using the following: |
| 140 | + |
| 141 | +- The `build` script in the `package.json` file uses Nodemon to watch for changes in the `src` directory and rebuild the Lambda. This is enabled using the [`nodemon-webpack-plugin`](https://www.npmjs.com/package/nodemon-webpack-plugin) plugin, which has been pre-configured in the `webpack.config.js` file. |
| 142 | +- The `S3Bucket` and `S3Key` parameters in the `awslocal lambda create-function` command are used to deploy the Lambda code from the `dist` directory. This is done by specifying the `dist` directory as the `S3Key` parameter. Everytime the Lambda is updated, Nodemon triggers another build and the `dist` directory is updated with the latest code changes. LocalStack then automatically updates the Lambda code with the latest changes from the `dist` directory. |
| 143 | + |
| 144 | +## Notes |
| 145 | + |
| 146 | +This sample application is inherited from a [public repository](https://github.com/pdlug/lambda-typescript-webpack). |
0 commit comments