Subtitle

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

- [Morgan] AWS Lambda is one of these serverless compute

options you have available to you on AWS. Lambda allows you to package and upload
your code
to the Lambda service, creating what's called a Lambda function. Once you create a
Lambda function, it isn't always running all of the time. Instead, Lambda functions
run in response to triggers. You configure a trigger for when you want your
function to run and from there, the Lambda
service waits for the trigger. There's a long list of available
triggers for AWS Lambda and new ones are being
supported frequently. So, I won't run through them all now. However, a couple of
common examples of triggers for Lambda functions are an HTTP request, an upload of
a file to the
storage service, Amazon S3, events originating from other AWS services or even in-
app activity
from mobile devices. When the trigger is detected,
the code is automatically run in a managed environment, an environment you do not
need to worry too much about because it is automatically
scalable, highly available and all of the maintenance of the environment itself is
done by AWS. You do, however, get
to choose the language your Lambda function is coded in, the amount of memory and
CPU
your function is allocated in the environment, it's permissions, dependencies and
many other aspects
of how the function runs. If you have one or 1000 incoming triggers, AWS Lambda
will scale your
function to meet demand, each in its own isolated environment. Lambda is currently
designed to run code that has a runtime of under 15 minutes. So, this isn't for
long running
processes like deep learning or batch jobs, you wouldn't host something
like a WordPress site on AWS Lambda. It's more suited for quick processing, like a
web backend for handling requests or a backend report processing service. One of
the best things about Lambda, is that you aren't billed
for code that isn't running, you only get billed for
the resources that you use, down to 100 millisecond intervals. To understand Lambda
more,
let's run through a quick demo. In this demo, I will
create a Lambda function that resizes images uploaded into the employee directory
application to be a uniform thumbnail size. It makes sense to create
this sort of logic with a Lambda function. You don't need an application
to be running 24/7 on EC2 to resize photos uploaded
to the application. You really only need
the resize logic to run when a new photo is uploaded. So, here's our diagram for
the app. What I want to do is add this. When a new photo is uploaded to Amazon S3,
it triggers a Lambda function
that resizes the image and uploads it to the same S3 bucket but to a different
location than where the original image is stored. Okay, let's go ahead and build
this out. You can see that I'm already
in the AWS management console and I'm going to go ahead
and click create function. From here, you have a couple of choices of how you want
to create this function. We are going to author
this one from scratch and I already have the code written and downloaded to my
local machine. So we're going to author from scratch, we'll name this, resize
photo. And then we get to select the runtime. And that's basically the language
that you've coded your function to be. So for us, we coded this in Python. So I'm
going to select the Python runtime. And then I'm also going to expand this
permission section here and I'm going to use an
existing IAM role for this code. The Python code that we
wrote is using the AWS SDK to make API calls to S3. Therefore, those API calls must
be signed and authenticated, as we
discussed in earlier lessons. So, in order for this code to gain access to the
temporary credentials
needed to sign the request, we are going to associate an IAM role with the
appropriate S3 permissions to this Lambda function. So, I'm going to go ahead
and select the existing role which is called AWS Lambda S3 access, and then I'll
click, create function. At this point, we are now
seeing the designer view and I'm going to add a trigger
for this Lambda function. I will select a trigger and you can see here as I scroll
down, all of the different triggers
that are available for Lambda - - there are quite a bit -- and there are also
partner
event sources powered by a service called Amazon EventBridge. But I'm going to go
ahead and select S3. Then I will select the
employee photos bucket which is the bucket that our app is using. And then I will
use the
prefix, employee-pic, because I know that that is the prefix where the function is
uploading the images and then I will scroll down and I'm going to acknowledge this
recursive
invocation piece here. And this is essentially
just letting us know that if I were to have
this Lambda function read from the employee-pic prefix which is what triggered the
event, and then process the image and
upload it to the same spot, it would trigger another
Lambda function run and that would kind of just go on forever. So, you want to make
sure that you are using a
different place for the output than you are for the input. So I'm going to go ahead
and click add, and now I will scroll down and we are going to add the source code.
So, if you have source
code with no dependencies, you can just type it right in here which is really nice
for experimentation but for us, we're using
some outside dependencies for resizing the image in Python. So, I'm going to upload
a zip
file from my local machine. Then I'm going to click save. Then I'm going to scroll
down and change this Lambda handler. Go ahead and check out the class notes if you
want to learn more
about Lambda handlers and how this works. I'm just changing this
so that it's referring to my actual file and
then I'll click save. And now what I want to do, is test this through the
employee directory application. So I'm going to add a new employee, we'll name this
Alana, locations, USA, role is Cloud Technologist. She is a Mac user and then we
will upload
her photo and click save. So now it's been saved. I want to then test to see if
this worked. So I'm going to click monitoring here and then I want to view the
logs in CloudWatch Logs. So, CloudWatch Logs is
where the output goes from your Lambda function. I'm going to scroll down and
click on the latest log stream which is just the latest
collection of log files from the Lambda function. And we can see that we did get
the image
resized output here. So I know that this code ran in response to the incoming event
from S3. And that's it. That's how you create
an AWS Lambda function at a high level. You could host the entire
employee directory application's backend on Lambda with some refactoring. But, I
think I'm going to save that for a later conversation. If you're interested in
the code that we used and more information
about how Lambda works and handlers and how you
write code for Lambda, I'll go ahead and include
some resources for you in the reading section
following this video.

You might also like