Skip to content

Loading a csv file in a Lambda layer

0

I would like to use a CSV file every time I trigger the Lambda function. So I zipped my colorchart.csv file into colorchart.zip, and added this file as a layer of my function.

But when I tried the following Python code in Lambda, it did not work.

with open('colorchart.csv', mode ='r')as file:
            csvFile = csv.reader(file)
            for lines in csvFile:
                pass

The error message is : FileNotFoundError: [Errno 2] No such file or directory: 'colorchart.csv' How to make my Lambda code find colorchart.csv? Thanks

asked a year ago585 views
1 Answer
0
Accepted Answer

From this page: https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html

When you add a layer to a function, Lambda extracts the layer contents into the /opt directory in your function’s execution environment. All natively supported Lambda runtimes include paths to specific directories within the /opt directory. This gives your function access to your layer content. For more information about these specific paths and how to properly package your layers, see Packaging your layer content.

I would update your code to open /opt/colorchart.csv as below and test the function.

with open('/opt/colorchart.csv', mode ='r')as file:
    csvFile = csv.reader(file)
    for lines in csvFile:
        pass
AWS
EXPERT
answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.