Skip to content

Commit 715449c

Browse files
authored
Merge pull request #23 from localstack-samples/hotreloading
2 parents f1c0aa3 + 50fbdd7 commit 715449c

5 files changed

Lines changed: 198 additions & 9 deletions

File tree

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ install:
1616
build:
1717
yarn && yarn build:backend;
1818

19+
hotreload:
20+
yarn && yarn hotreload:backend;
21+
1922
bootstrap:
2023
yarn cdklocal bootstrap;
2124

@@ -34,6 +37,9 @@ prepare-frontend-local:
3437
build-frontend:
3538
yarn build:frontend
3639

40+
start-frontend:
41+
yarn start:frontend
42+
3743
bootstrap-frontend:
3844
yarn cdklocal bootstrap --app="node dist/aws-sdk-js-notes-app-frontend.js";
3945

@@ -54,4 +60,12 @@ ready:
5460
logs:
5561
@localstack logs > logs.txt
5662

63+
setup-challenge:
64+
yarn install
65+
make build
66+
make bootstrap
67+
IS_LOCAL_DEV=true make deploy
68+
make prepare-frontend-local
69+
make hotreload
70+
5771
.PHONY: usage install run start stop ready logs

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"cdk": "cd packages/infra && yarn cdk",
1010
"cdklocal": "cd packages/infra && yarn cdklocal",
1111
"build:backend": "cd packages/backend && yarn build",
12+
"hotreload:backend": "cd packages/backend && yarn hotreload",
1213
"prepare:frontend": "node packages/scripts/populate-frontend-config.js",
1314
"prepare:frontend-local": "node packages/scripts/populate-frontend-config.js --local",
1415
"build:frontend": "cd packages/frontend && yarn build",

packages/backend/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
"description": "Backend for notes app created using modular AWS SDK for JavaScript",
66
"dependencies": {
77
"@aws-sdk/client-dynamodb": "3.245.0",
8-
"@aws-sdk/util-dynamodb": "3.245.0"
8+
"@aws-sdk/util-dynamodb": "3.245.0",
9+
"nodemon": "^3.1.7"
910
},
1011
"scripts": {
1112
"build": "tsc --noEmit && node build.js",
1213
"build:backend": "cd .. && yarn build:backend",
14+
"hotreload": "nodemon --watch src --ext '*' --exec 'node build.js'",
1315
"cdk": "cd .. && yarn cdk"
1416
},
1517
"keywords": [

packages/infra/cdk/notes-api.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { aws_dynamodb as dynamodb, aws_lambda as lambda } from "aws-cdk-lib";
1+
import {
2+
aws_dynamodb as dynamodb,
3+
aws_lambda as lambda,
4+
aws_s3 as s3,
5+
} from "aws-cdk-lib";
26
import { Construct } from "constructs";
37

48
export interface NotesApiProps {
@@ -17,11 +21,19 @@ export class NotesApi extends Construct {
1721

1822
const { table, grantActions } = props;
1923

24+
const isLocalDev = ["true", true].includes(process.env.IS_LOCAL_DEV);
25+
26+
const codeConfig = isLocalDev
27+
? lambda.Code.fromBucket(
28+
s3.Bucket.fromBucketName(this, "hot-reload", "hot-reload"),
29+
`${__dirname}/../../backend/dist/${id}`
30+
)
31+
: lambda.Code.fromAsset(`../backend/dist/${id}`);
32+
2033
this.handler = new lambda.Function(this, "handler", {
2134
runtime: lambda.Runtime.NODEJS_18_X,
2235
handler: "app.handler",
23-
// ToDo: find a better way to pass lambda code
24-
code: lambda.Code.fromAsset(`../backend/dist/${id}`),
36+
code: codeConfig,
2537
environment: {
2638
NOTES_TABLE_NAME: table.tableName,
2739
},

0 commit comments

Comments
 (0)