Sriram R
Sriram R

Reputation: 2249

Environment Variables with Serverless and AWS Lambda

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?

Upvotes: 0

Views: 2478

Answers (3)

Pablo Salazar
Pablo Salazar

Reputation: 957

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

Upvotes: 2

DBN
DBN

Reputation: 125

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.

Upvotes: 0

user8128927
user8128927

Reputation:

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

Upvotes: 2

Related Questions