@composurecdk/sns 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # @composurecdk/sns
2
+
3
+ SNS topic builder for [ComposureCDK](../../README.md).
4
+
5
+ This package provides a fluent builder for SNS topics with secure, AWS-recommended defaults. It wraps the CDK [Topic](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sns.Topic.html) construct — refer to the CDK documentation for the full set of configurable properties.
6
+
7
+ ## Topic Builder
8
+
9
+ ```ts
10
+ import { createTopicBuilder } from "@composurecdk/sns";
11
+
12
+ const alerts = createTopicBuilder()
13
+ .topicName("my-alerts")
14
+ .displayName("My Alert Topic")
15
+ .build(stack, "AlertTopic");
16
+ ```
17
+
18
+ Every [TopicProps](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sns.TopicProps.html) property is available as a fluent setter on the builder.
19
+
20
+ ## Secure Defaults
21
+
22
+ `createTopicBuilder` applies the following defaults. Each can be overridden via the builder's fluent API.
23
+
24
+ | Property | Default | Rationale |
25
+ | ------------ | ------- | ----------------------------------------------------------------------------- |
26
+ | `enforceSSL` | `true` | Denies publish/subscribe requests that do not use TLS (transport encryption). |
27
+
28
+ These defaults are guided by the [AWS SNS Security Best Practices](https://docs.aws.amazon.com/sns/latest/dg/sns-security-best-practices.html#enforce-encryption-data-in-transit).
29
+
30
+ The defaults are exported as `TOPIC_DEFAULTS` for visibility and testing:
31
+
32
+ ```ts
33
+ import { TOPIC_DEFAULTS } from "@composurecdk/sns";
34
+ ```
35
+
36
+ ### Overriding defaults
37
+
38
+ ```ts
39
+ const topic = createTopicBuilder().topicName("my-topic").enforceSSL(false).build(stack, "MyTopic");
40
+ ```
41
+
42
+ ## Recommended Alarms
43
+
44
+ The builder creates [AWS-recommended CloudWatch alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS) by default. No alarm actions are configured — access alarms from the build result to add SNS topics or other actions.
45
+
46
+ | Alarm | Metric | Default threshold | Created when |
47
+ | --------------------------------------------------- | --------------------------------------------------------------- | ----------------- | ------------ |
48
+ | `numberOfNotificationsFailed` | NumberOfNotificationsFailed (Sum, 1 min) | > 0 | Always |
49
+ | `numberOfNotificationsFilteredOutInvalidAttributes` | NumberOfNotificationsFilteredOut-InvalidAttributes (Sum, 1 min) | > 0 | Always |
50
+
51
+ The defaults are exported as `TOPIC_ALARM_DEFAULTS` for visibility and testing:
52
+
53
+ ```ts
54
+ import { TOPIC_ALARM_DEFAULTS } from "@composurecdk/sns";
55
+ ```
56
+
57
+ ### Customizing thresholds
58
+
59
+ Override individual alarm properties via `recommendedAlarms`. Unspecified fields keep their defaults.
60
+
61
+ ```ts
62
+ const topic = createTopicBuilder()
63
+ .topicName("my-topic")
64
+ .recommendedAlarms({
65
+ numberOfNotificationsFailed: { threshold: 5, evaluationPeriods: 3 },
66
+ });
67
+ ```
68
+
69
+ ### Disabling alarms
70
+
71
+ Disable all recommended alarms:
72
+
73
+ ```ts
74
+ builder.recommendedAlarms(false);
75
+ // or
76
+ builder.recommendedAlarms({ enabled: false });
77
+ ```
78
+
79
+ Disable individual alarms:
80
+
81
+ ```ts
82
+ builder.recommendedAlarms({ numberOfNotificationsFailed: false });
83
+ ```
84
+
85
+ ### Custom alarms
86
+
87
+ Add custom alarms alongside the recommended ones via `addAlarm`. The callback receives an `AlarmDefinitionBuilder` typed to `ITopic`, so the metric factory has access to the topic's properties.
88
+
89
+ ```ts
90
+ import { Metric } from "aws-cdk-lib/aws-cloudwatch";
91
+
92
+ const topic = createTopicBuilder()
93
+ .topicName("my-topic")
94
+ .addAlarm("highPublishRate", (alarm) =>
95
+ alarm
96
+ .metric(
97
+ (topic) =>
98
+ new Metric({
99
+ namespace: "AWS/SNS",
100
+ metricName: "NumberOfMessagesPublished",
101
+ dimensionsMap: { TopicName: topic.topicName },
102
+ statistic: "Sum",
103
+ period: Duration.minutes(1),
104
+ }),
105
+ )
106
+ .threshold(10000)
107
+ .greaterThanOrEqual()
108
+ .description("Topic receiving unusually high message volume"),
109
+ );
110
+ ```
111
+
112
+ ### Applying alarm actions
113
+
114
+ Alarms are returned in the build result as `Record<string, Alarm>`:
115
+
116
+ ```ts
117
+ const result = topic.build(stack, "MyTopic");
118
+
119
+ const alertTopic = new Topic(stack, "AlertTopic");
120
+ for (const alarm of Object.values(result.alarms)) {
121
+ alarm.addAlarmAction(new SnsAction(alertTopic));
122
+ }
123
+ ```
124
+
125
+ ## Examples
126
+
127
+ - [LambdaApiStack](../examples/src/lambda-api-app.ts) — REST API with TopicBuilder for alarm actions
128
+ - [DualFunctionStack](../examples/src/dual-function-app.ts) — Two Lambda functions with TopicBuilder for alarm actions
129
+ - [StaticWebsiteStack](../examples/src/static-website/app.ts) — Static website with TopicBuilder for alarm actions
@@ -0,0 +1,38 @@
1
+ import type { AlarmConfig } from "@composurecdk/cloudwatch";
2
+ /**
3
+ * Controls which recommended alarms are created for an SNS topic.
4
+ * All applicable alarms are enabled by default with AWS-recommended thresholds.
5
+ * Set individual alarms to `false` to disable them, or provide an
6
+ * {@link AlarmConfig} to tune thresholds.
7
+ *
8
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
9
+ */
10
+ export interface TopicAlarmConfig {
11
+ /**
12
+ * Master switch: set to `false` to disable all recommended alarms.
13
+ * Individual alarms can also be disabled via their own entry.
14
+ * @default true
15
+ */
16
+ enabled?: boolean;
17
+ /**
18
+ * Alarm when SNS fails to deliver notifications to subscribed endpoints.
19
+ *
20
+ * Metric: `AWS/SNS NumberOfNotificationsFailed`, statistic Sum, period 1 minute.
21
+ * Default threshold: > 0 failures.
22
+ *
23
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
24
+ */
25
+ numberOfNotificationsFailed?: AlarmConfig | false;
26
+ /**
27
+ * Alarm when messages are rejected due to invalid subscription filter
28
+ * policy attributes.
29
+ *
30
+ * Metric: `AWS/SNS NumberOfNotificationsFilteredOut-InvalidAttributes`,
31
+ * statistic Sum, period 1 minute.
32
+ * Default threshold: > 0 filtered messages.
33
+ *
34
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
35
+ */
36
+ numberOfNotificationsFilteredOutInvalidAttributes?: AlarmConfig | false;
37
+ }
38
+ //# sourceMappingURL=alarm-config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alarm-config.d.ts","sourceRoot":"","sources":["../src/alarm-config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE5D;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;;;;;OAOG;IACH,2BAA2B,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAElD;;;;;;;;;OASG;IACH,iDAAiD,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;CACzE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=alarm-config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alarm-config.js","sourceRoot":"","sources":["../src/alarm-config.ts"],"names":[],"mappings":""}
@@ -0,0 +1,14 @@
1
+ import type { AlarmConfig } from "@composurecdk/cloudwatch";
2
+ interface TopicAlarmDefaults {
3
+ enabled: true;
4
+ numberOfNotificationsFailed: Required<AlarmConfig>;
5
+ numberOfNotificationsFilteredOutInvalidAttributes: Required<AlarmConfig>;
6
+ }
7
+ /**
8
+ * AWS-recommended default alarm configuration for SNS topics.
9
+ *
10
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
11
+ */
12
+ export declare const TOPIC_ALARM_DEFAULTS: TopicAlarmDefaults;
13
+ export {};
14
+ //# sourceMappingURL=alarm-defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alarm-defaults.d.ts","sourceRoot":"","sources":["../src/alarm-defaults.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE5D,UAAU,kBAAkB;IAC1B,OAAO,EAAE,IAAI,CAAC;IACd,2BAA2B,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IACnD,iDAAiD,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;CAC1E;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,EAAE,kBAkBlC,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
2
+ /**
3
+ * AWS-recommended default alarm configuration for SNS topics.
4
+ *
5
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
6
+ */
7
+ export const TOPIC_ALARM_DEFAULTS = {
8
+ enabled: true,
9
+ /** Any delivery failure is worth investigating; threshold 0. */
10
+ numberOfNotificationsFailed: {
11
+ threshold: 0,
12
+ evaluationPeriods: 1,
13
+ datapointsToAlarm: 1,
14
+ treatMissingData: TreatMissingData.NOT_BREACHING,
15
+ },
16
+ /** Any message filtered due to invalid attributes indicates a configuration issue; threshold 0. */
17
+ numberOfNotificationsFilteredOutInvalidAttributes: {
18
+ threshold: 0,
19
+ evaluationPeriods: 1,
20
+ datapointsToAlarm: 1,
21
+ treatMissingData: TreatMissingData.NOT_BREACHING,
22
+ },
23
+ };
24
+ //# sourceMappingURL=alarm-defaults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alarm-defaults.js","sourceRoot":"","sources":["../src/alarm-defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAS9D;;;;GAIG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAuB;IACtD,OAAO,EAAE,IAAI;IAEb,gEAAgE;IAChE,2BAA2B,EAAE;QAC3B,SAAS,EAAE,CAAC;QACZ,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;IAED,mGAAmG;IACnG,iDAAiD,EAAE;QACjD,SAAS,EAAE,CAAC;QACZ,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;CACF,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { TopicProps } from "aws-cdk-lib/aws-sns";
2
+ /**
3
+ * Secure, AWS-recommended defaults applied to every SNS topic built
4
+ * with {@link createTopicBuilder}. Each property can be individually
5
+ * overridden via the builder's fluent API.
6
+ */
7
+ export declare const TOPIC_DEFAULTS: Partial<TopicProps>;
8
+ //# sourceMappingURL=defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEtD;;;;GAIG;AACH,eAAO,MAAM,cAAc,EAAE,OAAO,CAAC,UAAU,CAO9C,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Secure, AWS-recommended defaults applied to every SNS topic built
3
+ * with {@link createTopicBuilder}. Each property can be individually
4
+ * overridden via the builder's fluent API.
5
+ */
6
+ export const TOPIC_DEFAULTS = {
7
+ /**
8
+ * Enforce TLS for all publish and subscribe operations on the topic.
9
+ * Adds a resource policy condition that denies requests not using SSL.
10
+ * @see https://docs.aws.amazon.com/sns/latest/dg/sns-security-best-practices.html#enforce-encryption-data-in-transit
11
+ */
12
+ enforceSSL: true,
13
+ };
14
+ //# sourceMappingURL=defaults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAwB;IACjD;;;;OAIG;IACH,UAAU,EAAE,IAAI;CACjB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { createTopicBuilder, type TopicBuilderResult, type ITopicBuilder, } from "./topic-builder.js";
2
+ export { TOPIC_DEFAULTS } from "./defaults.js";
3
+ export { type TopicAlarmConfig } from "./alarm-config.js";
4
+ export { TOPIC_ALARM_DEFAULTS } from "./alarm-defaults.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,KAAK,kBAAkB,EACvB,KAAK,aAAa,GACnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { createTopicBuilder, } from "./topic-builder.js";
2
+ export { TOPIC_DEFAULTS } from "./defaults.js";
3
+ export { TOPIC_ALARM_DEFAULTS } from "./alarm-defaults.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,GAGnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC"}
@@ -0,0 +1,26 @@
1
+ import { type Alarm } from "aws-cdk-lib/aws-cloudwatch";
2
+ import type { ITopic } from "aws-cdk-lib/aws-sns";
3
+ import type { IConstruct } from "constructs";
4
+ import type { AlarmDefinition } from "@composurecdk/cloudwatch";
5
+ import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
6
+ import type { TopicAlarmConfig } from "./alarm-config.js";
7
+ /**
8
+ * Resolves the recommended alarm configuration into fully-resolved
9
+ * {@link AlarmDefinition}s for an SNS topic.
10
+ */
11
+ export declare function resolveTopicAlarmDefinitions(topic: ITopic, config: TopicAlarmConfig | undefined): AlarmDefinition[];
12
+ /**
13
+ * Creates AWS-recommended CloudWatch alarms for an SNS topic,
14
+ * merging recommended definitions with any custom alarm builders.
15
+ *
16
+ * @param scope - CDK construct scope for creating alarm constructs.
17
+ * @param id - Base identifier for alarm construct ids.
18
+ * @param topic - The SNS topic to create alarms for.
19
+ * @param config - User-provided alarm configuration, or `false` to disable all.
20
+ * @param customAlarms - Custom alarm builders added via `addAlarm()`.
21
+ * @returns A record mapping alarm keys to their created Alarm constructs.
22
+ *
23
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
24
+ */
25
+ export declare function createTopicAlarms(scope: IConstruct, id: string, topic: ITopic, config: TopicAlarmConfig | false | undefined, customAlarms?: AlarmDefinitionBuilder<ITopic>[]): Record<string, Alarm>;
26
+ //# sourceMappingURL=topic-alarms.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"topic-alarms.d.ts","sourceRoot":"","sources":["../src/topic-alarms.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAsB,MAAM,4BAA4B,CAAC;AAC5E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,sBAAsB,EAAoC,MAAM,0BAA0B,CAAC;AACpG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAM1D;;;GAGG;AACH,wBAAgB,4BAA4B,CAC1C,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,gBAAgB,GAAG,SAAS,GACnC,eAAe,EAAE,CA0CnB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,UAAU,EACjB,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,gBAAgB,GAAG,KAAK,GAAG,SAAS,EAC5C,YAAY,GAAE,sBAAsB,CAAC,MAAM,CAAC,EAAO,GAClD,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAUvB"}
@@ -0,0 +1,68 @@
1
+ import { Duration } from "aws-cdk-lib";
2
+ import { ComparisonOperator } from "aws-cdk-lib/aws-cloudwatch";
3
+ import { createAlarms, resolveAlarmConfig } from "@composurecdk/cloudwatch";
4
+ import { TOPIC_ALARM_DEFAULTS } from "./alarm-defaults.js";
5
+ const METRIC_PERIOD = Duration.minutes(1);
6
+ const METRIC_PERIOD_LABEL = `${String(METRIC_PERIOD.toMinutes())} minute`;
7
+ /**
8
+ * Resolves the recommended alarm configuration into fully-resolved
9
+ * {@link AlarmDefinition}s for an SNS topic.
10
+ */
11
+ export function resolveTopicAlarmDefinitions(topic, config) {
12
+ if (config?.enabled === false)
13
+ return [];
14
+ const definitions = [];
15
+ if (config?.numberOfNotificationsFailed !== false) {
16
+ const cfg = resolveAlarmConfig(config?.numberOfNotificationsFailed, TOPIC_ALARM_DEFAULTS.numberOfNotificationsFailed);
17
+ definitions.push({
18
+ key: "numberOfNotificationsFailed",
19
+ metric: topic.metricNumberOfNotificationsFailed({ period: METRIC_PERIOD }),
20
+ threshold: cfg.threshold,
21
+ comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
22
+ evaluationPeriods: cfg.evaluationPeriods,
23
+ datapointsToAlarm: cfg.datapointsToAlarm,
24
+ treatMissingData: cfg.treatMissingData,
25
+ description: `SNS topic is failing to deliver notifications. Threshold: > ${String(cfg.threshold)} failures in ${METRIC_PERIOD_LABEL}.`,
26
+ });
27
+ }
28
+ if (config?.numberOfNotificationsFilteredOutInvalidAttributes !== false) {
29
+ const cfg = resolveAlarmConfig(config?.numberOfNotificationsFilteredOutInvalidAttributes, TOPIC_ALARM_DEFAULTS.numberOfNotificationsFilteredOutInvalidAttributes);
30
+ definitions.push({
31
+ key: "numberOfNotificationsFilteredOutInvalidAttributes",
32
+ metric: topic.metricNumberOfNotificationsFilteredOutInvalidAttributes({
33
+ period: METRIC_PERIOD,
34
+ }),
35
+ threshold: cfg.threshold,
36
+ comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
37
+ evaluationPeriods: cfg.evaluationPeriods,
38
+ datapointsToAlarm: cfg.datapointsToAlarm,
39
+ treatMissingData: cfg.treatMissingData,
40
+ description: `SNS topic messages are being filtered out due to invalid subscription filter policy attributes. Threshold: > ${String(cfg.threshold)} filtered messages in ${METRIC_PERIOD_LABEL}.`,
41
+ });
42
+ }
43
+ return definitions;
44
+ }
45
+ /**
46
+ * Creates AWS-recommended CloudWatch alarms for an SNS topic,
47
+ * merging recommended definitions with any custom alarm builders.
48
+ *
49
+ * @param scope - CDK construct scope for creating alarm constructs.
50
+ * @param id - Base identifier for alarm construct ids.
51
+ * @param topic - The SNS topic to create alarms for.
52
+ * @param config - User-provided alarm configuration, or `false` to disable all.
53
+ * @param customAlarms - Custom alarm builders added via `addAlarm()`.
54
+ * @returns A record mapping alarm keys to their created Alarm constructs.
55
+ *
56
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
57
+ */
58
+ export function createTopicAlarms(scope, id, topic, config, customAlarms = []) {
59
+ if (config === false)
60
+ return {};
61
+ const enabled = config?.enabled ?? TOPIC_ALARM_DEFAULTS.enabled;
62
+ if (!enabled)
63
+ return {};
64
+ const recommended = resolveTopicAlarmDefinitions(topic, config);
65
+ const custom = customAlarms.map((b) => b.resolve(topic));
66
+ return createAlarms(scope, id, [...recommended, ...custom]);
67
+ }
68
+ //# sourceMappingURL=topic-alarms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"topic-alarms.js","sourceRoot":"","sources":["../src/topic-alarms.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,108 @@
1
+ import { type Alarm } from "aws-cdk-lib/aws-cloudwatch";
2
+ import { type ITopic, Topic, type TopicProps } from "aws-cdk-lib/aws-sns";
3
+ import { type IConstruct } from "constructs";
4
+ import { type IBuilder, type Lifecycle } from "@composurecdk/core";
5
+ import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
6
+ import type { TopicAlarmConfig } from "./alarm-config.js";
7
+ /**
8
+ * Configuration properties for the SNS topic builder.
9
+ *
10
+ * Extends the CDK {@link TopicProps} with additional builder-specific options.
11
+ */
12
+ export interface TopicBuilderProps extends TopicProps {
13
+ /**
14
+ * Configuration for AWS-recommended CloudWatch alarms.
15
+ *
16
+ * By default, the builder creates recommended alarms with sensible
17
+ * thresholds for every applicable metric. Individual alarms can be
18
+ * customized or disabled. Set to `false` to disable all alarms.
19
+ *
20
+ * No alarm actions are configured by default since notification
21
+ * methods are user-specific. Access alarms from the build result
22
+ * or use an `afterBuild` hook to apply actions.
23
+ *
24
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
25
+ */
26
+ recommendedAlarms?: TopicAlarmConfig | false;
27
+ }
28
+ /**
29
+ * The build output of a {@link ITopicBuilder}. Contains the CDK constructs
30
+ * created during {@link Lifecycle.build}, keyed by role.
31
+ */
32
+ export interface TopicBuilderResult {
33
+ /** The SNS topic construct created by the builder. */
34
+ topic: Topic;
35
+ /**
36
+ * CloudWatch alarms created for the topic, keyed by alarm name.
37
+ *
38
+ * Includes both AWS-recommended alarms and any custom alarms added
39
+ * via {@link ITopicBuilder.addAlarm}. Access individual alarms
40
+ * by key (e.g., `result.alarms.numberOfNotificationsFailed`).
41
+ *
42
+ * No alarm actions are configured — apply them via the result or an
43
+ * `afterBuild` hook.
44
+ *
45
+ * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#SNS
46
+ */
47
+ alarms: Record<string, Alarm>;
48
+ }
49
+ /**
50
+ * A fluent builder for configuring and creating an AWS SNS topic.
51
+ *
52
+ * Each configuration property from the CDK {@link TopicProps} is exposed
53
+ * as an overloaded method: call with a value to set it (returns the builder
54
+ * for chaining), or call with no arguments to read the current value.
55
+ *
56
+ * The builder implements {@link Lifecycle}, so it can be used directly as a
57
+ * component in a {@link compose | composed system}. When built, it creates
58
+ * an SNS topic with the configured properties and returns a
59
+ * {@link TopicBuilderResult}.
60
+ *
61
+ * The builder also creates AWS-recommended CloudWatch alarms by default.
62
+ * Alarms can be customized or disabled via the `recommendedAlarms` property.
63
+ * Custom alarms can be added via the {@link addAlarm} method.
64
+ *
65
+ * @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sns.Topic.html
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * const alerts = createTopicBuilder()
70
+ * .topicName("my-alerts")
71
+ * .displayName("My Alert Topic");
72
+ * ```
73
+ */
74
+ export type ITopicBuilder = IBuilder<TopicBuilderProps, TopicBuilder>;
75
+ declare class TopicBuilder implements Lifecycle<TopicBuilderResult> {
76
+ props: Partial<TopicBuilderProps>;
77
+ private readonly customAlarms;
78
+ addAlarm(key: string, configure: (alarm: AlarmDefinitionBuilder<ITopic>) => AlarmDefinitionBuilder<ITopic>): this;
79
+ build(scope: IConstruct, id: string): TopicBuilderResult;
80
+ }
81
+ /**
82
+ * Creates a new {@link ITopicBuilder} for configuring an AWS SNS topic.
83
+ *
84
+ * This is the entry point for defining an SNS topic component. The returned
85
+ * builder exposes every {@link TopicBuilderProps} property as a fluent setter/getter
86
+ * and implements {@link Lifecycle} for use with {@link compose}.
87
+ *
88
+ * @returns A fluent builder for an AWS SNS topic.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * const alerts = createTopicBuilder()
93
+ * .topicName("my-alerts")
94
+ * .displayName("My Alert Topic");
95
+ *
96
+ * // Use standalone:
97
+ * const result = alerts.build(stack, "AlertTopic");
98
+ *
99
+ * // Or compose into a system:
100
+ * const system = compose(
101
+ * { alerts, handler: createFunctionBuilder() },
102
+ * { alerts: [], handler: [] },
103
+ * );
104
+ * ```
105
+ */
106
+ export declare function createTopicBuilder(): ITopicBuilder;
107
+ export {};
108
+ //# sourceMappingURL=topic-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"topic-builder.d.ts","sourceRoot":"","sources":["../src/topic-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,EAAE,KAAK,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC1E,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAW,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAI1D;;;;GAIG;AACH,MAAM,WAAW,iBAAkB,SAAQ,UAAU;IACnD;;;;;;;;;;;;OAYG;IACH,iBAAiB,CAAC,EAAE,gBAAgB,GAAG,KAAK,CAAC;CAC9C;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,sDAAsD;IACtD,KAAK,EAAE,KAAK,CAAC;IAEb;;;;;;;;;;;OAWG;IACH,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC,iBAAiB,EAAE,YAAY,CAAC,CAAC;AAEtE,cAAM,YAAa,YAAW,SAAS,CAAC,kBAAkB,CAAC;IACzD,KAAK,EAAE,OAAO,CAAC,iBAAiB,CAAC,CAAM;IACvC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAwC;IAErE,QAAQ,CACN,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,CAAC,KAAK,EAAE,sBAAsB,CAAC,MAAM,CAAC,KAAK,sBAAsB,CAAC,MAAM,CAAC,GACnF,IAAI;IAKP,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,GAAG,kBAAkB;CAczD;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,kBAAkB,IAAI,aAAa,CAElD"}
@@ -0,0 +1,52 @@
1
+ import { Topic } from "aws-cdk-lib/aws-sns";
2
+ import { Builder } from "@composurecdk/core";
3
+ import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
4
+ import { createTopicAlarms } from "./topic-alarms.js";
5
+ import { TOPIC_DEFAULTS } from "./defaults.js";
6
+ class TopicBuilder {
7
+ props = {};
8
+ customAlarms = [];
9
+ addAlarm(key, configure) {
10
+ this.customAlarms.push(configure(new AlarmDefinitionBuilder(key)));
11
+ return this;
12
+ }
13
+ build(scope, id) {
14
+ const { recommendedAlarms: alarmConfig, ...topicProps } = this.props;
15
+ const mergedProps = {
16
+ ...TOPIC_DEFAULTS,
17
+ ...topicProps,
18
+ };
19
+ const topic = new Topic(scope, id, mergedProps);
20
+ const alarms = createTopicAlarms(scope, id, topic, alarmConfig, this.customAlarms);
21
+ return { topic, alarms };
22
+ }
23
+ }
24
+ /**
25
+ * Creates a new {@link ITopicBuilder} for configuring an AWS SNS topic.
26
+ *
27
+ * This is the entry point for defining an SNS topic component. The returned
28
+ * builder exposes every {@link TopicBuilderProps} property as a fluent setter/getter
29
+ * and implements {@link Lifecycle} for use with {@link compose}.
30
+ *
31
+ * @returns A fluent builder for an AWS SNS topic.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * const alerts = createTopicBuilder()
36
+ * .topicName("my-alerts")
37
+ * .displayName("My Alert Topic");
38
+ *
39
+ * // Use standalone:
40
+ * const result = alerts.build(stack, "AlertTopic");
41
+ *
42
+ * // Or compose into a system:
43
+ * const system = compose(
44
+ * { alerts, handler: createFunctionBuilder() },
45
+ * { alerts: [], handler: [] },
46
+ * );
47
+ * ```
48
+ */
49
+ export function createTopicBuilder() {
50
+ return Builder(TopicBuilder);
51
+ }
52
+ //# sourceMappingURL=topic-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"topic-builder.js","sourceRoot":"","sources":["../src/topic-builder.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,KAAK,EAAmB,MAAM,qBAAqB,CAAC;AAE1E,OAAO,EAAE,OAAO,EAAiC,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AA0E/C,MAAM,YAAY;IAChB,KAAK,GAA+B,EAAE,CAAC;IACtB,YAAY,GAAqC,EAAE,CAAC;IAErE,QAAQ,CACN,GAAW,EACX,SAAoF;QAEpF,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,sBAAsB,CAAS,GAAG,CAAC,CAAC,CAAC,CAAC;QAC3E,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,KAAiB,EAAE,EAAU;QACjC,MAAM,EAAE,iBAAiB,EAAE,WAAW,EAAE,GAAG,UAAU,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAErE,MAAM,WAAW,GAAG;YAClB,GAAG,cAAc;YACjB,GAAG,UAAU;SACO,CAAC;QAEvB,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,KAAK,EAAE,EAAE,EAAE,WAAW,CAAC,CAAC;QAEhD,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QAEnF,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAC3B,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,kBAAkB;IAChC,OAAO,OAAO,CAAkC,YAAY,CAAC,CAAC;AAChE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@composurecdk/sns",
3
+ "version": "0.3.0",
4
+ "description": "Composable SNS topic builder with well-architected defaults",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/laazyj/composureCDK",
8
+ "directory": "packages/sns"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "scripts": {
24
+ "clean": "rm -rf dist",
25
+ "build": "tsc -p tsconfig.build.json",
26
+ "typecheck": "tsc --noEmit",
27
+ "test": "vitest run",
28
+ "test:watch": "vitest"
29
+ },
30
+ "keywords": [],
31
+ "author": "Jason Duffett (https://github.com/laazyj)",
32
+ "license": "MIT",
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "type": "module",
37
+ "peerDependencies": {
38
+ "@composurecdk/cloudwatch": "^0.3.0",
39
+ "@composurecdk/core": "^0.3.0",
40
+ "aws-cdk-lib": "^2.0.0",
41
+ "constructs": "^10.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/node": "^25.5.0",
45
+ "aws-cdk-lib": "^2.245.0",
46
+ "constructs": "^10.6.0",
47
+ "typescript": "^6.0.2",
48
+ "vitest": "^4.1.2"
49
+ }
50
+ }