Deploying AWS Lambda Function with Terraform + Custom Dependencies

Deploying Lambda Functions written in Python that use custom dependencies not included in AWS Lambda Runtime for Python with Terraform as Infrastructure as Code (IaC).

AWS Lambda Functions

In this article, we will learn:

  • How to deploy Lambda function on Amazon Web Services.
  • How to use Terraform to deploy our project infrastructure.
  • How integrate S3 notifications with our Lambda Function.
  • How to package our Lambda Function with all ours dependencies and deploy it to AWS.

The purpose of this example project is: create a Lambda Function that will be activated by AWS S3 Event trowed when an video in “.mp4” format is copied in an S3 Bucket. The Lambda Function will notify us calling an mocked REST API endpoint.

Project structure:

Terraform

Terraform allow us to define infrastructure as code to manage the full lifecycle of our resources, create new resources, manage existing ones, and destroy those no longer needed.

For install Terraform in your system go through: https://learn.hashicorp.com/terraform/getting-started/install.html

Starting writing the Lambda Function

This Lambda Function has some behaviors, first in order to use some external package we will using “json_checker” to check some dictionary’s schema, later we read the files that sends the S3 Event notification and finally we send the data to an REST API endpoint using: https://beeceptor.com/.

→lambda.py

requirements.txt

Defining AWS resources with Terraform

Some files will be written in order to define all the resources and relations needed by our project. First, we’ll defining some variables in order to reuse it in all our resources.

variables.tf

We need to define the role that will execute the Lambda Function, define the security policy and attach it to the role.

iam.tf

Later, we need to define the bucket involve in this process. Also we will define the relation with the Lambda Function (don’t worry about the files where the relations are defined, they are managed by Terraform in a proper way).

buckets.tf

Finally, we will define the Lambda Function and the middle processes that are in charge of package our application.

As you can see, in the first definition block, we are calling the following script: “/scripts/create_pkg.sh”.

create_pkg.sh

In this moment, we are ready to deploy our Lambda Function, and for that we need to execute the followings commands:

After that, we need to configure our mock endpoint using: https://beeceptor.com/. For example: using “python-lambda” as an endpoint name.

Important, remember update your Lambda Function code with your specific endpoint URL.

And…

Finally, copy some video file to your Bucket using the following command (remember you will be using other Bucket by sure):

aws s3 cp test_video.mp4 s3://aws-lambda-function-read-videos/test_video.mp4

After finished the file copy, the S3 Event must be triggered and your endpoint must received the information!

Don’t forget delete your unused resources…

Well, that’s all. I hope this information will be useful in your next code time!!!

Code repository: https://github.com/alekglez/lambda_function_deploy.git

Bye…

Cloud & Solutions & Data Architect | Python Developer | Serverless Advocate