Setting up a serverless infrastructure with AWS. API Gateway + AWS Lambda + AWS SQS

The idea: to have a system where we don’t have to worry about the implementation specific details of a solution. We’re going call an API which triggers a script that adds items to be elaborated to a queue.

Creating an AWS API Gateway

In our Amazon Console let’s open the API gateway and create a new one.

Next step is to add some methods, depending on what we need. In our simple case, we just need to implement a POST action:

Where we can create our post method:

And decide to trigger a Lambda Function when the API is called.

The Lambda functions that can be sent are depending on the region, so make sure to select the correct one.
At this point of the tutorial, you might be not able to save it yet because you need to have Lambda call first.

Creating an SQS Queue

Once we create the Queue we need to add permissions to it to be accessed. Clicking in the list view we have the Permissions tab. To keep things simple I am just going to open the permissions to everybody and with the ability to do any action. Of course in a real-life scenario, we should have a granular specification.

Creating a Lambda Function to add items to an SQS Queue

Simply let’s create a new Lambda function from scratch, with the “Author from scratch” button.

We can use NodeJS, Python, Java or .NET.

var QUEUE_URL = '{AWS_ACCUOUNT_}/test-message';
var AWS = require('aws-sdk');
var sqs = new AWS.SQS({region : 'southeast-2'});

exports.handler = function(event, context) {
  var params = {
    MessageBody: JSON.stringify(event),
    QueueUrl: QUEUE_URL
  sqs.sendMessage(params, function(err,data){
    if(err) {
      console.log('error:',"Fail Send Message" + err);
      context.done('error', "ERROR Put SQS");  // ERROR with message
    } else {
      context.done(null,'');  // SUCCESS 

Complete the set up of the AWS API Gateway

Now that we have our Lambda function we can go back to the Gateway API and insert the correct Lambda function that we have just created.
And then we should be able to get to this screen:

Before testing though we have to deploy the API to use it. You can refer to this other article to do it. All is required is to click on the actions button and then select deploy API and follow the steps.

If everything went well we can now click the test button send the data to the queue and see the result.
To trigger it from a browser we can run something like:

fetch('', {
  method: 'post',
  body: JSON.stringify({id: "123465798", name: "test" })
}).then(function(response) {
  return response.json();
}).then(function(data) {

Or with CURL:

curl -H "Content-Type: application/json" -X POST -d '{"id":123}'