Environment variables undefined in serverless invoke test

I have posted this issue on Github but I am afraid I am not going to get an answer there: https://github.com/serverless/serverless/issues/6773 so I am looking for community help.

When using serverless invoke test , environment variables (such as AWS_REGION ) are not set.
However, when using serverless offline start , environment variables are set properly.

Running this from my test file: console.log("process.env.AWS_REGION:", process.env.AWS_REGION); returns process.env.AWS_REGION: undefined

Do I need to do anything to initialize env vars?

This is the content of my serverless.yml file:

service: compliance-auth
frameworkVersion: ">=1.1.0 <2.0.0"
custom:
  dynamodb:
    stages:
      - ${self:provider.stage}
    start:
      port: 8000
      inMemory: true
      migrate: true
    migration:
      dir: offline/migrations
provider:
  name: aws
  runtime: nodejs10.x
  region: eu-central-1
  stage: dev
  environment:
    USERS_DYNAMODB_TABLE: ${self:service}-users-${opt:stage, self:provider.stage}
    ORGANIZATIONS_DYNAMODB_TABLE: ${self:service}-organizations-${opt:stage, self:provider.stage}
    AWS_USER_POOL_ID: eu-central-1_0Jxxx8KP8
    AWS_USER_POOL_URL: https://cognito-idp.${self:provider.region}.amazonaws.com/${self:provider.environment.AWS_USER_POOL_ID}
    AWS_USER_POOL_APP_CLIENT_ID: 1qjdqf802d2v3k1ncp1t4vxxxx
    AWS_IDENTITY_POOL_ID: ${self:provider.region}:38c9e7c8-023d-47a0-xxxx-9a7dc0b692b3
    AWS_API_VERSION: 2016-04-19
    AWS_KEYS_URL: ${self:provider.environment.AWS_USER_POOL_URL}/.well-known/jwks.json
  profile: default
  logRetentionInDays: 30
  timeout: 30
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.USERS_DYNAMODB_TABLE}"
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.ORGANIZATIONS_DYNAMODB_TABLE}"
functions:
  login:
    handler: handler.login
    events:
      - http:
          path: login
          method: post
          cors: true
  getOrganizations:
    handler: handler.getOrganizations
    events:
      - http:
          path: organizations
          method: get
          cors: true
  createOrganization:
    handler: handler.createOrganization
    events:
      - http:
          path: organizations
          method: post
          cors: true
  getOrganization:
    handler: handler.getOrganization
    events:
      - http:
          path: organizations/{id}
          method: get
          cors: true
  getUsers:
    handler: handler.getUsers
    events:
      - http:
          path: users
          method: get
          cors: true
  registerUser:
    handler: handler.registerUser
    events:
      - http:
          path: users
          method: post
          cors: true
  confirmRegistrationCode:
    handler: handler.confirmRegistrationCode
    events:
      - http:
          path: codes/confirm
          method: post
          cors: true
  resendRegistrationCode:
    handler: handler.resendRegistrationCode
    events:
      - http:
          path: codes/resend
          method: post
          cors: true
  forgotPassword:
    handler: handler.forgotPassword
    events:
      - http:
          path: users/password/forgot
          method: post
          cors: true
  confirmPassword:
    handler: handler.confirmPassword
    events:
      - http:
          path: users/password/confirm
          method: post
          cors: true
  verifyToken:
    handler: handler.verifyToken
    events:
      - http:
          path: tokens/verify
          method: post
          cors: true
  refreshToken:
    handler: handler.refreshToken
    events:
      - http:
          path: tokens/refresh
          method: post
          cors: true
resources:
  Resources:
    AuthDynamoDbTable:
      Type: "AWS::DynamoDB::Table"
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: "${self:provider.environment.USERS_DYNAMODB_TABLE}"
    OrgDynamoDbTable:
      Type: "AWS::DynamoDB::Table"
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: "${self:provider.environment.ORGANIZATIONS_DYNAMODB_TABLE}"
    CognitoUserPool:
      Type: "AWS::Cognito::UserPool"
      Properties:
        MfaConfiguration: OFF
        UserPoolName: complyhub-user-pool
        UsernameAttributes:
          - email
        Policies:
          PasswordPolicy:
            MinimumLength: 6
            RequireLowercase: False
            RequireNumbers: True
            RequireSymbols: False
            RequireUppercase: True
    CognitoUserPoolClient:
      Type: "AWS::Cognito::UserPoolClient"
      Properties:
        ClientName: complyhub-pool-client
        GenerateSecret: false
        UserPoolId:
          Ref: CognitoUserPool
    ApiGatewayAuthorizer:
      DependsOn:
        - ApiGatewayRestApi
      Type: AWS::ApiGateway::Authorizer
      Properties:
        Name: cognito-authorizer
        IdentitySource: method.request.header.Authorization
        RestApiId:
          Ref: ApiGatewayRestApi
        Type: COGNITO_USER_POOLS
        ProviderARNs:
          - Fn::GetAtt: [CognitoUserPool, Arn]
plugins:
  - serverless-dynamodb-local
  - serverless-offline
  - serverless-mocha-plugin

you actually forgot to set AWS_REGION in the serverless.yml :slight_smile: