Messaging & event-driven architectures
Messaging is a key part of building scalable and decoupled systems in AWS, and allows you to create an event-driven architecture.
Choosing your architecture
SQS-only
You can publish a message directly to an SQS queue. This is useful for point-to-point communication.
- Decouples producers and consumers
- Supports dead-letter queues, retries, delay queues
SNS with optionally SQS
Alternatively, you can publish a message to an SNS topic instead. This is useful for a pub/sub model, e.g. when different systems will need to subscribe to a topic independently. Every subscriber can have its own SQS queue.
- Pushes messages to multiple subscribers (email, HTTP, Lambda, SQS, etc.)
- Good for broadcasting events to multiple systems
EventBridge
EventBridge is an event bus for event-driven architectures, and can be used in combination with SQS queues.
- Schema registry, event routing with rules
- Ideal for loosely coupled microservices
Adding CDK constructs
To add your SQS queue, you can use the constructs from aws-cdk-lib/aws-sqs
:
import { Stack, StackProps, Duration } from "aws-cdk-lib";
import { Construct } from "constructs";
import { Queue } from "aws-cdk-lib/aws-sqs";
import { SqsEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
export class AppStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// const myLambdaFunction = ...
const myQueue = new Queue(this, "MyQueue", {
queueName: "my-queue"
});
// Add the SQS queue as event source for your Lambda
myLambdaFunction.addEventSource(new SqsEventSource(myQueue));
}
}
To add an SNS topic to your infrastructure code, you can use the CDK constructs from aws-cdk-lib/aws-sns
.
You can either create your own topic, or import an existing one with Topic.fromTopicArn
:
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import { Topic } from "aws-cdk-lib/aws-sns";
import { SqsSubscription } from "aws-cdk-lib/aws-sns-subscriptions";
export class AppStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// create your own SNS topic:
const myTopic = new Topic(this, "MyTopic", {
displayName: "my-topic",
topicName: "my-topic"
});
// make sure to arrange additional permissions to publish to your topic
// ...or import an existing one, e.g. from another project:
// const myTopic = Topic.fromTopicArn(...)
// add a subscription to your queue:
// const myQueue = ... (see previous example)
myQueue.addSubscription(new SqsSubscription(queue));
}
}
More examples for these constructs can be found on our CDK snippet page.
Publishing messages
Publishing messages to SQS or SNS can be done with the corresponding AWS SDK clients.
You can additionally use utilities from @backpack/amazon-sqs
and @backpack/amazon-sqs
to make it even simpler:
import { SNSClient } from "@aws-sdk/client-sns";
import { SnsPublisher } from "@backpack/amazon-sns";
const client = new SNSClient();
const publisher = new SnsPublisher(client, "my-topic-arn");
// publishes the message as JSON payload
await publisher.publishJson({ foo: "Foo" });
Check out the API reference section to learn more.
Handling messages
To handle SNS or SQS messages, you will need to create an AWS Lambda with the correct integration.
With the utilities from @backpack/aws-lambda
you can do this easily:
import { defineSqsLambda } from "@backpack/aws-lambda";
export const handler = defineSqsLambda(async function(event) {
// ...
});
Check out @backpack/aws-lambda
to learn more.