aws / aws-lambda-nodejs-runtime-interface-client

Apache License 2.0
183 stars 56 forks source link

restarting RIC #9

Open bahtou opened 3 years ago

bahtou commented 3 years ago

Rebuilding the image with every code change is not an ideal local developer experience.

Is there a way to restart the emulator on code change? Volume mount the function directory into the container, perform code changes to the function, and have the emulator restart?

djpate commented 3 years ago

I got this working with the following in my package.json

"live": "tsc-watch --onSuccess \"/usr/local/bin/aws-lambda-rie /usr/local/bin/aws-lambda-ric /var/task/build/index.handler\"",

chrisjsherm commented 1 year ago

I got this working with the following in my package.json

"live": "tsc-watch --onSuccess \"/usr/local/bin/aws-lambda-rie /usr/local/bin/aws-lambda-ric /var/task/build/index.handler\"",

This put me on the right track, but I received an error saying the server was already listening on the port.

I changed it to:

"live": "tsc-watch --onSuccess \"docker compose restart my-container-name\"",

Dockerfile:

FROM node:18-alpine AS builder

# Install NPM dependencies for function
WORKDIR /app
COPY tsconfig.json package*.json ./
RUN npm clean-install --ignore-scripts

# Copy source files
COPY src src

# Transpile TypeScript to JavaScript
RUN npm run build

################ Create dev stage #######################
FROM amazon/aws-lambda-nodejs:18 as dev

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ] 

################ Create runtime stage #######################
FROM amazon/aws-lambda-nodejs:18 as prod

# Install only production dependencies
COPY package*.json ${LAMBDA_TASK_ROOT}/
RUN npm clean-install --ignore-scripts --omit=dev

# Copy transpiled code
COPY --from=builder /app/dist ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ] 

docker-compose.yml:

version: '3.9'
services:
  web:
    build:
      context: .
      target: dev
    ports: 
      - 9000:8080
      - 9229:9229 # debugger
    environment:
      ValidatedEmailAddress: example@mail.com
    volumes:
         - ~/.aws:/root/.aws:ro # AWS credentials
         - ./dist:/var/task
         - ./node_modules:/var/task/node_modules
govindrai commented 1 year ago

I don't think either of those are the right solutions.

The first works outside of docker which means you have to account for inconsistencies between different OS' that your devs are using. The second one forces container restart which is costly in terms of both time and resources.

It would be very helpful if the client could add support for watching and hot-reloading the handler code upon code changes.

This along with the ability to enter debugger mode would make local development of lambdas amazing.

rbalman commented 11 months ago

I am currently using nodemon --exec for the workaround and is working well enough.

 "scripts": {
    "local:lambda": "nodemon --exec \"/usr/local/bin/aws-lambda-rie /usr/local/bin/npx aws-lambda-ric index.handler\""
  }