Migrating to v3 and env variables on CLI

Hi everyone,

Hope this message finds you well! :smile:

I am in the task of updating a few lambda functions we have from v2 to v3. We want to do this mainly because AWS will stop supporting Node12, but I’ve encountered some issues while trying to do so.

The first thing I did was update all packages and dependencies to the latest versions available. Then, I updated my serverless.yml with the following

service: ...

frameworkVersion: '3'

useDotenv: true

  - serverless-dotenv-plugin
  - serverless-offline
  - '@kingdarboja/serverless-plugin-typescript'
  name: aws
  runtime: nodejs16.x
  memorySize: 256
  stage: ${env:STAGE}
  region: us-west-2

    handler: src/main.handler
      - http:
          method: ANY
          path: /
          cors: true
      - http:
          method: ANY
          path: '{proxy+}'
          cors: true

and this is my package.json

    "name": "...",
    "version": "0.3.0",
    "description": "",
    "author": "",
    "private": true,
    "license": "UNLICENSED",
    "scripts": {
        "prebuild": "rimraf dist",
        "build": "nest build",
        "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
        "start": "nest start",
        "start:dev": "nest start --watch",
        "start:debug": "nest start --debug --watch",
        "start:prod": "node dist/main",
        "start:dev-serverless": "serverless offline start --env local",
        "serverless:deploy": "serverless deploy --region $AWS_SECRET_REGION --stage $STAGE",
        "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
        "test": "jest",
        "test:watch": "jest --watch",
        "test:cov": "jest --coverage",
        "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
        "test:e2e": "jest --config ./test/jest-e2e.json"
    "dependencies": {
        "@aws-sdk/client-kms": "^3.155.0",
        "@aws-sdk/client-secrets-manager": "^3.154.0",
        "@kingdarboja/serverless-plugin-typescript": "^1.4.1",
        "@nestjs/axios": "^0.1.0",
        "@nestjs/common": "^9.0.11",
        "@nestjs/config": "^2.2.0",
        "@nestjs/core": "^9.0.11",
        "@nestjs/platform-express": "^9.0.11",
        "@types/aws-lambda": "^8.10.102",
        "@vendia/serverless-express": "^4.10.1",
        "aws-lambda": "^1.0.7",
        "aws-sdk": "^2.1200.0",
        "aws-serverless-express": "^3.4.0",
        "axios": "^0.27.2",
        "class-validator": "^0.13.2",
        "dynamoose": "^2.2.0",
        "express-jwt": "^7.7.5",
        "express-jwt-authz": "^2.4.1",
        "jsonwebtoken": "^8.5.1",
        "jwks-rsa": "^2.1.4",
        "nestjs-dynamoose": "^0.4.0",
        "reflect-metadata": "^0.1.13",
        "rimraf": "^3.0.2",
        "rxjs": "^7.5.6",
        "serverless-dotenv-plugin": "^4.0.2"
    "devDependencies": {
        "@nestjs/cli": "^9.0.0",
        "@nestjs/schematics": "^9.0.1",
        "@nestjs/testing": "^9.0.11",
        "@types/express": "^4.17.13",
        "@types/jest": "28.1.7",
        "@types/node": "^18.7.10",
        "@types/supertest": "^2.0.12",
        "@typescript-eslint/eslint-plugin": "^5.34.0",
        "@typescript-eslint/parser": "^5.34.0",
        "eslint": "^8.22.0",
        "eslint-config-prettier": "^8.5.0",
        "eslint-plugin-prettier": "^4.2.1",
        "jest": "^28.1.3",
        "plugin": "^0.3.3",
        "prettier": "^2.7.1",
        "serverless": "^3.22.0",
        "serverless-offline": "^9.2.6",
        "serverless-plugin-optimize": "^4.2.1-rc.1",
        "serverless-plugin-typescript": "^2.1.2",
        "source-map-support": "^0.5.21",
        "supertest": "^6.2.4",
        "ts-jest": "^28.0.8",
        "ts-loader": "^9.3.1",
        "ts-node": "^10.9.1",
        "tsconfig-paths": "^4.1.0",
        "typescript": "^4.7.4"

So my main question would be, is it possible to access ENV variables from the CLI as I’m doing in my start:dev-serverless and serverless:deploy scripts?

If I run npm run serverless:deploy I get the following error

> serverless deploy --region $AWS_SECRET_REGION --stage $STAGE

Environment: linux, node 16.14.2, framework 3.22.0 (local), plugin 6.2.2, SDK 4.3.2
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Missing value for CLI parameter "--stage"

I’m also not sure how the stage is managed here, we had it in a way that it would read it from an .env file with the STAGE value in it, but according to this post it does not work that way anymore. But I can’t wrap my head around it, so it would be helpful if someone knows what the best way to do it.

We just put our values in a .env file, like so:


Update the serverless.yml file region entry:

  stage: ${env:STAGE}
  region: ${env:REGION}

And don’t pass them on the cli in your package.json

"serverless:deploy": "serverless deploy",

and it should deploy to the region and stage set in your .env file. :grinning: :+1:

Our CI/CD build has a custom script that generates the .env file with appropiate STAGE entries


Thanks for the response!

Are you guys using serverless v3 and node16?

Serverless v3 and node14. Lumigo didn’t support node16 at the time, though will revisit at some point.