0
votes

I am following a tutorial in order to learn how to work with the serverless framework. The goal is to deploy a Django application. This tutorial suggests putting the necessary environment variables in a separate yml-file. Unfortunately, following the tutorial gets me a KeyError. I have a serverless.yml, variables.yml and a handler.py. I will incert all code underneath, together with the given error.

serverless.yml:

service: serverless-django
custom: ${file(./variables.yml)}
provider:
  name: aws
  runtime: python3.8
functions:
  hello:
    environment: 
      - THE_ANSWER: ${self:custom.THE_ANSWER}
    handler: handler.hello

variables.yml:

THE_ANSWER: 42

handler.py:

import os

def hello(event, context):
    return {
        "statusCode": 200,
        "body": "The answer is: " + os.environ["THE_ANSWER"]
    }

The error in my terminal:

{
    "errorMessage": "'THE_ANSWER'",
    "errorType": "KeyError",
    "stackTrace": [
        "  File \"/var/task/handler.py\", line 7, in hello\n    \"body\": \"The answer is: \" + os.environ[\"THE_ANSWER\"]\n",
        "  File \"/var/lang/lib/python3.8/os.py\", line 675, in __getitem__\n    raise KeyError(key) from None\n"
    ]
}
 
  Error --------------------------------------------------
 
  Error: Invoked function failed
      at AwsInvoke.log (/snapshot/serverless/lib/plugins/aws/invoke/index.js:105:31)
      at AwsInvoke.tryCatcher (/snapshot/serverless/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:729:18)
      at _drainQueueStep (/snapshot/serverless/node_modules/bluebird/js/release/async.js:93:12)
      at _drainQueue (/snapshot/serverless/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (/snapshot/serverless/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate._onImmediate (/snapshot/serverless/node_modules/bluebird/js/release/async.js:15:14)
      at processImmediate (internal/timers.js:456:21)
      at process.topLevelDomainCallback (domain.js:137:15)
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          linux
     Node Version:              12.18.1
     Framework Version:         2.0.0 (standalone)
     Plugin Version:            4.0.2
     SDK Version:               2.3.1
     Components Version:        3.1.2

The command i'm trying is 'sls invoke -f hello'. The command 'sls deploy' has been already executed succesfully.

I am new to serverless, so please let me know how to fix this, or if any more information is needed.

1

1 Answers

0
votes

First of all, there is an error in yml script:

Serverless Error ---------------------------------------

Invalid characters in environment variable 0

The error is defining environment variables as an array instead of key-value pairs

And then after deployment, everything works smoothly (sls deploy -v)

sls invoke --f hello

{ "statusCode": 200, "body": "The answer is: 42" }


serverless.yml

service: sls-example

custom: ${file(./variables.yml)}

provider:
  name: aws
  runtime: python3.8

functions:
  hello:
    environment:
        THE_ANSWER: ${self:custom.THE_ANSWER}
    handler: handler.hello

variables.yml

THE_ANSWER: 42