AWS Lambda read contents of file in zip uploaded as source code
<p>I have two files:</p>

<pre><code>MyLambdaFunction.py

config.json
</code></pre>

<p>I zip those two together to create <em>MyLambdaFunction.zip</em>.  I then upload that through the AWS console to my lambda function.  </p>

<p>The contents of <em>config.json</em> 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.</p>

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

<p>Figured it out with the push in the right direction from @helloV.</p>

<p>At the top of the python file put "import os"</p>

<p>Inside your function handler put the following:</p>

<pre><code>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)
</code></pre>

<p>That bit right there, line by line, does the following:</p>

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

<p>Here is what the config.json looks like:</p>

<pre><code>{
    "environment":"dev"
}
</code></pre>

python amazon-web-services aws-lambda amazon-lambda