AWS Made Simple: Create an AWS Lambda function from scratch
Tomasz Łakomy
—January 20, 2020
How do I create an AWS Lambda function?
First up, you'll need to log in to your AWS account.
If you don't have an AWS account, feel free to use my credentials:
email: tlakomy@jqueryIndustries.compassword: NeverShareYourCredentialsWithAnyone
Once you're in, navigate to Lambda (you'll find it under Compute section in Services menu) and click on Create function
, you should see something like this:
There are three options available here:
- Author from scratch - you'll start with a simple "Hello World" example and you'll be able to implement your function on your own
- Use a blueprint - allows you to build a lambda function using a "blueprint". A blueprint in this context means a piece of code and configuration for common use cases, for instance logging updates made to a DynamoDB table or triggering a function when something is uploaded to S3
- Browse serverless app respository - allows you to deploy entire sample applications from the AWS Serverless Application Repository. The packages can be either provided by AWS or by the community
The last two options are definitely worth checking if you have a feeling that the problem you're trying to solve has already been solved by someone else, no need to re:Invent (get it?) the wheel. Not to mention you can learn quite a lot by checking them out.
One more thing before we create our function:
AWS Lambda is not a global service
What that means it that you'll create your functions in one of the AWS regions:
I'm going to be deploying my functions in Frankfurt because it's only 143h by foot from my office:
Runtime
In order to create a function we need to specify what kind of runtime we're going to use - in other words, which programming language do we want to write our function in. There are a number of programming languages supported by AWS Lambda:
There's Go, JavaScript, Java, Python, .NET and Ruby - a little something for everyone. If you're coding on Halloween you might even decide to raise the dead and write your functions in Python 2.7 [*].
Okay, let's roll - we're going to choose Node.js as our runtime, leave everything as default and create a function.
Behold, a Lambda function designer
There are a couple of things to notice here.
First up - every Lambda function (this is also true for every resource of AWS) has a unique ARN (Amazon Resource Name) which you can see in the upper right corner.
Each function can have multiple triggers and destinations.
As discussed in my previous post, your functions can be triggered by various sources, including:
- AWS Gateway can trigger your lambda function by an HTTP request (useful when you want to create an API)
- AWS IoT - you can literally have a "push to prod" button on your desk
- AWS S3 - a lambda function can be triggered by uploading a file to an S3 bucket
- AWS DynamoDB - you can trigger a lambda function by for instance adding an item to your DynamoDB table
When it comes to destinations - your function invocation records can be sent to multiple places: an SQS queue, a SNS topic or an event bus. Useful when you'd like to keep a track of your lambda function failures (or receive an email every single time someone calls your function, terrible idea btw)
This is the our lambda function in its beauty:
exports.handler = async (event) => {// TODO implementconst response = {statusCode: 200,body: JSON.stringify('Hello from Lambda!'),};return response;};
Not much, huh?
It's a convention when it comes to AWS Lambda that every function implements a handler
function that gets called whenever a lambda gets triggered. In this case, we'll always return a response with HTTP code 200 (OK
) and a body 'Hello from Lambda!'
)
How do I test my function?
In order to test (and actually call) our function, we need to create a test event. To do that (you guessed it), click on the Test button.
Before we're allowed to test our function, we need a test event to be sent to it. Every time a lambda function gets executed, it'll receive an event
as its argument (you can see it in the example above)
Currently we're not doing anything with the event so let's call it "myTestEvent" (or whatever) leave everything as default and smash that save button
Okay, now that we're ready - let's click on Test again and see the result:
Congrats!! You've just ran your very first lambda function!
We can see the result we've specified earlier, as well as logs detailing the function duration, memory size, max memory used and billed (ka-ching!) duration. Note that you won't get charged for the first million invocations of your lambda function in a month.
Óne more thing!
If you don't feel like reading and prefer to watch a bunch of quick (less than 3 minutes) videos - this blog series is based on a 💰free 💰egghead.io video collection I've published recently, check it out here: Learn AWS Lambda from scratch.
I've also launched an AWS Cloud Development Kit course on egghead.io - make sure to check out Build an App with the AWS Cloud Development Kit!
AWS Cloud Development Kit allows you to build the entire stack (frontend, serverless backend, AWS infrastructure) using a single programming language - TypeScript!
🔥🔥🔥