Home > Software engineering >  Referencing local environment variables in Serverless v3 upgrade
Referencing local environment variables in Serverless v3 upgrade

Time:09-21

Im upgrading serverless to v3, worked fine in v2 but for some reason i cant debug it locally in vscode as it keeps crashing when initializing. I have tracked it down to the local env var im referencing from my bashrc file. I know its this as if i just write the values direct in to the yml file it will start up and debug fine. Serverless have provided a guide to upgrading to v3 https://www.serverless.com/framework/docs/guides/upgrading-v3 and right at the bottom it says add useDotenv: true in your yml file. I tried this and still doesnt work?

Working

provider:
  name: aws
  stage: 'dev'
  region: 'us-west-1'
  runtime: nodejs16.x
  iam:
    role: 'sls-lamda-role'
  deploymentBucket:
    name: 'sls-bucket'
  vpc:
    securityGroupIds:
      - 'sls-sec-group'
    subnetIds:
      - 'sls-subnet-1'
      - 'sls-subnet-2'

Not Working

provider:
  name: aws
  stage: '${env:TARGET_ENV_NAME}'
  region: '${env:TARGET_REGION}'
  runtime: nodejs16.x
  iam:
    role: '${env:SLS_LAMBA_ROLE}'
  deploymentBucket:
    name: '${env:SLS_BUCKET}'
  vpc:
    securityGroupIds:
      - '${env:SLS_VPN_SECURITY_GROUP}'
    subnetIds:
      - '${env:SLS_SUBNET_1}'
      - '${env:SLS_SUBNET_2}'

CodePudding user response:

In your serverless, keep below config.

useDotenv: true

The framework looks for .env and .env.{stage} files in service directory and then tries to load them using dotenv. If .env.{stage} is found, .env will not be loaded. If stage is not explicitly defined, it defaults to dev.

refer: https://www.serverless.com/framework/docs/environment-variables

  • Related