I’m using the Serverless Framework to deploy an API Gateway and AWS Lambda functions together.
Yesterday, I successfully deployed 6 Lambda functions using a single serverless.yaml
file located in the parent directory of the individual Lambda npm projects. Each function’s package size was between 500 KB and 5 MB, depending on the function.
However, when I tried deploying the same setup today, I noticed that all the functions now include the node_modules
directory from the parent directory, even though the functions don’t use it. This has significantly increased the package size for each Lambda function.
The directories and the serverless.yaml
file haven’t changed since yesterday, so I’m unsure why this is happening. How can I exclude the node_modules
directory from the parent directory during deployment?
Here’s the serverless.yaml
file I’m using (located at project-lambdas/serverless.yaml
).
service: projects
frameworkVersion: '4'
provider:
name: aws
runtime: nodejs20.x
region: ap-northeast-2 # seoul
role: ${self:custom.ENV.ARN.lambdaRole}
stage: ${opt:stage, 'dev'}
vpc:
securityGroupIds: ${self:custom.ENV.VPC.securityGroupIds}
subnetIds: ${self:custom.ENV.VPC.subnetIds}
package:
individually: true
excludeDevDependencies: true
functions:
user-auth:
handler: api-user-auth-ts/src/lambda.handler
memorySize: 512
timeout: 15
package:
include:
- api-user-auth-ts/**
exclude:
- ../node_modules/**
events:
- http:
path: /
method: get
- http:
path: api/user-auth
method: get
- http:
path: api/user-auth/{proxy+}
method: any
community:
handler: api-community-ts/src/lambda.handler
memorySize: 512
timeout: 15
package:
include:
- api-community-ts/**
exclude:
- ../node_modules/**
events:
- http:
path: api/community
method: get
- http:
path: api/community/{proxy+}
method: any
id-generator:
handler: lambdas/id-generator/src/lambda.handler
memorySize: 256
package:
include:
- lambdas/id-generator/**
exclude:
- ../node_modules/**
ncp-mailer:
handler: lambdas/ncp-mailer/src/lambda.handler
memorySize: 256
package:
include:
- lambdas/ncp-mailer/**
exclude:
- ../node_modules/**
s3-manager:
role: ${self:custom.ENV_LAMBDAS.ARN.lambdaRoleS3}
handler: lambdas/s3-manager/src/lambda.handler
memorySize: 256
package:
include:
- lambdas/s3-manager/**
exclude:
- ../node_modules/**
token-manager:
handler: lambdas/token-manager/src/lambda.handler
memorySize: 256
package:
include:
- lambdas/token-manager/**
exclude:
- ../node_modules/**
build:
esbuild:
bundle: true
minify: true
exclude:
- 'projects-lambdas/node_modules/**'
# Custom Variables
custom:
stages:
- dev
- qa
- release
ENV: ${file(./env.${self:provider.stage}.js):ENV_CONFIG}
ENV_AUTH: ${file(./api-user-auth-ts/env.${self:provider.stage}.js):ENV_CONFIG}
ENV_COMMUNITY: ${file(./api-community-ts/env.${self:provider.stage}.js):ENV_CONFIG}
ENV_LAMBDAS: ${file(./lambdas/env.${self:provider.stage}.js):ENV_CONFIG}
To explain directories
project-lambdas/(npm project to use AWS-SDK when load env.js file)
└ serverless.yaml
└ node_modules/ ← which is include all other function package
└ api-user-auth-ts/(npm project)
└ node_modules/
└ api-community-ts/(npm project)
└ node_modules/
└ lambdas
└ id-generator/(npm project)
└ node_modules/
└ ncp-mailer/(npm project)
└ node_modules/
└ s3-manager/(npm project)
└ node_modules/
└ token-manager/(npm project)
└ node_modules/