Deploy AWS Lambda functions using .zip file archives

Shashank P. Sharma
6 min readJun 30, 2021

--

In computer programming, a lambda function refers to a small anonymous function that can take any number of arguments.

AWS Lambda is a serverless compute service provided by Amazon

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. It's a saviour if we need some custom logic to be integrated with other AWS services, eg. processing images before storing in AWS S3 or creating a backup bucket in S3.

It's as simple as it gets, just write codes in the language of your choice (and is supported by AWS Lambda) and configure it with a trigger or integrate it with Lambda API Gateway. Your life is perfectly fine and relaxed until you come across any imports. I’ve worked in python runtime for most of my Lambda days so I’ll speak in that context but feel free to relate as the feeling is quite general. Most of the python modules which we use are not pre-installed on the AWS Lambda runtime. We will install and use all those modules using a special feature known as AWS Lambda Layers.

Before we proceed further it’s good to have some basic understanding of AWS EC2 Instances and Linux terminals. We will use a virtual Linux machine to create a .zip file (which contains all the source and vendor codes) as I’ve found that it’s the safest way to avoid OS compatibility issues in every possible situation.

Note: AWS Lambda runtime runs on a Linux machine and if we create an archive in Windows or Mac it flags OS compatibility issues while deployment.

Now first things first. Let’s go ahead and spin an Amazon Linux on EC2 instance running t2.micro

Amazon Linux 2 AMI

Connect to your instance using Connect in EC2.

accessing EC2 instance from terminal

Then run sudo yum update the command to get your machine up to date.

Runsudo su to go into root user mode (now you can run commands without the use of sudo).

Run cd to come to the base folder.

Now we will install python and pip on our machine

We must install the same version of python we will be using in our Lambda runtime.

As python installation required the GCC compiler on our system we can use the following command to install prerequisites for Python before installing it.

Download Python using the following command from the Python official site. You can also download the latest version in place of the specified below.

Now let’s extract the downloaded package.

Now we’ll install python. We use the below set of commands to compile Python source code for our system using altinstall.

make altinstall is used to prevent replacing the default python binary file /usr/bin/python.

Now we remove the downloaded source archive file from our system

Check the latest version installed of python. We use the command python3.7 instead of python. The new binary will be installed at /usr/local/bin/python3.7 location:

Create python file

Now we’ll create a folder cancel where we make a new cancel.py file needed to deploy on lambda.

Create a file using sudo nano command for Linux and put your code in it.

Now run the code using python cancel.py (or python3 cancel.py). You will most probably get the following error.

This is because the pymongo module is not installed. We will install this module using below pip command (which creates a vendor folder in this root folder).

Now running the above code will yield no error. We are all set to zip up our code.

Zipping it up!

This below command will zip all the files and folders in my root folder (cancel) into a zip file called cancel.zip

Contents of cancel folder would look like below.

Upload zip file to S3 bucket

Before we upload our file to our S3 bucket we have to ensure the below steps.

  1. Create an IAM role with S3 write access or admin access
  2. Map the IAM role to an EC2 instance
  3. Install AWS CLI in EC2 instance
  4. Run the AWS s3 cp command to copy the files to the S3 bucket

Create an IAM role with S3 write access or admin access

Login to your AWS Management Console and go to IAM.

Create a new IAM role with S3 Admin Access which can be lateral mapped to the EC2 instance for easy S3 and EC2 integration.

Refer the following GIF to know how to create a new IAM role for S3 access

https://www.middlewareinventory.com/wp-content/uploads/2020/09/S3IAM.mp4?_=1

Map the IAM role to an EC2 instance

Choose the EC2 instance you want to assign this IAM role to.

Click on Actions > Security> Modify IAM Role

Search for the IAM Role we have created in the previous step and select it and hit Save.

Install AWS CLI in EC2 instance

This is already installed on your machine as we chose Amazon Linux at first place.

Run the AWS s3 cp command to copy the files to the S3 bucket

Use the following commands to copy the files to S3 Bucket from EC2.

aws s3 cp cancel.zip s3://<bucket>/cancel.zip

Running the Lambda function

We can easily use the file from S3 by providing it’s S3 URI in AWS Lambda upload option.

Copy the URI from S3.

Upload it on lambda using option below.

Now the file is uploaded to your AWS Lambda and ready for deployment.

The use of trigger and Lambda API gateway is outside the scope of this article so I’ll keep it for another one. Till then may the force be with you.

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

Write a response