Home > Net >  Is it necessary to have ${WORKSPACE} directory in serverless package for Lambda?
Is it necessary to have ${WORKSPACE} directory in serverless package for Lambda?

Time:09-28

I have started using Serverless framework with AWS. My source is in Typescript which would be built to JavaScript before deploying. This gets uploaded to S3 and then lambda function is created. I noticed that my lambda functions are over 70MB although I only have a few lines of code with operations that use just the aws-sdk, like querying DynamoDB or SecretsManager.

To investigate this, I downloaded the zipped file which gets uploaded to S3 by serverless framework and unzipped for its content. It has a folder named ${WORKSPACE} which accounts for the 70% of the package memory and it does not seem to have any relevant content for the lambda function.

My package.json looks like this

{
"name": "my-api",
"version": "0.0.1",
"description": "API to retrieve secrets from Secrets Manager",    
"license": "UNLICENSED",
"engines": {
    "node": ">= 14.0.0"
},
"scripts": {
    "build": "tsc -p .",
    "deploy": "sls deploy"
},
"dependencies": {
    "@aws-sdk/client-secrets-manager": "^3.33.0"
},
"devDependencies": {
    "serverless": "^2.59.0",
    "typescript": "^4.4.3"
}
    }

and the relevant part of the serverless.yml looks like this

service: sls-my-api

useDotenv: true

variablesResolutionMode: 20210326

frameworkVersion: '2'

package:
 patterns:
  # Includes
  - 'dist/**.js'
  # Excludes
  - '!./**.md'
  - '!./**.env*'
  - '!.github/**'
  - '!.npm/**'
  - '!coverage/**'
  - '!docker/**'
  - '!docs/**'
  - '!dist/tsconfig.build.tsbuildinfo'
  - '!dist/**.d.ts'
  - '!src/**'
  - '!test/**'
  - '!.eslintrc.js'
  - '!.npmrc'
  - '!.prettier*'
  - '!./tsconfig*.json'
  - '!Jenkinsfile'
  - '!jest.json'
  - '!nest-cli.json'
  - '!run-sonar.sh'
  - '!sonar-project.properties'
  - '!tslint.json'
  - '!.gitattributes'
  - '!.npmignore'
  - '!.s2iignore'
  - '!database/**'

In the Jenkins stages, the following are run

npm run build 
npm run deploy 

Also because of the size limitation, inline editing is not available on the lambda console which usually is a very useful action for me, especially when testing and debugging quickly without having to wait for a redeployment.

Does the directory ${WORKSPACE} have any significance for this deployment? If not, how do I exclude it from the deployment package and make my lambda lean?

CodePudding user response:

False Alarm!

The directory ${WORKSPACE} is generated because of Jenkins run and not because of the serverless framework. The frame although was picking it up while packing and deploying the application thus making the lambda function bulk.

Excluding it as follows did the trick.

package:
  patterns:
    # Includes
    - 'dist/**.js'
    # Excludes
    - '!${WORKSPACE}'

Maybe something good to know for people using serverless cli on Jenkins.

  • Related