Skip to content Skip to sidebar Skip to footer

Aws Lambda Read Contents Of File In Zip Uploaded As Source Code

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. Th

Solution 1:

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.

Solution 2:

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

import osos.environ['LAMBDA_TASK_ROOT']/config.json

Solution 3:

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]

Post a Comment for "Aws Lambda Read Contents Of File In Zip Uploaded As Source Code"