How to access a yaml file uploaded to a layer in a python aws lambda

Question:

I am using python 3.8 as the lambda runtime for my lambda function which I am trying to deploy using cdk v2. This is a lambda as a custom resource that runs every time the stack deploys.

This is the folder structure

 /Dynamics/cust_resource/__init__.py
 /Dynamics/cust_resource/data_objects.py
 /Dynamics/cust_resource/lambda.py
 /Dynamics/cust_resource/requirements.txt
 /Dynamics/deps/config.yaml
 /Dynamics/__init__.py
 /Dynamics/dyna_stack.py
 app.py

The lambda function that needs to read off that text config.yaml file. This yaml file is shared by the cdk stack that creates this function. Using S3 is not an option currently. I have packaged the file in a lambda layer. when I download and unzip the layer, I can see that the file exists in the layer I created.

When I try to access the file using either of the options below:

with open('config.yaml') as f:
        data = yaml.load(f, Loader=SafeLoader)
        print(data)

OR

with open('deps/config.yaml') as f:
        data = yaml.load(f, Loader=SafeLoader)
        print(_data)

I get errors as below for the two options:

Received response status [FAILED] from custom resource. Message
returned: Error: [Errno 2] No such file or directory: ‘config.yaml’

Received response status [FAILED] from custom resource. Message
returned: Error: [Errno 2] No such file or directory: ‘deps/config.yaml’

Asked By: user20358

||

Answers:

The solution that worked for me was as below

with open('/opt/config.yaml') as f:
        data = yaml.load(f, Loader=SafeLoader)
        print(data)

From the documentation

If your Lambda function includes layers, Lambda extracts the layer
contents into the /opt directory in the function execution
environment. Lambda extracts the layers in the order (low to high)
listed by the function. Lambda merges folders with the same name, so
if the same file appears in multiple layers, the function uses the
version in the last extracted layer.

Answered By: user20358
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.