Alecu Bernard Louis
Alecu Bernard Louis

Reputation: 23

Serverless Framework Python 1 yml and multiple directories per each lambda function

I did as much research as I could but I can`t seem to find a way to structure my folder the way I want to.

My folder structure looks like this:

aws-lambdas
    database_credentials.yml (just a file to read the creds from in a var)
    serverless.yml
    functions
        ETLTool
            somefile1.py
            somefile2.py
            lambda_function.py
            ETLToolFullLoadSLS.yml
            ETLToolSLS.yml
        TriggerSnowflakeETL
            somefile1.py
            somefile2.py
            lambda_function.py
            TriggerSnowflakeETLSLS.yml

What I want to do is to pull in all the .yml from inside the functions folder into the serverless.yml at the root folder. My main serverless.yml file looks as such:

service: SnowflakePoc
frameworkVersion: '2'

custom:
    database_credentials: ${file(./database_credentials.yml):database_credentials}

provider:
    name: aws
    runtime: python3.8
    lambdaHashingVersion: 20201221
    timeout: 90
    memorySize: 2048

    stage: dev
    region: eu-west-2

    vpc:
        securityGroupIds:
            - sg-013059b0cbf4054b5
            - sg-02c6fcaa9f2bfac7f
        subnetIds:
            - subnet-04aa5cacdb8d9d077
            - subnet-0ea7eb629fbc6f6a8

    iam:
        role: arn:aws:iam::309161096106:role/LamdaRDSAccess

functions:
    - ${file(./functions/ETLTool/ETLToolSLS.yml)}
    - ${file(./functions/ETLTool/ETLToolFullLoadSLS.yml)}
    - ${file(./functions/TriggerSnowflakeETL/TriggerSnowflakeETLSLS.yml)}

plugins:
  - serverless-python-requirements

The issue is that the whole functions/* folder is picked up by each of the lambdas even if I have something like this in each inner function yml file.

TriggerETLTool:
    timeout: 600
    memorySize: 5000
    reservedConcurrency: 3
    handler: functions/TriggerSnowflakeETL/lambda_function.lambda_handler
    layers:
        - arn:aws:lambda:eu-west-2:309161096106:layer:Snowflake:3
        - arn:aws:lambda:eu-west-2:309161096106:layer:DatabaseUtilities:5
    package: 
        patterns: 
            - '!functions/TriggerSnowflakeETL/**'
            - functions/TriggerSnowflakeETL/lambda_function.py

Inside AWS it looks like this: Pic from AWS Lambda Source Code

  1. Is there a better pattern than having 1 directory per lambda?

  2. I would like just the files inside each function to be at the root of my lambdas without them being inside a folder once they reach AWS as they are in the image. Also I`d like to have just the files from each inner-function inside the functions folder rather than the whole functions directory.

Upvotes: 2

Views: 794

Answers (1)

Aaron Stuyvenberg
Aaron Stuyvenberg

Reputation: 3787

If you want to package each function individually, you'll need two things (one of which you've already done)

  1. Configure your serverless.yml file to package functions individually:
   service: SnowflakePoc
   package:
     individually: true
   provider:
     ...

  1. In each function, specify the pattern to correctly zip just that part (you've already done this)

Packaging individually is configurable globally or at a per-function level, so you can choose what's best for you.

You can find more information in the documentation

Upvotes: 1

Related Questions