Getting Started with AWS Lambda

Roman Sypchenko
3 min readApr 10, 2023

--

Introduction

In this article, we will provide a step-by-step guide to getting started with AWS Lambda using Node.js, complete with code examples.

1. Setting up an AWS account and installing the AWS CLI

Step 1.1: Sign up for an AWS account

Step 1.2: Install the AWS CLI (Command Line Interface)

Step 1.3: Configure the AWS CLI

  • Run the command aws configure in your terminal and enter your access key, secret key, default region, and output format when prompted.

2. Creating a Lambda function

Step 2.1: Create a project directory

  • Create a new directory for your Lambda function on your local system and navigate to it in the terminal.

Step 2.2: Write the Lambda function code

  • In the project directory, create a file named index.js with the following code:
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};

This simple Lambda function returns a “Hello from Lambda!” message with a 200 status code.

3. Deploying the Lambda function

Step 3.1: Package the Lambda function

  • In the terminal, navigate to your project directory and create a zip file containing index.js. You can use the following command on Linux or macOS:
zip function.zip index.js

Step 3.2: Create an IAM role for the Lambda function

  • In the AWS Management Console, navigate to the IAM (Identity and Access Management) service.
  • Click on “Roles” in the left sidebar and then click on “Create role.”
  • Select “Lambda” as the service that will use this role and click “Next: Permissions.”
  • Attach the “AWSLambdaBasicExecutionRole” policy and click “Next: Tags.”
  • Add any desired tags and click “Next: Review.”
  • Enter a name for the role (e.g., “lambda_basic_execution”) and click “Create role.”

Step 3.3: Deploy the Lambda function using the AWS CLI

  • Run the following command in the terminal, replacing YOUR_ROLE_ARN with the ARN (Amazon Resource Name) of the IAM role you created in Step 3.2:
aws lambda create-function \
--function-name myLambdaFunction \
--runtime nodejs14.x \
--role YOUR_ROLE_ARN \
--handler index.handler \
--zip-file fileb://function.zip

4. Testing the Lambda function

Step 4.1: Test the Lambda function using the AWS CLI

  • Run the following command to test your Lambda function:
aws lambda invoke \
--function-name myLambdaFunction \
--payload '{}' \
response.json

The response.json file should contain the response from your Lambda function, which should look like this:

{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}

5. Creating an API Gateway to expose the Lambda function

Step 5.1: Create a REST API using the AWS Management Console

  • Navigate to the API Gateway service in the AWS Management Console.
  • Click on “Create API” and then select “REST API.”
  • Choose “New API” and provide a name and description for your API.
  • Click “Create API.”

Step 5.2: Create a resource and method for the API

  • In the “Resources” section of your newly created API, click on “Create Resource.”
  • Provide a name for the resource (e.g., “HelloLambda”) and click “Create Resource.”
  • With the new resource selected, click on “Create Method” and choose “GET” from the dropdown menu.
  • In the “Integration type” section, select “Lambda Function.”
  • Choose the same AWS region where you deployed your Lambda function.
  • In the “Lambda Function” field, enter the name of your Lambda function (e.g., “myLambdaFunction”).
  • Click “Save” and then click “OK” in the confirmation popup.

Step 5.3: Deploy the API

  • In the “Actions” dropdown menu, click on “Deploy API.”
  • Select “New Stage” and provide a name for the stage (e.g., “prod”).
  • Click “Deploy.”

Step 5.4: Test the API

  • After deployment, you will see an “Invoke URL” for your API. You can use this URL to test your Lambda function using a tool like curl or Postman.

Conclusion

In this article, we covered setting up an AWS account, installing the AWS CLI, creating a Lambda function, deploying the function, testing it, and creating an API Gateway to expose the function. With this foundation, you can now explore more advanced use cases for AWS Lambda and integrate it with other AWS services to build powerful serverless applications.

--

--