AWS Lambda Python – AWS Lambda Python Tutorial

Hello guys and welcome to this series on
AWS Lambda. In this series, I will show you the following: 1. How to create and
deploy Lambda functions in Python. 2. How to execute the functions with HTTP calls
and lastly how to integrate lambda with other AWS services like S3 and much more. In this video, I’ll go over the fundamentals of Serverless, AWS Lambda, and I’ll end the video by creating our first Lambda function in
Python. NKT Studios Intro Video So what are Serverless applications? Serverless applications, are applications that run without you having to provision or manage the servers. Hence the name Server Less, meaning, without servers. But just because you don’t provision or manage the servers doesn’t mean they are not there.
Your application will still run on the servers but all the management is done
by a AWS. The cool thing is that, you only pay for the compute time you consume and therefore there is no charge when your code is not running. In essence
you just upload your code and Lambda will take care of everything
required to run and scale your code with high availability. Now let’s move on to
what is Lambda? Lambda is a compute service where you can upload your code. As mentioned above your code will then be executed using AWS servers. You also don’t have to worry about managing servers this means that you don’t have
to worry about what operating system to use, patching, scaling and high
availability. That is all done for you. A simple way to think of Lambda is that it
is Functions As A Service. Your code is called a function on the Lambda platform. Lambda functions can be written in a variety of programming languages such as
Java, Python, Go, C#, Ruby and Node.Js . AWS Lambda can be used in the
following two ways The first one. As an event-driven compute service were it runs your code in response to events. These events could, for example, be changes to data in an Amazon S3 bucket. The events that come from other debris services are
caught triggers the second way in which a Tabriz lambda can be used is as a
computer vez to run your code in response to HTTP requests using Amazon
API gateway or two API calls made using AWS SDKs in this
series who look at both scenarios persistence all the functions that you
write on a tab is lambda F to be stateless this however doesn’t mean that
you can’t store any persistent data with lambda you can connect with s3 and
DynamoDB to store any persistent data so just how much does a Deborah’s lambda
cost a Deborah’s lambda is covered under the free tier and AWS gives away a lot
for free you get 1 million requests per month and 400,000 gigabyte seconds of
compute time per month for free gigabyte second is a unit of measurement that
specifies the amount of memory allocated to a task per second that that task runs
for example if the memory allocated is one gigabyte and the task runs for 10
seconds the compute time consumed would therefore be one gigabyte times 10
seconds which is equal to 10 gigabytes per second after the first 3 1 million
requests the cost then becomes 20 cents per million requests day after and after
the first free 400,000 gigabyte seconds the cost then becomes zero point zero
zero zero zero one six six six six seven for every gigabit seconds remember with
lambda if there is no usage there is no costs they are the cool thing with
lambda is that the free tier does not automatically expire at the end of your
12 months AWS free tier Tim but it is available
indefinitely now that we have a basic understanding of lambda let’s create our
first lambda function in Python which simply is two numbers and displays the
result the first thing that we need to do is just log in to the Amazon
management console once in the console we can search for lambda here under
services and then just click here to launch the lambda home screen if your
screen is showing something different you can just go to the top here and
search for lambda and then click on the first result to go to the lambda home
screen if you already have some lambda functions they will be displayed here
one thing to note is that a Debra’s lambda is bound by regions in this
instance we to create our lambda function in the
island region and that is where to reside if I were to change the region
for example to Northern Virginia you see any lambda functions if any as you can
see I’m now in the Northern Virginia region and as can be seen here I’ve got
a lambda function that I last modified nine months ago anyway let’s go back to
our island region because that’s where I want us to create our lambda function to
create our lambda function just click create function here it will give us
three options for creating the lambda function which our author from scratch
the second one is used a blueprint which is building a lambda application from
sample code and configuration presets for common use cases and lastly deploy a
sample lambda application from the AWS surveillance application repository we
are going to go with author from scratch which is already selected now we need to
give it a name we’re going to call a lambda calc so just type copy in there
next we need to choose the run time the run time is just a programming language
that we are going to use so click on the drop-down and select Python 3.8 we
always try to use the latest and greatest so python 3.8 is the latest
version of python as of this recording next we need to give permissions to our
lambda so just expand choose or create an execution role permissions in AWS are
basically a way to give an AWS user or service in this case access to another
AWS service so for instance we want our lambda 2 right it’s logs to Amazon
CloudWatch therefore we need to give it the permissions to be able to access
Amazon CloudWatch hooligan go with the pre-selected option
which is create a new row with basic lambda permissions then click on the
create function button here to create our lambda function it will take a few
seconds to create the lambda function it’s done now and it gives you a
confirmation here at the top in green that it successfully created the
function calc this space that we are seeing is the lambda configuration page
and it has got many different sections such as designer function code
environment variables tags and execution rows the others if you scroll down let’s
just expand designer and see what is there
as you can see it – it triggers and destinations triggers
are the things that can invoke our lambda function in the next video
whoo ed API gateway is a trigger for our lambda function now let’s focus on the
function code section there for pass treat the court enter type the runtime
the handler and lastly the online text editor where you can write your code the
code enter type basically asks you how you want to put in the code for the
lambda it has three options which are edit code in line which is pre-selected
and it’s the one that we’re going to use you can also upload a zip file this
allows you to upload a zip file with the lambda function and lastly there’s
upload a file from Amazon s3 this option allows you to choose a file that you
have already uploaded to Amazon s3 the next part is the run time as I mentioned
earlier you use the run time to select the programming language that you’ll be
using for the lambda function we have already selected Python 3.8 and that is
the one shown here but you’re able to change the run time by clicking on the
drop down and selecting from the supported languages there the third part
is the handler information this just ties in the name of your Python file and
the name of the method with the entry point in this case the name of the
Python file is lambda underscore function dot P oh I and the name of the
endler is lambda handler you can name your lambda n like anything you want but
just remember to also update it here on the handler so that AWS will be able to
launch it AWS lambda expects a handler function
that takes in two parameters that is event and context if it is a Python
runtime let me give you a bit more information about event in context event
is an object formed from a json formatted string that presents the
action I didn’t want to reuse the word event or the trigger that caused the
invocation of the lambda for instance if our lambda was triggered by an upload to
s3 then the event who contained the information about object being uploaded
I’ve put a link in the description below for an event example context provides
information about the invocation function and execution environment for
your lambda so you can use it to check things like memory allocation or to
retrieve the number of milliseconds left before the execution times out
we chose edit code line so this is where we’re going to
write our code but before we do that I just want to show you a few more things
so just go down with me to basic settings this is where we set the memory
and the time art for the lambda you can also put a description here but it is
not mandatory the function is allocated CPU proportional to the memoir
configured and it defaults to 128 megabytes 10 watt is the longest amount
of time that the function can run for if your function isn’t finished by this
time Amazon will just kill it the term Rd Falls to 3 seconds but you
can put 1 second as a smallest unit and the maximum time out is 15 minutes this
means that you can leave a lambda function that runs for more than 15
minutes as of today the 13th of December 2019 the 128 megabytes memory for
function is okay but the three seconds timeout is too much so I’m just gonna
reduce it to one second remember this is a configuration page so we can get to
see what we have already configured for example the execution row that we set up
when we’re creating the lambda with this I think we are ready to write and test
our lambda function so just scroll up to the inline editor but before we change
anything we just need to make sure that everything is wired up correctly that is
can we run this default lambda function as it is without any changes if it runs
it means that everything is wired up correctly and then we can modify this
lambda to perform the task that we want in this instance add two numbers and
return the result just as lambda function we need to create a test
configuration and we do that by clicking on this test button here it will allow
us to create a test configuration there are many templates you can choose from
to mimic the event that you are building the lambda for if you click on the drop
down here you’ll be able to see all the events you can also even search for a
particular event for instance if I just want the s3 events I just type in s3 and
it will filter the results and just show me the events that pertain to s3 we are
going to use the event template in Lord so just click anywhere outside the
search bar under event name we’re gonna call our event my test event we’re not going to send anything to our
lambda so we can delete the key value pairs here but tech not will leave the
braces there they are required if we remove them we’ll get an error because
the test event has to be in JSON format so the braces are important just click
on create to create our test event as you can see we now have a test event
code my test event and it is displayed next to our test pattern now that we
have a test event we can just the wiring of our lambda function by clicking on
the test pattern here if everything is wired up correctly we’re going to expect
a return statement that has this information here that is the information
in the brackets and we’ll see it in two places
the first place is a pop-up that’ll come up here and the second place is under
execution results and if it has the status of succeeded then we know that
everything is wired up correctly so let’s just click on test and see as you
can see we’ve got a succeeded status and we can expand details here to see the
information this including the brackets is what was retained and if we check our
lambda function this is what we are returning for a lambda function and if
we check on the execution result we have the same information what we are
returning here is the response the execution result tab also shows
information such as the request ID and the function logs you can also see this
function logs in AWS cloud watch if we’re to scroll up to the pop-up you can
also see the same function logs but it does give you a link to see these logs
in cloud watch now that we are happy that our lambda is wired up correctly
and it works we can now modify the lambda function to do the task that we
want the test that we want is for each to add two numbers and then return that
is out so I’m just going to put my first number is 10 and then my second number is 20 and then
I’m going to store the result in result and then I’m going to return this out in
the body of the return statement click on save to save the changes and once
it’s done click on test as you can see here the body s 30 which is the result
of 18 10 and 20 if we scroll up we can also see the body has got 30 which is
the result you might be wondering is it not possible to just return the results
yes it’s possible so I can delete this and just say the 10 is out it was to
work and you’ve just returned that is out safe and then test as you can see
the response is 30 which is the result of adding 10 and 20 if we scroll up the
result is just 30 the thing is a double is introduced something called lambda
proxy and for it to work it requires a return statement that is at the minimum
a status code as well as the body it can also include things like content headers
we’ll see this when we integrate our lambda function with API gateway that is
all for this video in the next video we are going to be connecting our lambda
function to API gateway so that we can access it over HTTP thank you for
watching and please subscribe to support the channel if you haven’t already

Leave a Reply

Your email address will not be published. Required fields are marked *