Integrating PubSub+ Cloud with Lambda via API Gateway
Tags: aws, pubsub, serverless, lambda
Published: 2020-05-21 14:45:35

Serverless architecture is all the rage these days and nothing screams serverless like AWS Lambda. When Lambda was announced a few years ago by AWS, it caused quite a bit of confusion but slowly it has gained popularity and has become analogous to serverless architecture.

> **AWS Lambda**  is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. 
> 
> <cite><a href="https://aws.amazon.com/lambda/features/" target="_blank" rel="noreferrer noopener">AWS</a></cite>

Lambdas are super powerful because they can be easily triggered and scaled without having to worry about the underlying infrastructure.

In this post, I would like to show you how you can send a message to a PubSub+ broker running as a managed service on PubSub+ Cloud and use it to trigger a lambda through AWS’s API Gateway. And all of this without any code, except for the code you need for your Lambdas.

More specifically, we will publish a message to a topic that is being subscribed to by a queue. This is known as topic-to-queue mapping in the PubSub+ world. Once the message is received by the queue, it will be forwarded to API Gateway which will trigger the Lambda function.

We will be using REST Delivery Points (RDPs) to achieve this. An RDP is a provisioned object on a Message VPN that facilitates message delivery to REST consumers. For our use case, we will need an RDP to link our queue (which will receive the message) to the AWS API Gateway endpoint.

Here is what our architecture looks like:

![](http://abitdeployed.com/wp-content/uploads/2020/05/solace_aws_architecture-1024x261.png)

Let’s begin!

#### Creating an AWS Lambda

![](http://abitdeployed.com/wp-content/uploads/2020/05/lambda-1.png)

_Lambdas_ can be extremely simple or highly complicated. For this blog post, I am going to create an extremely simple one that simply prints the event that triggered it.

To create a Lambda function, go to your AWS Console and select `Lambda` from Services section. Then, click on `Create Function` button on the right side.

You will be provided with multiple options on how to create a Lambda. You can pick from existing Lambdas available in AWS Serverless Application Repository, build from templates or create a new one from scratch.

Pick the option `Author from scratch`, give your function a name, and pick a runtime environment for your function. I have picked my Runtime environment to be `Python 3.8` and my function’s name is `myFirstLambda`. Click on `Create Function`.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-1024x519.png)

Clicking on `Create Function` will create your Lambda function and will take you to the next page where you can design/code your function. In the `Function code` block, you can add your code. As mentioned earlier, we will only be printing our event that triggered the Lambda. Here is the code to do that:

```
import json

def lambda_handler(event, context):
    # TODO implement
    print('## EVENT')
    print(event)
    return {
        'statusCode': 200,
        'body': event
    }
```

Click `Save` button on top right to save your changes. We have now created a Lambda function!

#### Creating an API Gateway endpoint

![](http://abitdeployed.com/wp-content/uploads/2020/05/aws-api-gateway-icon-1024x1024.png)

Now that we have our Lambda, we need an endpoint to trigger it. We will use [API Gateway](https://console.aws.amazon.com/apigateway/) to do that.

On the API Gateway page, click on the `Create API` button on top right. You will then be presented with different options on types of APIs you can create such as `HTTP API`, `WebSocket API`, and `REST API` (public or private). Pick `REST API` (public) and click on `Build` button.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-1-1024x427.png)

On the next page, leave all of the settings as default and give your API a name (i.e. `myFirstAPI`) and click on `Create API` button.

Note: Currently, PubSub+ does not support SNI for REST consumers so we need to make sure that our `Endpoint Type` is set to `Regional`, instead of `Edge Optimized`.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-2-1024x587.png)

Now, we can create different endpoints under our root endpoint and specify which HTTP methods they support. For example, let’s create one called `purchase` which supports `POST` method.

Click on `Actions` and then, `Create Resource` and fill in the details:

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-3-1024x380.png)

Click the `Create Resource` button when finished. Then, click on `Create Method `and select `POST` from dropdown menu. Click the little circle with checkmark to finish.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-4.png)

You can now configure what happens when a `POST` request is issued against your endpoint. We would like it to trigger our Lambda function.

Again, keep all the settings as default and select your Lambda function from dropdown menu:

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-5-1024x419.png)

Click on `Save` to save your changes.

Now that we have designed our API, we need to deploy it to be able to use it. We can also create different `Stages` to manage lifecycle of our endpoint.

Click on `Actions` and then, `Deploy API` which will pop-up a mini window where you can specify the stage you want to deploy your API to and provide a description for your deployment. Because we don’t have an existing stage, you will see `[New Stage]` option when you click on `Deployment stage` dropdown menu.

Select `[New Stage]` and enter information about your stage. I am calling mine: `PROD`.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-6-1024x652.png)

Click `Deploy` when finished.

You will now find details about your newly deployed endpoint, specifically, the invoke URL. Mine is: `https://1v3hgu8bia.execute-api.us-east-1.amazonaws.com/PROD`

We have now successfully created an endpoint which will trigger our Lambda function when a `POST` request is issued against it. All that’s left to do now is to integrate our endpoint with PubSub+ Cloud!

#### PubSub+ Cloud

![](http://abitdeployed.com/wp-content/uploads/2019/10/solace_pubsub.jpg)

If you are unfamiliar with [PubSub+ Cloud](https://console.solace.cloud/login/new-account), it is Messaging-as-a-Service provided by Solace. PubSub+ Cloud allows you to easily spin up a PubSub+ event broker on any of the major cloud providers. It also has a free tier so you can easily spin up a service to follow along with this post without having to worry about any cost.

Follow instructions [here](https://docs.solace.com/Solace-Cloud/cloud-getting-started.htm) on how to create a service on Solace Cloud.

#### Creating a Queue with topic subscription

Before we create our RDP, we need to create a queue with the appropriate topic subscription.

Go to your messaging service, click on `Manage` tab, and then click on `Message VPN` block under `PubSub+ Manager Quick Settings`.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-7-1024x519.png)

That will popup a management UI for your service which you can use to create your queue. Click on `Queues` on the left navigation bar and then click on `+ Queue` button. Give it a name of the form `[rdpname].[consumername]`. This syntax is not mandatory but it will help you identify which queue is bound to which consumer and for which RDP. Because we haven’t created either an RDP or a consumer, you can call it whatever you like. I am calling mine: `myFirstRDP.myRDPConsumer`.

Click `Create` button and then leave the settings as default, click `Apply`. Your queue has now been created:

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-8-1024x178.png)

Now we need to add a topic subscription to it. To do that, click on the queue and navigate to `Subscriptions`. Click on `+ Subscription` and enter `POST/PROD/purchase` and then, click on `Create`. Note that our topic must be of this syntax: `<HTTP_METHOD>/<STAGE>/<endpoint>`.

#### Creating a REST Delivery Point (RDP)

Now that we have our queue ready, we can go ahead and create our RDP. To do that, click on `Client Connections` on left navigation bar and then `REST`. Then, click on `+ REST Delivery Point` button. As decided earlier, we will call our RDP: `myFirstRDP`. Toggle the `Enabled` button to enable the RDP and click `Apply`. You will notice that the `Operational State` is currently `Down` and that’s because we haven’t added a REST consumer yet.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-9-1024x205.png)

Click on your RDP and navigate to `REST Consumers`. Click on `+ REST Consumer` and give it a name. We decided earlier (when creating our queue) that our REST consumer will be called: `myRDPConsumer`. Click `Create`.

On the next page, we will need to provide the host url for our AWS API Gateway endpoint. This URL is just the base URL and should not include `/PROD/purchase`. In my case, that URL is: `1v3hgu8bia.execute-api.us-east-1.amazonaws.com`

The port number should be set to `443`. Toggle the button next to `TLS Enabled` to enable TLS.

Click on `Apply` button to create your consumer.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-11-1024x470.png)

Click on your newly created RDP consumer and navigate to `TLS Options` on top. Click on `+ Trusted Common Name` and enter `*.execute-api.us-east-1.amazonaws.com`.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-12-1024x318.png)

Go back to your RDP and enable it by clicking on the toggle button next to `Enabled`. You will notice that the `Operational State` has now changed to `Up`.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-13-1024x190.png)

We now need to add our queue binding to our RDP. Click on `Queue Bindings` and then, `+ Queue Bindings`. Select the queue we had created earlier, `myFirstRDP.myRDPConsumer`, from the dropdown menu. Click Create.

On the following page, enter a `Post Request Target`. In our case, that would be `/PROD/purchase`. Make sure you include the “/” in front.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-14-1024x215.png)

Click `Apply`. `Operation State` of your queue binding should be `Up`.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-15-1024x201.png)

#### Adding Amazon’s CA Cert

To make sure our TLS settings work, we need to upload Amazon’s CA Cert to our broker. The certificate can be downloaded from [here](https://www.amazontrust.com/repository/AmazonRootCA1.pem).

To upload the certificate, go back to your service on PubSub+ Cloud and click on `Certificate Authorities` block.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-16-1024x326.png)

Click on `+ Add New`, give it a name, and paste the content in the popup box. Click on `Submit`.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-17-1024x608.png)

That should be it! We can now test to see if our setup works.

#### Demo Time

A quick recap. Our goal is to send a message to PubSub+ broker and have a lambda triggered by it. To achieve this, we created an RDP which has a REST consumer that points to our AWS API Gateway endpoint and a queue binding which is subscribed to a topic. Our AWS API Gateway endpoint is configured to trigger our Lambda function when a POST request is issued.

So, let’s send a message to our PubSub+ broker and see what happens. We can easily do that by using the `Try Me!` app on PubSub+ Cloud. Click on `Try Me!` on left navigation bar and enter your connection information on the Publisher app.

You can get your credentials by going back to your service and navigating to Connect tab on top and then selecting REST.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-19-1024x185.png)

Click on `Connect` once you have entered your connection details in the `Publisher` app.

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-18-1024x800.png)

We can now set our `Topic` to `POST/PROD/purchase` and our `Message Content` to a JSON message:

```
{
"body" : "A purchase order has been placed by Solace"
}
```

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-21-998x1024.png)

Click on `Publish` to send the message. Now, we need to verify whether our Lambda was invoked. To do so, we can go to our Lambda and click on `Monitoring` on top and then, click on `View logs in CloudWatch.`

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-24-1024x125.png)

That will take you to CloudWatch console where you will see a `Log Stream`. Click on it to see the contents:

![](http://abitdeployed.com/wp-content/uploads/2020/05/image-25-1024x301.png)

As you can see, our Lambda was successfully invoked and it consumed the message we sent it from our Try Me! app. We were able to accomplish this with mostly configuration settings and without having to code anything.

Hope you enjoyed this post and learned something interesting!