Once logged into our account, in the services menu select the Lambda option.

It shows three different options, select “Crear desde cero” to create from scratch. First lets give a name for the function, in this case we’re going to convert a text from upper case to lower case passing the text with a POST method. Select Node.Js as language (AWS have a lot of different language to choose).

Inside the panel of the Lambda, go to the code section.

As we can see, the handler object is that returns the Lambda, so we must always export a handler.

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

Pass the function up, and down export the handler, in the end it’s the same but a little easier to understand. (The function is now assigned to the export handler).

const toLowerCase = async (event) => {
        // TODO implement
        const response = {
                statusCode: 200,
                body: JSON.stringify('Hello from Lambda!'),
        };
        return response;
};

exports.handler = toLowerCase;

The “Event” param it’s what we send within the POST or PUT (According to the case).

In the space where is the comment // TODO implement we define our variable to obtain something that comes inside event, for this example: let newSentence. My sentence will be the same as what comes inside event.sentence. (Inside the POST comes a property called “sentence” and its a string)

We will apply the native function toLowerCase, assign a variable of whatever comes in sentence and convert it to lowercase. And in the body returns an object that we convert to json and inside this object we’re going to put the sentence already converted.

const toLowerCase = async (event) => {
        let newSentence = event.sentence.toLowerCase();
        const response = {
                statusCode: 200,
                body: JSON.stringify({ newSentence }),
        };
        return response;
};

exports.handler = toLowerCase;

Click on “Deploy” to apply changes.

To test the code we wrote, simply go and click where it says “Probar”, then we create a test case, put any name and in the example object we assume that inside the event comes a property called “sentence” and the text “HELLO WORLD THIS IS CARLOS WITH LAMBDA”.

Click on create button. And click on test to see the result, it returns a 200 status and the sentence converted to lowercase.

This would be a simple and common Lambda with Node.Js.

Now the most important thing is how we could export this through a REST API.

In the services menu we select “API Gateway” in the options that appear we will select the one that says REST API (The REST API option that doesn’t say private), because in this case we don’t require any type of security. Click on “create” and close the following message.

Select “New API” option, give it a name, the description can be empty and default type endpoint.

In this panel we select the resource, then click on actions, create resource and we give it a name “lower-case”, (this time we do not select anything from proxy or CORS).

Now on the resource select it, click on actions again, then click create method and select POST.

Press the check button to configure it, in the integration type we leave the option of “Lambda Function”, in the Lambda Function field we write the name of our function and a list will appear with our functions, select it and press save button. In the following message accept. And we are almost finishing, the only thing that remains is to deploy this implementation.

Finally in “Actions” select “Implementar la API”, in the field “Etapa de implementación” select “Nueva Etapa” , set a phase name (we call this phase: “test” to simulate a QA stage) and click on “Implementación” and save changes.

Now we can copy the endpoint.

And inside postman select create a new request, define the POST method, paste the URL of the endpoint and in the raw we write the json {“sentence”: “your sentence”}. After send the request we see that it returns the sentence in lowercase. :D

As we can see, this example is something very basic, but the objective was to be able to explain how a lambda function can be written in a specific language and exposed in a REST API.

Now maybe you think “it is very cool, but what is it for?”. In my next post I will be write more about what is a Lambda, for which cases can I use a Lambda, and related issues more in depth.