aws lambda tutorial
Javascript Node.js

AWS Lambda Tutorial – Hello World and API Gateway

In this article, I’m going to show you how to create a hello world function on amazon web service Lambda and then integrate it with API Gateway so you can invoke it via browser or HTTP request.

Setup AWS Account

First, If you don’t have AWS account, you can get one with free usage tier for 12 months. Noted that you’ll still need to provide a credit card information for verification but no charge. Once you did that, login to the service console and click lambda. and click create function.

Create New Function

There are blueprints for you to pick – but for this tutorial, just click “Author from scratch”. Name your function and then assign the role.

aws-lambda-tutorial-1

 

Role defines your function permission policy, so let’s create a new role for this tutorial. — and then pick policy template from the list. simple Microservice permission is the most suitable for this job

aws-lambda-tutorial-2

Here you will be provided with a sample code. You can switch between the language you’d prefer but we’ll use node.js for this tutorial.

aws-lambda-tutorial-3

AWS labmda will invoke your exports handler so this is where it starts. You’ll have three parameters.

  • Event – This is where the AWS can pass the data or query string to your lambda function.
  • Context – provides the information about run-time of the current execution. For example, you can retrieve the memory limit or the remaining execution time before lambda terminate your function.
  • The callback is where you can pass the data back to lambda. First parameter is where you can send information in case of any error has been occurred. So we’ll pass null for a success run. The second the result of your function. Please noted that, you can pass in only JSON format or something that compatible with JSON.stringify. Your result argument will be ignore if there are any error.

So, to run and test your function, first you’ll need to create a test event which allows you to setup the event data for your function. Click Test on the upper right button. Then , config your test event something like this.

aws-lambda-tutorial-4

Now we have setup the event data, we can retrieve the value like this in our lambda function

exports.handler = (event, context, callback) => {
    callback(null, "key1 value is " + event.key1);
};

Click Save and test. and here is the result.

aws-;ambda-tutorial-5

See video below for all steps in action:

So that’s the basic of how to create a lambda function but in real life scenario, we do not invoke our function like this. Next, I’m going to show you how to integrate your lambda function with AWS API gateway.

Integrate Lambda with API Gateway

On your AWS service console, select API gateway. select new API and choose a name and description and click create API.

aws-lambda-api-gateway-1

First, you’ll need to create a resource. Your API can have many resouces which can be called separately by it’s own url path. Since we’re going to call our helloworld lambda function, let’s name it Hello world and click create Resource.

aws-lambda-api-gateway-2

Now we have the resource path, next we’ll create an access method for your API. Let’s use GET method.

aws-lambda-api-gateway-3

Choose lambda function for integration type. Choose your Lambda region and your function name

aws-lambda-api-gateway-4

Here is the diagram of your API flow. The client send a GET request which passthrough lambda integration. The function executed and pass back as HTTP response back to the client.

aws-lambda-api-gateway-5

Deploy your API

Now the API is ready – but before you can use, you’ll need to deploy it. Click action dropdown and select deploy API. You’ll need to specify which stage you’ll deploy your API to. For example, you can deploy API to your beta stage and test it. Once all defect has been identify and fixed. You can later deploy to prod stage when ready. For this tutorial, let’s deploy to prod stage directly.

aws-lambda-api-gateway-6

After that you have invoke url of your stage

aws-lambda-api-gateway-7

So we can call our API like this

https://7imcky3kh0.execute-api.ap-southeast-1.amazonaws.com/prod/helloworld

Now you can see that our function has been executed and return correctly except the undefined value. This because, in our lambda function, we display the key1 value on the event object. But since we didn’t pass any query string or data, it was displayed as undefined.

See the result in video below!

In next tutorial, I’m going to show you how to pass the data on API ur      l to the lambda function with event object. If you like this article, please don’t forget to like or subscribe our Facebook and Youtube Channel to support us 🙂

Leave a Reply

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

error: