Everything Cloud

Get started with AWS Serverless technologies — Lambda | API Gateway

AWS Lambda is a serverless computing platform that allows you to run code without provisioning or managing servers. API Gateway is a fully managed service that makes it easy to create, publish, maintain, monitor, and secure APIs.

In this blog, we will walk through the steps to create a simple Lambda function and expose it through an API Gateway.

Step 1: Create a Lambda function

  1. Go to the AWS Lambda console and click the “Create function” button.
  2. Select the “Author from scratch” option and give your function a name.
  3. Select a runtime for your function. For this example, we will choose Node.js.
  4. Leave the default options for the Role and Policy templates, and click the “Create function” button.

Step 2: Configure the trigger

Next, we need to configure the trigger that will invoke our Lambda function. In this case, we want to trigger the function when an HTTP request is made to the API Gateway. To do this, follow these steps:

  1. In the designer view, click the “Add trigger” button.
  2. From the list of trigger options, select “API Gateway”.
  3. Select “Create a new API” and choose “Open” for security.
  4. Click the “Add” button to create the trigger.

Step 3: Write your Lambda function code

Now it’s time to write the code for our Lambda function. In this example, we’ll create a simple function that returns a greeting when an HTTP request is made to the API.

To do this, open the inline code editor in the Lambda dashboard and replace the default code with the following:

exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Hello from Lambda!',
}),
};
return response;
};

Click the “Save” button to save your function code.

Step 4: Test your Lambda function

  1. In the top right corner of the function code editor, click the “Test” button.
  2. In the “Configure test event” dialog, create a new test event by giving it a name and clicking the “Create” button.
  3. In the “Test” tab, click the “Test” button to test your function.

If the test is successful, you should see a message saying “Execution result: succeeded” in the “Execution results” section.

Step 5: Configure the API Gateway

Now that our Lambda function is working, we can connect it to the API Gateway. To do this, follow these steps:

  1. In the designer view, click the “API Gateway” trigger.
  2. From the “API Gateway” settings, click the “API name” dropdown and select the API you created in step 2.
  3. Click the “Deploy” button to deploy the API.

Step 6: Test the API

Finally, let’s test the API to make sure it’s working as expected. To do this, follow these steps:

  1. From the “API Gateway” settings, copy the “API endpoint” URL.
  2. Paste the URL into a web browser and hit “Enter”.

If everything is working correctly, you should see a JSON object containing the response

Vishal Kanade
Vishal Kanade

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Featured Today

Socials Share

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Discover

Related Posts