AWS Lambda, a part of Amazon Web Services (AWS) is a serverless computing service that works as FaaS (Function as a Service). A FaaS is a service which provides users to develop and manage application service without thinking about infrastructure.
Terraform is an Infrastructure as Code (IaC) tool which is developed by Hasi Corp to manage resources of cloud services like AWS, Google Cloud, Azure, etc. It is open-source and developed by golang.
It is always challenging to zip the codes and upload them for AWS Lambda every time at the time of deployment. The more complex part is to upload codes of libraries e.g python libraries. At Craftsmen, we need to manage a lot of lambdas for various development purposes. So a smart solution for uploading lambda function code and libraries while deployment is a crying need.
Our approach is to upload function codes as function level and libraries in lambda layer. The reasons are 1. Share python libraries between lambdas 2. The console code editor can only visualize 3MB of code. So uploading libraries can make a chance to see codes in console editor.
Let’s start by setting the project skeleton. We are going to use pipenv because it’s more developer-friendly to maintain dev and release packages. First, we install pipenv from here. Then we will install terraform from here
Add python libraries
We will only use a single library called requestsin [packages] and pipenv at [dev-packages]. Also, we are going to use python 3.8. Let’s add all to Pipfile.
Initiate python virtual environment by pipenv with
This will add the Pipfile.lock file which contains information on all python packages.
Let’s start with a simple lambda function which just gets a web page and log it.
Add Terraform code
At first, we will add some files for Terraform
In requirements_creator.py, a python argument parser will be added which gets the filename of pip requirements e.g. requirements.txt/p>
Now let’s add shell script to generate a zip file for lambda layer with python libraries
Now add lambda layer terraform code
Finally, we are going to add lambda terraform code
Time to test
To test if everything works, we have to add Terraform provider. In our case the provider is AWS. Let’s add a file provider.tf in terraform directory.
The project will look like
Let’s build infrastructure