AWS Lambda function setup for Python 3.

Charith Prasanna
5 min readMay 10, 2021

In this post I discuss about how to setup a AWS lambda function for python. Before that you will better to know what is a lambda function and why programmer use them. Check below simple diagram.

In above diagram shows the mechanism of AWS lambda function. There are two ways to access a lambda function.

  1. invoking the function by using its ARN
  2. as shown in above figure , using a AWS API gateway.

We can define different levels of IAM role to Lambda functions. As a example if are going to access a s3 bucket using a lambda we can add a IAM role with s3 full access to out lambda function.

Why programmers need lambda functions?

when use micro service architecture , we can run a separate functionality using those lambda functions. It automatically scale depending into how much work feed into it. It can execute sequentially and parallel (synchronous or asynchronous). Major thing according to my perspective is the less coding and high computational power. We can configure the memory and the response time of a lambda function are the other advantages.

Lets back to the work,

first you need to login to the AWS account and search for lambda functions in top search box. Now you will see below(figure 1) drop down. select Lambda function,

figure 01

Then you will navigate to the lambda function create screen. On the top right corner you can see the create function button in my favorite color orange(figure 02). Lets click it.

figure 02

Then you can follow below steps as show in the figure 03. Give a name for the function and select the runtime environment for the function.Now in lambda python 3.8 is available. After creating the lambda function also you can change it. Don’t much think about it.In Permission selection might be import, because you can set the privacy for the lambda function here. If you are going to work with lambda + s3 bucket , definitely you can add a IAM with s3 full access or relevant IAM with access. As in figure 03 I had selected the first role.Because i m not going do a master minded work with this lambda.You can use an existing IAM role or in here you can create a relevant IAM with selecting 3rd option. Huh.. i think you got an basic idea on this.

figure 03

After doing all the stuff you will navigate to the figure 04 screen.

figure 04

Then lets import some libraries to the lambda function. Why i had use the requests library? Because in python lambda , request library hasn’t installed default. We have to import it into the lambda. Now Layers come to the crease. Using layers we can add external libraries to the lambda function. we can upload library file as a zip file or if library is more than 50mb we can use s3 bucket.In here i use zip file way.

figure 05

Figure 05 show the create layer screen. You can get here by using left navigation bar.

figure 06

in figure 06 shows how looks like layer creating screen. Then we look how to generate library zip file.

pip install requests -t .

use above command to generate library files. when create the zip file , keep in your mind to put all library files inside to a root file. Then below command will help you to genrate zip file.

zip -r dependencies.zip .

After upload the file , you have to select run times for the library. It is multi option drop down. You can select run times up to 15.

Then click the create button in orange. Then go to the lambda function and scroll down until then add a layer section. Layers can use all the lambda function you create. For use those inside your lambda function you need to add those layers into the function. Figure 07 you can see the add a layer section.

figure 07

After touching the add a layer, you will navigate to the layer adding screen. As in Figure 08 , you need to select those details. don’t worry about version, version is automatically set by the AWS. if we add a two same layer , those are separately show in different versions.

figure 08

Then once again navigate to the lambda function screen.Now you need to setup a test env for testing purpose. Lets configure it. As shown in the figure 04 click the test button. Then u will navigate to the figure 09 screen. Then setup as it shows.

figure 09

Now all go. after navigating to the function screen.Now you can import requests library.And deploy it. Once you do a change. you need to deploy those changes.Then click the test button and you will see the below response.

figure 10

As shown in figure 10, you will receive the test response, otherwise you have to go to the top of the article and go through all facts.

Thank you guys!! Cheers.

Lets meet from a another article.

--

--

Charith Prasanna

Software Engineer | University Of Moratuwa | Intervest Software Technologies | Full Stack Developer