Matt M
Matt M

Reputation: 67

Python Lambda no such file or directory found run time issue

I have a lambda function that is returning back an error that no such file or directory has been found.

To be clear the image below shows the file structure exists in my lambda and it is clear that the directory that I am looking for does exist.

My current file structure in the lambda function

Error Message that I am receiving:

[ERROR] FileNotFoundError: [Errno 2] No such file or directory: 'config'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 23, in lambda_handler
    os.chdir("config")

Below is the lambda_handler:

import os
import json

def lambda_handler(event, context):
    
    
    print(event)
    os.chdir("config")
    loginInfo = json.load(open('secrets.json'))
    return loginInfo

The reason why I am changing the directory to config is so that I can access my secrets.json file.

Please let me know if this is sufficient detail to reproduce.

Upvotes: 2

Views: 4130

Answers (3)

Markonick
Markonick

Reputation: 328

I am ignoring the details posted here - I am sure they are valuable - and going to go on a tangent and mention that since you are handling secrets, it is worth investing in a future proof secrets management policy.

https://dev.to/dvddpl/where-do-you-keep-credentials-for-your-lambda-functions-5dno

Using AWS Secrets Manager with Python (Lambda Console)

Upvotes: 0

Traycho Ivanov
Traycho Ivanov

Reputation: 3197

If you need to run it locally you could check aws sam cli.

https://github.com/awslabs/aws-sam-cli

It requires you to define a default cloudformation template.yml with setup of your lambda function.

In the end just execute

sam local invoke -e lambda-event.json

Upvotes: 0

balderman
balderman

Reputation: 23815

Running as a Lambda is not the same as running on your dev machine. If you want to read config - you have several options:

  1. Env variables
  2. Uplaod your config file to s3 and read it from there
  3. Use AWS parameter store and read the config from there,

Upvotes: 2

Related Questions