Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hotreloading capabilities #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ install:
build:
yarn && yarn build:backend;

hotreload:
yarn && yarn hotreload:backend;

bootstrap:
yarn cdklocal bootstrap;

Expand All @@ -34,6 +37,9 @@ prepare-frontend-local:
build-frontend:
yarn build:frontend

start-frontend:
yarn start:frontend

bootstrap-frontend:
yarn cdklocal bootstrap --app="node dist/aws-sdk-js-notes-app-frontend.js";

Expand All @@ -54,4 +60,12 @@ ready:
logs:
@localstack logs > logs.txt

setup-challenge:
yarn install
make build
make bootstrap
IS_LOCAL_DEV=true make deploy
make prepare-frontend-local
make hotreload

.PHONY: usage install run start stop ready logs
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"cdk": "cd packages/infra && yarn cdk",
"cdklocal": "cd packages/infra && yarn cdklocal",
"build:backend": "cd packages/backend && yarn build",
"hotreload:backend": "cd packages/backend && yarn hotreload",
"prepare:frontend": "node packages/scripts/populate-frontend-config.js",
"prepare:frontend-local": "node packages/scripts/populate-frontend-config.js --local",
"build:frontend": "cd packages/frontend && yarn build",
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"description": "Backend for notes app created using modular AWS SDK for JavaScript",
"dependencies": {
"@aws-sdk/client-dynamodb": "3.245.0",
"@aws-sdk/util-dynamodb": "3.245.0"
"@aws-sdk/util-dynamodb": "3.245.0",
"nodemon": "^3.1.7"
},
"scripts": {
"build": "tsc --noEmit && node build.js",
"build:backend": "cd .. && yarn build:backend",
"hotreload": "nodemon --watch src --ext '*' --exec 'node build.js'",
"cdk": "cd .. && yarn cdk"
},
"keywords": [
Expand Down
18 changes: 15 additions & 3 deletions packages/infra/cdk/notes-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { aws_dynamodb as dynamodb, aws_lambda as lambda } from "aws-cdk-lib";
import {
aws_dynamodb as dynamodb,
aws_lambda as lambda,
aws_s3 as s3,
} from "aws-cdk-lib";
import { Construct } from "constructs";

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

const { table, grantActions } = props;

const isLocalDev = ["true", true].includes(process.env.IS_LOCAL_DEV);

const codeConfig = isLocalDev
? lambda.Code.fromBucket(
s3.Bucket.fromBucketName(this, "hot-reload", "hot-reload"),
`${__dirname}/../../backend/dist/${id}`
)
: lambda.Code.fromAsset(`../backend/dist/${id}`);

this.handler = new lambda.Function(this, "handler", {
runtime: lambda.Runtime.NODEJS_18_X,
handler: "app.handler",
// ToDo: find a better way to pass lambda code
code: lambda.Code.fromAsset(`../backend/dist/${id}`),
code: codeConfig,
environment: {
NOTES_TABLE_NAME: table.tableName,
},
Expand Down
Loading
Loading