A Beginner's Guide to AWS Lambda: Creating Your First Function
A Beginner's Guide to AWS Lambda: Creating Your First Function
AWS Lambda is a revolutionary compute service introduced by Amazon that allows developers to run code without provisioning or managing servers. You simply pay for the exact compute time your code consumes—nothing more.
In essence, you upload your business logic to the Lambda service, and your code is executed automatically in response to specific triggers, such as an HTTP request or an event within the AWS ecosystem. Because you no longer need to worry about maintaining or scaling underlying infrastructure like EC2 instances, Lambda is highly optimized for running decoupled, event-driven tasks.
Lambda also features a generous free tier, offering 1 million requests and 400,000 GB-seconds of compute time per month. Unless you are running a high-traffic production service, you can likely start experimenting with Lambda completely for free.
1. Creating a New Function in the Console
When you navigate to the AWS Lambda dashboard, you will find a Create a Lambda function button.
Since Lambda operates on an event-driven model—meaning functions execute only in response to a predefined event—you generally need to create one specific Lambda function for each unit of work.
2. Bypassing the Blueprints
Before you start configuring your new function, AWS will present a screen prompting you to choose a blueprint. Blueprints are essentially pre-configured templates that you can use as a starting foundation. While a basic "hello world" blueprint is available, for the purpose of learning, it is often best to skip this step and start from scratch.
3. Configuring Function Details
Next, you will arrive at the primary configuration screen. Here are the core settings you must define:
- Name: Assign a clear, descriptive name to your function, such as
HelloWorld. - Runtime: Choose the programming language and environment. For this tutorial, we will use Node.js.
- Handler: This tells Lambda exactly which method to invoke when an event triggers the function. If you specify
index.handler, AWS will look inside yourindex.jsfile for an exported function namedhandler. - Role: This defines the permissions your function requires. If your Lambda function needs to interact with other AWS services (like reading from an S3 bucket or a database), you must create an AWS IAM Role with the appropriate permissions and attach it here.
- Memory and Timeout: At the bottom of the page, you can allocate memory and set an execution timeout limit. For testing purposes, the default values are sufficient. You can monitor your logs later to fine-tune these settings.
4. Writing the Handler Code
The handler function must follow a specific signature, accepting two main parameters:
- event: An object containing the data passed to the function when it was triggered (e.g., the payload of an HTTP request).
- context: An object providing runtime execution information and methods to interact with the Lambda execution environment.
Here is an example of a simple Node.js handler:
console.log('Loading function');
exports.handler = function(event, context) {
console.log('event: ', JSON.stringify(event));
var name = event.myname || 'Anonymous';
context.succeed('Hello World, ' + name);
};
When triggered, this code logs the incoming event data. It checks if a myname key exists in the event payload. If it does, it returns a personalized greeting; otherwise, it defaults to greeting "Anonymous".
We utilize the context.succeed() method to signal to Lambda that the function has successfully completed its execution.
5. Review and Provision
After filling out your configuration, click "Next" to review your settings. Double-check your details on the review screen, and if everything is correct, click Create function to provision your new Lambda.
6. Testing Your Lambda Function
Once created, you will be redirected to the function's dashboard. To verify your code works, click the Test button or select Actions -> Configure test event.
Because our sample code expects a myname value in the event payload, you should provide a simple JSON object containing that key for the test event.
Run the test, and you will see the execution results at the bottom of the console. The log output (which is also permanently stored in Amazon CloudWatch) will confirm whether the function behaved as expected.
7. Connecting Event Sources and API Endpoints
Your Lambda function is now ready, but it needs a trigger to actually execute.
Event Sources: You can configure internal AWS services—such as an S3 file upload, a DynamoDB table update, or a CloudWatch alarm—to automatically trigger your function.
API Endpoints: If you want your function to respond to standard web traffic (HTTP requests) from external clients like mobile apps or front-end websites, you must connect it to AWS API Gateway. API Gateway acts as the "front door," converting incoming HTTP requests into the event payload that your Lambda function processes.
Getting started with AWS Lambda opens the door to building highly scalable, modern serverless applications without the operational overhead of traditional server management.