Search code examples
node.jsdockeraws-lambdaserverless

How can i use my docker image for lambda function using serverless?


I've decided to use my docker image to upload my lambda express application due to large file size.

FROM public.ecr.aws/lambda/nodejs:20

COPY . ${LAMBDA_TASK_ROOT}

RUN npm ci --production

CMD [ "index.handler" ]

How can i specify this image in my serverless yml ?

org: projectname
app: express
service: deployments
frameworkVersion: '3'


provider:
  name: aws
  runtime: nodejs20.x
  timeout: 10
  tracing:
    lambda: false

functions:
  api:
    handler: index.handler
    name: express-api
    events:
      - httpApi: '*'

Solution

  • For that, you will need to configure AWS ECR in your provider, that will be built locally and uploaded to ECR.

    First configure provider in serverless.yml

    provider:
      name: aws
      runtime: nodejs20.x
      timeout: 10
      tracing:
        lambda: false
      ecr:
        images:
          express_api: #image name
            path: ./
    

    Dockerfile and serverless.yml has to be in same root directory.

    Then replace the handler with image

    functions:
      api:
        image:
          name: express_api
          command:
            - index.handler # this will replace command in docker file
        name: express-api
        events:
          - httpApi: '*'
    

    Serverless doc