arabold / serverless-export-env

Serverless plugin to export environment variables into a .env file
MIT License
102 stars 34 forks source link

sls invoke local =broken after serverless 1.35.0? #12

Closed herebebogans closed 4 years ago

herebebogans commented 5 years ago

Anyone else noticing that the plugin seems to be broken with sls invoke local -f <function> after serverless 1.35.0

Ref: Environment variables resolve to [object Object] in the handler. Last serverless release that works is 1.34.1

This commit looks possibly the issue?

https://github.com/serverless/serverless/pull/5565/commits/44a6ac1a101a4ea8aee405bcd1ca538c1b1d88ce

herebebogans commented 5 years ago

Works again if I hack my local serverless/lib/plugins/aws/invokeLocal/index.js to revert the referenced commit.

@arabold not sure on the best way to fix the hook order?

brucekaufman commented 5 years ago

I've also run into this problem. Any suggestions on how to fix?

herebebogans commented 5 years ago

@brucekaufman we've replicated the functionality of the plugin using stack exports to set environment variables in helper wrappers around serverless offline and serverless invoke.

You can then do a use "env variable or fallback to cloudformation ref" in serverless variables.

brucekaufman commented 5 years ago

@herebebogans can you provide an example of what you're talking about? I am serverless newbie :)

herebebogans commented 5 years ago

No worries - I explained it poorly.

Here's an example of referencing a dynamo table with the pattern

custom:
  defaults:
    dynamo:
      myTable: !Ref MyTable
....
  dynamo:
    myTable: ${env:MY_TABLE, self:custom.defaults.dynamo.myTable}    
.....
functions:
  myFunction:
    handler: index.handler
    environment:
      MY_TABLE: ${self:custom.dynamo.myTable}
...    
resources:
  Description: My Stack
  Resources:
    MyTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: mykey
            AttributeType: S
        BillingMode: PAY_PER_REQUEST          

You then need something to set the MY_TABLE env variable before running sls offline or sls invoke local We were using the aws cli along with direnv but now we have wrapper scripts for sls offline / invoke using gulp.

You could also pass it in like

MY_TABLE=dynamotablename sls offline

jfn0 commented 4 years ago

@arabold Any update, is this repo still maintain?

arabold commented 4 years ago

Sorry for the total lack of communication from my side. I have pushed a version 1.3.1 that should fix this problem by explicitly setting the environment variables before the actual local invoke.