5x Decrease In Build Time For AWS Lambda

Posted on Mar 30, 2024

Background:

At my work we have an API that is setup as an AWS API gateway with Lambda functions attached to the API gateway routes. We package the Lambda functions in containers. This blog post goes over the benefits of packaging your Lambdas in containers. Pretty standard setup. Each Lambda function had its own Python file and Dockerfile. At the time of writing there are around ~15 unique Lambda functions. The project file structure was as follows.

.
├── dockerfiles
│   ├── func1.dockerfile
│   ├── func2.dockerfile
│   └      …
├── src
│   ├── func1.py
│   ├── func2.py
│   └      …
├── samconfig.toml
├── setupfiles.example
└── template.yaml

Problem:

The build and deployment times were getting ridiculous. On our CI/CD the time to build and deploy was getting to over 15 minutes. 15 minutes to deploy is not great especially if you are trying to push a fix fast.

Solution:

The solution is to build one container with all the required files and override the command in the sam template. I got this idea of packaging and overriding the the command from fly.io docs on Run Multiple Process Groups in an App. You can override the command by adding ImageConfig to your function:

ImageConfig:
  Command:
    - “folder/file.lamdba_handler”

Although there is a small startup that costs around ~100ms for running the larger container. I think that the small cost in startup is worth the speed in deployment and the cost of maintaining more and more Docker files as the project grows. After making this change the time to deploy on the CI/CD has gone down to 3 mins.