Home > Net >  Serverless framework is ignoring CLI options
Serverless framework is ignoring CLI options

Time:01-18

I'm trying to dynamically pass in options to resolve when deploying my functions with serverless but they're always null or hit the fallback.

custom:
  send_grid_api: ${opt:sendgridapi, 'missing'}
  SubscribedUsersTable:
    name: !Ref UsersSubscriptionTable
    arn: !GetAtt UsersSubscriptionTable.Arn
  bundle:
    linting: false

provider:
  name: aws
  lambdaHashingVersion: 20201221
  runtime: nodejs12.x
  memorySize: 256
  stage: ${opt:stage, 'dev'}
  region: us-west-2
  environment:
    STAGE: ${self:provider.stage}
    SEND_GRID_API_KEY: ${self:custom.send_grid_api}

I've also tried:

  environment:
    STAGE: ${self:provider.stage}
    SEND_GRID_API_KEY: ${opt:sendgridapi, 'missing'}

both yield 'missing', but why?

sls deploy --stage=prod --sendgridapi=xxx

also fails if I try with space instead of =.

Edit: Working Solution

In my github action template, I defined the following:

      - name: create env file
        run: |
          touch .env
          echo SEND_GRID_API_KEY=${{ secrets.SEND_GRID_KEY }} >> .env
          ls -la
          pwd

In addition, I explicitly set the working directory for this stage like so:

working-directory: /home/runner/work/myDir/myDir/

In my serverless.yml I added the following:

  environment:
    SEND_GRID_API_KEY: ${env:SEND_GRID_API_KEY}

sls will read the contents from the file and load them properly

CodePudding user response:

opt is for serverless' CLI options. These are part of serverless, not your own code.

You can instead use...

provider:
  ...
  environment:
    ... 
    SEND_GRID_API_KEY: ${env:SEND_GRID_API_KEY}

And pass the value as an environment variable in your deploy step.

- name: Deploy
  run: sls deploy --stage=prod
  env:
    SEND_GRID_API_KEY: "insert api key here"

CodePudding user response:

It think those notations are both incorrect, because one can access environmental variables directly:

SEND_GRID_API_KEY: ${ SEND_GRID_API_KEY }

See Using AWS Lambda environment variables.


One verify a function's the defined environment with:

aws lambda get-function-configuration --function-name my-function
  •  Tags:  
  • Related