Leo
Leo

Reputation: 5122

Including config files for lambdas

I have created a lambda and I would like to access some files from a different directory (ie. data_dir).

How do I include them?

So far, I tried using layers and efs mount but I didn't manage to get either of these to work...

Here is my file system structure:

src
|__lambdas
      |__my_lambdas.py
data_dir
|__transform.xsl
|__schema.json
|__config.ini

I am currently using this template to sam build --use-container and sam local invoke IngestFunction

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Description

Resources:
  IngestFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/lambdas/
      Handler: my_lambdas.some_handler_method
      Runtime: python3.7
      Events:
        IngestSQS:
          Type: SQS
          Properties:
            Queue: arn:aws:sqs:eu-west-1:xxx:xxx
            BatchSize: 10

Upvotes: 2

Views: 5297

Answers (2)

Traycho Ivanov
Traycho Ivanov

Reputation: 3207

General your CodeUri has to point to location where all your files are available. It could be folder or archive.

First option is to zip it.

During your build package lambda.

zip -qr build/package.zip src data_dir
Handler: src/lambdas/my_lambdas.some_handler_method
CodeUri: ./build/package.zip

Second option is copy files.

During your build copy files.

cp -r src build
cp -r data_dir build

Handler: src/lambdas/my_lambdas.some_handler_method
CodeUri: ./build

Upvotes: 2

Avinash Dalvi
Avinash Dalvi

Reputation: 9301

There is two approach you can do this for :

  1. Keep this data_dir inside lambda folder itself and access within itself.

  2. Create S3 Bucket data_dir and access this by boto3 S3 download_file function each file.

For layer you have to create python package using __init__.py and load this dependant file and access using layer. You can't directly access.

Not sure about EBS mount haven't tried this option.

Upvotes: 1

Related Questions