Pass enviromental variable on the fly to Serverless invoke local function

I have the following Serverless configuration file:

service: aws-node-scheduled-cron-project

frameworkVersion: '2 || 3'

plugins:
  - serverless-plugin-typescript

provider:
  name: aws
  runtime: nodejs14.x
  lambdaHashingVersion: 20201221
  region: us-west-2
  # Imports all the enviromental variables in `.env.yml` file
  environment: ${file(./env.yml)}

functions:
  ...

env.yml

DATABASE_HOST: 127.0.0.1
DATABASE_USER: root
DATABASE_PASSWORD: your_mysql_root_password
DATABASE_TABLE: laravel
DATABASE_PORT: 3306
DATABASE_USE_SSL: false
NOTIFICATION_SERVICE_URL: http://localhost:4000

Now I would like to change DATABASE_TABLE on the fly when invoking the function locally. I have tried:

export DATABASE_TABLE=1-30-task-template-schedule && npx serverless invoke local --function notifyTodoScheduleFullDay

but the variable DATABASE_TABLE gets overwritten by the one in env.yml. Is it possible to do it via command line?


On your yml file you can declare the table name as ${opt:tablename,'DEFAULT'}. This line means that you are going to give the name as a parameter from the terminal command like this serverless deploy ... -tablename NAME_OF_THE_TABLE if you do not give it as a parameter it takes the default name you can give it. This can generate a name on the fly.