Skip to main content

Overview

The SNSTopic construct creates an Amazon SNS topic for pub/sub messaging. One publish call fans the message out to every subscriber (Lambda, SQS, HTTPS endpoints, email). Fjall applies two defaults on top of the CDK topic: TLS-only access (enforceSSL) and signature version 2 (SHA-256).

Prerequisites

  • A Fjall application with an infrastructure.ts file.
  • @fjall/components-infrastructure installed in that application.
fjall add messaging scaffolds an SQS queue, not a topic. Add SNS topics by editing infrastructure.ts directly.

Import

Add a topic

Use the MessagingFactory and app.addMessaging(). The topic lands in the application’s messaging stack.
To place the construct yourself, instantiate it directly against a stack scope:

Properties

removalPolicy defaults to DESTROY in every environment, including production. This is deliberate: a topic holds no durable state, and subscriptions are re-created by the next deploy. Retaining one orphans it when a parent construct’s logical ID changes. Pass removalPolicy: "RETAIN" only for a topic that must survive stack deletion.

Methods

grantPublish and grantSubscribe accept any CDK IGrantable, such as a Fjall LambdaFunction or an IAM role.

Examples

Standard topic

FIFO topic

FIFO topics deliver messages in order and support deduplication. Fjall appends the required .fifo suffix to topicName, so the topic below deploys as order-events.fifo.

Encryption at rest

The application’s backup tier decides: at enterprise a topic is encrypted with the stack’s shared customer-managed key; at standard and resilient it is unencrypted, and Fjall reports it as an SNS_TOPIC_NOT_ENCRYPTED posture finding rather than pre-paying for a key you may not want. encryption: "SSE_KMS" opts one topic into the shared key at any tier and encryption: "NONE" is the auditable opt-out at enterprise; the finding names whichever applies. masterKey brings your own key and wins over both.
To encrypt with a key you manage yourself:

Fan-out to multiple queues

Combine SNS with SQS to give each consumer its own buffered copy of the event.
Each SqsSubscription adds one statement to the queue’s resource policy, not the topic’s. SQS caps that policy at 20 statements and 8,192 bytes, and a Fjall queue’s TLS-only deny counts towards both. The byte cap binds first on a queue whose name CloudFormation generates, so a queue with no other grants carries 18 subscriptions. From fjall 38 a Fjall SQSQueue, which is what type: "queue" builds above, checks its own policy when the application synthesises and refuses an over-budget one before the deploy. A queue you build straight from aws-cdk-lib carries no such check, so its over-budget policy fails mid-deploy and rolls the stack back. Split the subscriptions across queues when one queue subscribes to many topics. See Queue-policy limits.

Gotchas

AWS service publishers need an explicit Allow

The TLS-only default synthesises an AWS::SNS::TopicPolicy, and that resource replaces the topic’s implicit default policy. Any AWS service principal that published under the default Allow, CloudWatch alarms being the common case, stops publishing until you merge an explicit Allow back in. Same-account IAM publishers are unaffected, because their identity policies suffice.

An encrypted topic needs key-policy grants too

A service principal publishing to an encrypted topic also needs kms:Decrypt and kms:GenerateDataKey* on the key’s resource policy, or its publishes fail silently. The AWS-managed aws/sns key cannot serve service publishers, because its policy is immutable. Use a CustomerManagedKey and call key.addToResourcePolicy(...).

Signature version

Fjall sets signatureVersion: "2" (SHA-256). The AWS account-level default is "1" (SHA-1), which signature-verifying HTTPS subscribers reject, leaving their subscriptions stuck in PendingConfirmation. Pass signatureVersion: "1" only for a legacy consumer that cannot verify SHA-256.

FIFO-only options

contentBasedDeduplication applies to FIFO topics only. Setting it on a standard topic logs a warning at synth time and has no effect.

CloudFormation Outputs

Next Steps

Messaging Factory

Create queues, topics, and event buses via the factory

SQS Queue

Buffer each fan-out consumer with its own queue

EventBridge Bus

Route events by pattern instead of by subscription

KMS Key

Provision the customer-managed key that encrypts a topic