Trigger Lambda functions with events from Solace

Jonathan Michaux

Jonathan Michaux

Mar 28, 2023
Trigger Lambda functions with events from Solace
Trigger Lambda functions with events from Solace

As part of the TriggerMesh 1.24 release, we announced our new connectors for Solace PubSub+. TriggerMesh can now read and write to this highly versatile messaging platform.

In this post, we’ll demonstrate how you can use TriggerMesh to trigger an AWS Lambda function when an event lands on a Solace queue. The post focuses on running the event flow on a computer with Docker, but we’ll also show how you can easily transition to run this in a Kubernetes-native way, by leveraging TriggerMesh’s declarative Kubernetes CRDs, such as the one for the Solace source component.

The Solace source will subscribe to a Solace queue and forward events to the TriggerMesh broker. The TriggerMesh broker will route events to a Lamba target component, which invokes the AWS Lambda function by passing it the event from Solace.

Start Solace and create a queue

An easy way to run Solace is to use their provided docker-compose file. 

git clone https://github.com/SolaceLabs/solace-single-docker-compose.git
cd solace-single-docker-compose/template
docker-compose -f PubSubStandard_singleNode.yml up -d

Open the Solace console at http://localhost:8080. The default user/password is admin/admin. 

In the console, create a queue called source-queue.

Ingest events from Solace into TriggerMesh

In a terminal, make sure you have installed TriggerMesh’s CLI called tmctl, then create the 4 TriggerMesh objects that will let us route events from the source-queue to the target-queue.

tmctl create broker solace

tmctl create source solace --url amqp://host.docker.internal:5672 \
                           --queueName source-queue
                           
tmctl create target awslambda --arn arn:aws:lambda:us-east-1:043455440429:function:myPythonFunc \
                              --auth.credentials.accessKeyID <access key>
                              --auth.credentials.secretAccessKey <secret key>
                              
tmctl create trigger --eventTypes io.triggermesh.solace.event \
                     --target solace-awslambdatarget

It is crucial that you specify the eventTypes filter for the trigger as shown here. If you don’t, responses from the Lambda function back to the broker will get sent back to Lambda again, and so on, creating an event loop which could impact your AWS bill in a negative way. 

Also, I’m not giving explicit names to these components here, so they’ll default to names like <brokername>-<component type>, for example solace-awslambdatarget. Be sure to adapt these commands if you chose a different broker name, or if you’ve customized the names of components.

I’m using host.docker.internal as the hostname here for the Solace source because tmctl is running on Docker Desktop on Mac. You can adjust this according to your environment. 

Run tmctl describe to check that these 5 pieces are up and running. 

% tmctl describe
Broker     Status
solace     online(http://localhost:56591)

Trigger                     Target                     Filter
solace-trigger-3d85a689     solace-awslambdatarget     type is io.triggermesh.solace.event

Source                  Kind             EventTypes                      Status
solace-solacesource     solacesource     io.triggermesh.solace.event     online(http://localhost:56601)

Target                     Kind                Expected Events     Status
solace-awslambdatarget     awslambdatarget     *                   online(http://localhost:56610)

Run tmctl watch in a separate terminal so that you can see events land in the broker. 

Send a test event to the source-queue

Now send an event to the source-queue from the “Try me!” tab in the Solace console. Make sure you send a valid JSON payload like so:

Check that the event has made it to the TriggerMesh broker by viewing the CloudEvent logged by the watch command (note that the event type is io.triggermesh.solace.event):

Below that event, you might also notice an additional event: it’s the Lambda function’s response back to TriggerMesh. You can potentially use that response to trigger other actions, thereby allowing you to use the response returned by the lambda function. In my example, there is nothing interesting about the response, the Lambda returns Null.

Finally, let’s check that our Lambda function was successfully triggered. I’m looking at the Lambda’s logs in CloudWatchLogs here, in which we can see the Hello world! message encapsulated inside a CloudEvent: 

You can now easily run this flow on Kubernetes

First of all, you’ll need a Kubernetes cluster with TriggerMesh installed. You can find instructions on installing TriggerMesh on Kubernetes in the documentation. We’ve tested this on development clusters like Kind, Minikube, and Kubernetes for Docker Desktop.

Once your cluster is ready, you can export the event flow you just created with tmctl as a Kubernetes manifest that is ready to run:

tmctl dump > manifest.yaml

Here is an example of the Solace source component that is produced by the dump command:

apiVersion: sources.triggermesh.io/v1alpha1
kind: SolaceSource
metadata:
  labels:
    triggermesh.io/context: solace
  name: solace-solacesource
spec:
  queueName: source-queue
  sink:
    ref:
      apiVersion: eventing.triggermesh.io/v1alpha1
      kind: RedisBroker
      name: solace
  url: amqp://host.docker.internal:5672

You should be able to apply this manifest to your cluster using kubectl apply -f manifest.yaml, with a few considerations first:

  • Make sure you stop the broker running with tmctl first, by doing tmctl stop. This will avoid both environments competing for the same messages on the Solace queue
  • You may need to adjust the URL used for the Solace queue in the Solace source manifest. This is entirely dependent on your network configuration and which kind of Kubernetes distribution you are using. 

Come and join the TriggerMesh community

If you need help, you can easily reach the team on our public Slack. We love interacting with users and will answer any questions as best we can. 

And for a more general onboarding into everything TriggerMesh, head over to our quickstart guide which will help you to get up and running in minutes on any computer that has Docker. 

Thanks for reading and we hope to see you again soon!

Create your first event flow in under 5 minutes