Create your first microservice with AWS Lambda

Create your first microservice with AWS Lambda

Cloud, serverless, microservices … if you’ve followed the latest trends in web development, you’ve certainly heard about it all. What is moving part or all of your development cycle to the cloud? What does it even look like? AWS Lambda is a solution that allows you to create microservices using the serverless architecture paradigm.Instead of creating a server from scratch and wasting time and resources on hosting and configuration, Lambda provides something we define as Function as a Service – this allows you to quickly and easily write server-side code in the cloud.

The code runs with a specific event. This event can be anything from a request to the gateway API to the appearance of a new file on S3. A popular example here is resizing or creating a thumbnail: a user uploads a photo via an app that we store on the S3. Loading starts a piece of code that adjusts the image size to the desired dimensions and places the image itself in the target bucket.

The location of the resized image is then passed to the client that created the request. The Uri, on the other hand, is stored in the database for the future. If the above is completely new to you, then this article will help you. AWS documentation is at best confusing and at worst simply unclear – especially if you have no prior experience working in the cloud or as a DevOps.

We will write a simple Lambda function “hello, world”, which will be launched by the GET request in the API gateway – we will also build this with the rest. At the end of this tutorial you will be able to create your first microservice.

The first thing we’ll do is set up an AWS account. Go here to get started. However, remember that this whole process requires you to enter your credit card details. However, Amazon has a free option that lasts for 12 months.

What’s more, Lambda gives you unlimited free access, which means that if your annual AWS access ends, you can still use lambda for free if you use it within the limit. Exceeding the limit will, of course, cost, but the limits are so large that they can only be exceeded when there is a lot of production traffic. If you have any questions or concerns, please go here.

Then create an account in the AWS Management Console. In the” find Services ” field, type Lambda and press enter. Select “Create function” and then “author from scratch”. Fill in the basic information by selecting node.js as runtime. In the” Execution role “field, select ” Create a new role with basic Lambda permissions.”And here’s my setup:

When this is done, press “Create function”. According to the information, it may take a minute or two to create and configure the executive role. Then you will see the implementation of the function helloWorld. You can take a look at the designer and go to the code of our function.

What’s important now is that when the Lambda function is executed, AWS looks at index.js a function called handlerwhich is attached to the object exports. Remove code from index.js and add the following. Remember that the function absolutely must be marked as async for everything to work as it should:

exports.handler = async (event) => {
const name = event['name'];

const response = {
statusCode: 200,
body: JSON.stringify(`Hello, ${name}!`)

return response;

Click the “test” button, which is located at the top of the page. Create a new test event with the name key and the value set to your name. This is what it looks like for me:

Click the “Create” button at the top and make sure you select the event name from the dropdown menu. Then press “test” again. The result should show something like this:

"statusCode": 200,
"body": ""Hello, Spike!""

Next, we will fire up the gateway API, which will run our lambda function so that it is generally available. Click the “+ add trigger ” button in the designer.

Select” API gateway “from the dropdown menu, and then click”Create a new API”. Then we can finally choose “Open” in security. Click “Add”, which will create a new API.

We need to configure the gate settings now. Make sure you select” API gateway ” in the designer and click the link at the bottom of the page. I have to click the link from helloWorld-API:

In the dropdown menu called “actions” select “Create method”. The next dropdown menu should appear in the resource tree. Choose there GET and click the check mark to create an action. In the right panel we need to do some configuration for our endpoint.

Type the name of the lambda function in the input field at the bottom of the page. Then click “save” and “OK”. This is what it looks like for me:

Then, in the panel, click the” method request “link, followed by”URL query string parameters ” and “add query string”. Then type “name” and click the check mark.

Now we need to click the” method execution “link at the top to go back to the configuration page-then we need to click the” Integration request “and” mapping templates “link at the bottom, then” add mapping template ” and type application/json. At the end we click the check mark.

Next, we click ” Yes, secure this integration” and add the following in place for the text:

"name": "$input.params('name')"

Click “save” at the bottom and go back to the configuration by clicking “method execution” at the top of the page.

When you click “Save”, there will be no indication that the mapping template has been saved. However, we can be sure that this has happened and continue. On the configuration page, click “TEST” and enter name=Spike in the text box under query strings.

You can enter your own name. Click “test” at the bottom and if all goes well you will see a welcome with your name.

One more thing and it’s over. In the”actions “menu on the left we select “Deploy API”. Then select Default as the deployment phase, enter a description and click “deploy”. This is what it looks like for me:

In the panel on the left, click the disclosure triangle, which is located next to “default”, to open the tree. Then we click the GET method and on the right we will see a link through which we can call our function. I will use Postman to test the endpoint-you can use another tool (e.g. curl, or just a browser). I will now pass the name key in the parameters with the value spike.

When you send a request, you’ll see a welcome along with your name in the response:

That’s it. We just deployed our first microservice. I hope that this will open you up to different possibilities and that you will use AWS with greater ease and confidence.

You can read the original English text here.

Go to our cases Get a free quote