20

I have two files:

MyLambdaFunction.py

config.json

I zip those two together to create MyLambdaFunction.zip. I then upload that through the AWS console to my lambda function.

The contents of config.json are various environmental variables. I need a way to read the contents of the file each time the lambda function runs, and then use the data inside to set run time variables.

How do I get my Python Lambda function to read the contents of a file, config.json, that was uploaded in the zip file with the source code?

Scott Decker
  • 4,229
  • 7
  • 24
  • 39
  • Not sure using that method, but you could absolutely move the config to S3 and read the file from there each time. – j-u-s-t-i-n Sep 13 '16 at 21:44
  • https://www.npmjs.com/package/aws-lambda-config – j-u-s-t-i-n Sep 13 '16 at 21:49
  • @j-u-s-t-i-n agreed, and if I have to I will. But it'd be really nice to just have it all zipped up in one file (MyLambdaFunction.zip) and upload that to the lambda function. Eliminates the need for another S3 bucket and worrying about encryption there. – Scott Decker Sep 13 '16 at 21:56

5 Answers5

24

Figured it out with the push in the right direction from @helloV.

At the top of the python file put import os

Inside your function handler put the following:

configPath = os.environ['LAMBDA_TASK_ROOT'] + "/config.json"
print("Looking for config.json at " + configPath)
configContents = open(configPath).read()
configJson = json.loads(configContents)
environment = configJson['environment']
print("Environment: " + environment)

That bit right there, line by line, does the following:

  • Get the path where the config.json file is stored
  • Print that path for viewing in CloudWatch logs
  • Open the file stored at that path, read the contents
  • Load the contents to a json object for easy navigating
  • Grab the value of one of the variables stored in the json
  • Print that for viewing in the CloudWatch logs

Here is what the config.json looks like:

{
    "environment":"dev"
}

EDIT AWS lambda now supports use of environmental variables directly in the console UI. So if your use case is the same as mine (i.e. for a config file) you no longer need a config file.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Scott Decker
  • 4,229
  • 7
  • 24
  • 39
3

Try this. The file you uploaded can be accessed like:

import os

os.environ['LAMBDA_TASK_ROOT']/config.json
helloV
  • 50,176
  • 7
  • 137
  • 145
  • Not working. I added "import os" at the top of the python file. Inside my function handler I have a line that says "environmentalConfig = os.environ['LAMBDA_TASK_ROOT']/config.json". I then take the python file and the "config.json" file and zip them into one folder. Upload that through the AWS Lambda console. When it runs I get an error that says " "environmentalConfig = os.environ['LAMBDA_TASK_ROOT']/config.json"]], "errorType": "NameError", "errorMessage": "global name 'config' is not defined"}" – Scott Decker Sep 17 '16 at 14:53
  • @ScottDecker that's because the answer above is not correct syntax. It's a string concat so either do `os.environ['LAMBDA_TASK_ROOT'] + '/config.json'` or use an f-string `f"{os.environ['LAMBDA_TASK_ROOT']}/config.json"`... – jbaranski Jan 26 '22 at 18:57
1

Actually, I'd rather prefer judging the context of the running lambda to determine the config it should use, instead of uploading different zip files, which is difficult to maintain.

lambda_configs = {
    "function_name_1":{
    },
    "function_name_2":
   {
   }
}

config = lambda_configs[context.function_name]
Menglong Li
  • 2,177
  • 14
  • 19
1

Just done it in python3.8 lambda with simple:

with open('./dir/file.whatever') as f:

And it works just fine.

wtdmn
  • 73
  • 1
  • 7
0
import os


os.environ['LAMBDA_TASK_ROOT'] + '/config.json'

or you can use formatting

f"{os.environ['LAMBDA_TASK_ROOT']}/config.json"
Koedlt
  • 4,286
  • 8
  • 15
  • 33
Ashok
  • 19
  • 4