6

I am designing a serverless application with AWS Lambda. There's a piece of code on one of the functions that process the request in a certain way. I am going to make another function that's going to make the same processing with the request data, in the same way.

The problem is that, if I change the processing function in one of the Lambda functions, I'm going to have to copy the function and paste it into the other Lambda function. Every time I make a change I will have to do this. This will be even more cumbersome if I want to do the same processing function in more than two Lambda functions.

Is there a way to share pieces of code between Lambda functions, so I may respect DRY principles? Thanks.

2 Answers 2

7

Now you can use Layers to share libraries and code between your Functions.
It is possible to base more then one Function on one Layer.

You can create a zip file for the Layer pretty much the same way as you can do so for a Function. The only thing will be that all the common packages go to python/lib/python3.7/site-packages directory inside of zip and all your code goes to python directory.

So if you have file structure like this:

bundle.zip/
  python/
    common/
      __init__.py
      lib.py

Then from your Lambda Function's code you can reference it like this:

from common.lib import ...
Sign up to request clarification or add additional context in comments.

Comments

2

One solution is to use Terraform to synchronize your infrastructure and lambda functions. With Terraform, you'll be able to define each lambda function like so:

resource "aws_lambda_function" "func1_lambda" {
    function_name = "func1_lambda"
    handler = "func1"
    runtime = "python2.7"
    filename = "lambda.zip"
    source_code_hash = "${base64sha256(file("lambda.zip"))}"
    role = "${aws_iam_role.lambda_exec_role.arn}"
}

resource "aws_lambda_function" "func2_lambda" {
    function_name = "func2_lambda"
    handler = "func2"
    runtime = "python2.7"
    filename = "lambda.zip"
    source_code_hash = "${base64sha256(file("lambda.zip"))}"
    role = "${aws_iam_role.lambda_exec_role.arn}"
}

Inside lambda.zip (a zip file containing lambda.py), you would define each lambda function as well as any common functions needed by all lambdas:

def aCommonFunc(input):
    # return something here

def func1(event, context):
    return { "message": aCommonFunc("hello, world") }

def func2(event, context):
    return { "message": aCommonFunc("another string") }

Deploying your new set of lambdas would involve writing a script that zips up your python files and then runs terraform apply.

While this does add more work up-front, it will allow you to track and re-create your Lambdas more efficiently over time as your project grows.

You can see a full example here.

1 Comment

Well, It seems to add a lot of complexity on setting the environment, at least if compared with apex, but it may be worthy, as it also handles other infra components, as API Gateways. I'll give it a try! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.