Using AWS Lambda to run code without managing servers

Introduction

AWS Lambda is a compute service that lets you run your code without provisioning or managing servers. It is a serverless architecture that executes your code only when needed and scales with your usage. Lambda automatically manages compute resources, so you don’t have to worry about server maintenance, capacity planning, scaling, patching, or deployment. With Lambda, you can focus on writing code and building applications, not on managing infrastructure. In this tutorial, we will walk you through the process of using AWS Lambda to run your code without managing servers.

Pre-requisites

Before we get started, you need to have the following:

  • An AWS account
  • Basic understanding of programming concepts
  • Basic understanding of AWS services
  • AWS CLI installed on your local machine

Creating a Lambda Function

To create a Lambda function, you need to follow these steps:

  1. Open the AWS Management Console and navigate to the Lambda console.
  2. Click the Create function button to create a new function.

  3. Provide a name for your function, a runtime environment, and a role.

  4. Choose the runtime environment that best suits your needs. Lambda supports various programming languages, including Node.js, Python, Java, and C#.

  5. Choose a role that grants your function the necessary permissions to access other AWS resources, such as S3, RDS, and DynamoDB. If you don’t have a role, you can create one by selecting Create a new role with basic Lambda permissions.

  6. Click on Create function.

Creating Function Code

Now that you have created a function, you need to write code for it. In this tutorial, we will use Node.js to write our function code.

  1. Open the function dashboard and scroll down to the Function code section.
  2. Click the Edit code button to open the inline code editor.

  3. Replace the existing code with the following sample code:

exports.handler = function(event, context, callback) {
    console.log('Hello World!');
    callback(null, 'Lambda function executed successfully!');
};
  1. Click the Save button to save the function code.

Testing the Function

Now that you have written your function code, it’s time to test it.

  1. Click on Test button on your function dashboard.
  2. In the Configure test event dialog box, enter the name and other details for your test event.

  3. Click on the Create button to create the test event.

  4. Click on the Test button again and wait for the results.

You should see the output “Hello World!” and “Lambda function executed successfully!” in the Log Output section. This confirms that your function is working correctly.

Invoking the Function via AWS SDK

You can also use the AWS SDK to invoke your function programmatically.

  1. Open a new file in your text editor and save it as index.js.
  2. Add the following code to the file:

const AWS = require('aws-sdk');

const lambda = new AWS.Lambda();

exports.handler = function(event, context, callback) {
    const params = {
        FunctionName: 'my-function',
        Payload: JSON.stringify(event),
    };

    lambda.invoke(params, function(error, data) {
        if (error) {
            console.log(error);
            callback(error);
        } else {
            console.log(data);
            callback(null, data.Payload);
        }
    });
};
  1. Save the file and exit the text editor.

  2. Install the AWS SDK using npm:

npm install aws-sdk --save
  1. Open the AWS Management Console and navigate to the IAM console.

  2. Create a new IAM user with programmatic access and attach the AWSLambdaFullAccess policy to the user.

  3. Copy the Access key ID and Secret access key of the user.

  4. Open the terminal and configure the AWS CLI with the access key ID and secret access key of the user:

aws configure
  1. Enter the access key ID and secret access key of the IAM user.

  2. Create a test event file and add the following code:

{
    "test": "value"
}
  1. Save the file as test-event.json.

  2. Invoke the function using the following command:

aws lambda invoke --function-name my-function --payload file://test-event.json output.txt
  1. Check the output.txt file to see the results of the invocation.

Conclusion

In this tutorial, we walked you through the process of using AWS Lambda to run your code without managing servers. We started by creating a Lambda function, adding function code, and testing it. We then showed you how to invoke the function programmatically using the AWS SDK. Now that you have learned how to use AWS Lambda, you can explore its other features, such as triggered events, AWS Serverless Application Model (SAM), and more.

Related Post