0
votes

I am learning serverless framework and I'm making a simple login system.

Here is my serverless.yml file

service: lms-auth

provider:
    name: aws
    runtime: nodejs8.10
    stage: dev
    region: ap-south-1

environment:
    MONGODB_URI: $(file(../env.yml):MONOGDB_URI)
    JWT_SECRET: $(file(../env.yml):JWT_SECRET)

functions:
    register:
        handler: handler.register
        events:
            - http:
                  path: auth/register/
                  method: post
                  cors: true
    login:
        handler: handler.login
        events:
            - http:
                  path: auth/login/
                  method: post
                  cors: true

plugins:
    - serverless-offline

As you can see, I have two environment variables and both of them are referencing to a different file in the same root folder.

Here is that env.yml file

MONOGDB_URI: <MY_MONGO_DB_URI>
JWT_SECRET: LmS_JWt_secREt_auth_PasSWoRds

When I do sls deploy, I see that both the variables are logging as null. The environment variables aren't sent to lambda.

How can I fix this?

Also, currently I'm using this method and adding the env.yml to .gitignore and saving the values. Is there any other efficient way of hiding sensitive data?

3
You write "different file in the same root folder." but the file where you read the envs from is the same. Can you share your directory structure as well? Is the env.yml in the parent folder from serverless.yml? - Milan Cermak

3 Answers

2
votes

I would do something like this to help you out with the syntax

service: lms-auth

custom: ${file(env.yml)}
provider:
    name: aws
    runtime: nodejs8.10
    stage: dev
    region: ap-south-1

environment:
    MONGODB_URI: ${self:custom.mongodb_uri}
    JWT_SECRET: ${self:custom.jwt_secret}

functions:
    register:
        handler: handler.register
        events:
            - http:
                  path: auth/register/
                  method: post
                  cors: true
    login:
        handler: handler.login
        events:
            - http:
                  path: auth/login/
                  method: post
                  cors: true

plugins:
- serverless-offline

Then in your env.yml you can do

mongodb_uri: MY_MONGO_DB_URI
jwt_secret: LmS_JWt_secREt_auth_PasSWoRds
0
votes

I ended up solving it. I had set up my Dynamo DB in AWS us-west region. reinitialized in US-East-2, and reset the region under 'provider' within the .yml file.

0
votes

Enviroment variables
1. Add useDotenv:true to your .yml file
2.Add your variables like this -> ${env:VARIABLE_NAME}
3.Create a file called .env.dev and write the variables (You can add .env.prod but you have to change the stage inside your .yml file )

Example :

service: lms-auth

useDotenv: true

provider:
    name: aws
    runtime: nodejs12.x
    stage: dev
    region: us-east-1

environment:
    MONGODB_URI: ${env:MONOGDB_URI}
    JWT_SECRET: ${env:JWT_SECRET}

functions:
    register:
        handler: handler.register
        events:
            - http:
                  path: auth/register/
                  method: post
                  cors: true
    login:
        handler: handler.login
        events:
            - http:
                  path: auth/login/
                  method: post
                  cors: true

plugins:
    - serverless-offline

.env.dev

MONOGDB_URI = The URI Value 
JWT_SECRET = The JWT Scret Value