or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

analytics-targets.mdapi-targets.mdcicd-targets.mdcompute-targets.mdindex.mdmessaging-targets.mdorchestration-targets.mdsystem-targets.md

messaging-targets.mddocs/

0

# Messaging Targets

1

2

Targets for AWS messaging services that can receive and deliver EventBridge events.

3

4

## Capabilities

5

6

### SQS Queue Target

7

8

Send messages to Amazon SQS queues in response to EventBridge events.

9

10

```typescript { .api }

11

/**

12

* Use an SQS queue as a target for Amazon EventBridge rules

13

*/

14

class SqsQueue implements events.IRuleTarget {

15

readonly queue: sqs.IQueue;

16

17

constructor(queue: sqs.IQueue, props?: SqsQueueProps);

18

19

/**

20

* Returns a RuleTarget that can be used to trigger this SQS queue

21

* as a result from an EventBridge event

22

*/

23

bind(rule: events.IRule, id?: string): events.RuleTargetConfig;

24

}

25

26

interface SqsQueueProps extends TargetBaseProps {

27

/**

28

* The message to send to the queue

29

* @default the entire EventBridge event

30

*/

31

readonly message?: events.RuleTargetInput;

32

33

/**

34

* Message Group ID for FIFO queues

35

* Required for FIFO queues, not used for standard queues

36

* @default no message group ID

37

*/

38

readonly messageGroupId?: string;

39

}

40

```

41

42

**Usage Example:**

43

44

```typescript

45

import * as sqs from "@aws-cdk/aws-sqs";

46

import * as events from "@aws-cdk/aws-events";

47

import * as targets from "@aws-cdk/aws-events-targets";

48

49

// Create SQS queue

50

const queue = new sqs.Queue(this, "EventQueue", {

51

queueName: "event-processing-queue",

52

});

53

54

// Create FIFO queue for ordered processing

55

const fifoQueue = new sqs.Queue(this, "FifoQueue", {

56

queueName: "ordered-events.fifo",

57

fifo: true,

58

contentBasedDeduplication: true,

59

});

60

61

// Create dead letter queue

62

const dlq = new sqs.Queue(this, "DeadLetterQueue");

63

64

// Create rule for EC2 state changes

65

const rule = new events.Rule(this, "EC2StateRule", {

66

eventPattern: {

67

source: ["aws.ec2"],

68

detailType: ["EC2 Instance State-change Notification"],

69

},

70

});

71

72

// Add SQS target with custom message format

73

rule.addTarget(new targets.SqsQueue(queue, {

74

deadLetterQueue: dlq,

75

maxEventAge: Duration.hours(4),

76

retryAttempts: 3,

77

message: events.RuleTargetInput.fromObject({

78

eventId: events.EventField.fromPath("$.id"),

79

instanceId: events.EventField.fromPath("$.detail.instance-id"),

80

state: events.EventField.fromPath("$.detail.state"),

81

timestamp: events.EventField.fromPath("$.time"),

82

}),

83

}));

84

85

// Add FIFO queue target with message group ID

86

rule.addTarget(new targets.SqsQueue(fifoQueue, {

87

messageGroupId: "ec2-state-changes",

88

message: events.RuleTargetInput.fromPath("$.detail"),

89

}));

90

```

91

92

### SNS Topic Target

93

94

Publish messages to Amazon SNS topics in response to EventBridge events.

95

96

```typescript { .api }

97

/**

98

* Use an SNS topic as a target for Amazon EventBridge rules

99

*/

100

class SnsTopic implements events.IRuleTarget {

101

readonly topic: sns.ITopic;

102

103

constructor(topic: sns.ITopic, props?: SnsTopicProps);

104

105

/**

106

* Returns a RuleTarget that can be used to trigger this SNS topic

107

* as a result from an EventBridge event

108

*/

109

bind(rule: events.IRule, id?: string): events.RuleTargetConfig;

110

}

111

112

interface SnsTopicProps {

113

/**

114

* The message to send to the topic

115

* @default the entire EventBridge event

116

*/

117

readonly message?: events.RuleTargetInput;

118

}

119

```

120

121

**Usage Example:**

122

123

```typescript

124

import * as sns from "@aws-cdk/aws-sns";

125

import * as snsSubscriptions from "@aws-cdk/aws-sns-subscriptions";

126

import * as events from "@aws-cdk/aws-events";

127

import * as targets from "@aws-cdk/aws-events-targets";

128

129

// Create SNS topic

130

const topic = new sns.Topic(this, "AlertTopic", {

131

topicName: "security-alerts",

132

displayName: "Security Alerts",

133

});

134

135

// Add email subscription

136

topic.addSubscription(new snsSubscriptions.EmailSubscription("admin@example.com"));

137

138

// Create rule for security events

139

const securityRule = new events.Rule(this, "SecurityRule", {

140

eventPattern: {

141

source: ["aws.guardduty", "aws.securityhub"],

142

detailType: ["GuardDuty Finding", "Security Hub Findings - Imported"],

143

},

144

});

145

146

// Add SNS target with formatted message

147

securityRule.addTarget(new targets.SnsTopic(topic, {

148

message: events.RuleTargetInput.fromObject({

149

severity: events.EventField.fromPath("$.detail.severity"),

150

title: events.EventField.fromPath("$.detail.title"),

151

description: events.EventField.fromPath("$.detail.description"),

152

account: events.EventField.fromPath("$.account"),

153

region: events.EventField.fromPath("$.region"),

154

time: events.EventField.fromPath("$.time"),

155

}),

156

}));

157

158

// Simple usage with default message (entire event)

159

const simpleRule = new events.Rule(this, "SimpleRule", {

160

eventPattern: {

161

source: ["myapp.orders"],

162

},

163

});

164

165

simpleRule.addTarget(new targets.SnsTopic(topic));

166

```

167

168

## Common Messaging Patterns

169

170

### Error Handling with Dead Letter Queues

171

172

Both SQS and SNS targets (where supported) can use dead letter queues for failed deliveries:

173

174

```typescript

175

import * as sqs from "@aws-cdk/aws-sqs";

176

import * as targets from "@aws-cdk/aws-events-targets";

177

178

// Create main queue and dead letter queue

179

const mainQueue = new sqs.Queue(this, "MainQueue");

180

const deadLetterQueue = new sqs.Queue(this, "DeadLetterQueue", {

181

queueName: "failed-events-dlq",

182

});

183

184

// Configure target with retry policy and DLQ

185

const queueTarget = new targets.SqsQueue(mainQueue, {

186

deadLetterQueue: deadLetterQueue,

187

retryAttempts: 3,

188

maxEventAge: Duration.hours(2),

189

});

190

```

191

192

### Message Transformation

193

194

Both targets support custom message transformation using `RuleTargetInput`:

195

196

```typescript

197

import * as events from "@aws-cdk/aws-events";

198

199

// Transform event to custom format

200

const customMessage = events.RuleTargetInput.fromObject({

201

// Extract specific fields

202

eventSource: events.EventField.fromPath("$.source"),

203

eventTime: events.EventField.fromPath("$.time"),

204

// Add static fields

205

environment: "production",

206

// Combine fields

207

summary: events.EventField.fromPath("$.detail.title") + " in " + events.EventField.fromPath("$.region"),

208

});

209

210

// Use entire event detail

211

const detailMessage = events.RuleTargetInput.fromPath("$.detail");

212

213

// Use entire event

214

const fullMessage = events.RuleTargetInput.fromEventPath("$");

215

```