Package and deploy AWS Lambda functions

Amol Kokje
FAUN — Developer Community 🐾
3 min readNov 22, 2019

--

Most recent cloud deployments have multiple Lambda functions. As the number of functions keep increasing, the packaging of the code along with its dependencies, and updating the deployments becomes more and more complex! So, it has essentially become a requirement to automate this flow.

In this post, I am planning to go over a few methods that you can use to automate that task with their Pros and Cons. The supporting code files for all the contents of this post is located in my GitHub repo.

Package and deploy AWS Lambda functions

Using a Script:

The first thing that comes to mind when we talk about automation is — let’s write a script! Yes, you can achieve packaging using a script. Here is the link to a simple example on how you can script the packaging of python lambda code with its dependencies. It follows this guide from AWS. It has five main steps:

  • Create python virtual env
  • Install all the dependencies in the virtual env.
  • Package all the dependencies, and the lambda function code in a zip.
  • Upload the package zip to a s3 bucket.

Remember to get rid of the virtual env if you do not need it for later.

PROS: No dependencies required for the script. Can customize script for different dependencies and needs.

CONS: Extra logic required for Lambda Layers. Need to manage local testing with some other framework. You need to support and manage your indigenous solution.

Using SAM CLI:

The most popular approach for Lambda deployments is use of SAM CLI. SAM stands for Serverless Application Model and is the “way to go” for serverless deployments, since it offers many features like local testing, packaging, templates for stack deployments, etc. If new to this model, it may seem like an overhead, but once you get used to SAM, its very easy, fast and clean. Trust me, it will save a lot of your time moving forward in your project! Once you are setup, it only takes two steps:

  • sam build → Creates package with your code files and specified dependencies.
  • sam package — s3-bucket <bucket-name> → Deploys the package in the specified s3 bucket.

To use the SAM CLI, you need to install and it on your host, for which you can follow this guide. Here is a link to the sample code.

PROS: Support for Lambda Layers, Local testing, Package deployment, Cloudformation stack management. AWS supported, and lots of documentation, examples.

CONS: SAM CLI installation is required. The SAM package command generates a random name when package uploaded to s3. Note: The points listed here are not really downsides as such, but more of things you may want to take note of when using SAM.

Of course, writing a script feels more intuitive, and you can also enable more complex ways to add dependencies, but it does not scale very well for complex environments. SAM CLI will scale very well for most use cases, and is also supported by AWS along with a huge community! If you are wanting to learn how Lambda works, a script will help, but I recommend on-boarding to SAM from Day One.

Good luck with your serverless journey! I hope this post is helpful, and feel free to reach out if you may have any questions.

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--