-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(events): support SQS queues as rule targets (#2683)
Extend the Target for Event Rule to SQS Queues. This may be needed in situations where users want to create an Event Rule and have as a target an SQS Queue. Closes #1786.
- Loading branch information
Showing
8 changed files
with
5,372 additions
and
4,979 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import events = require('@aws-cdk/aws-events'); | ||
import iam = require('@aws-cdk/aws-iam'); | ||
import sqs = require('@aws-cdk/aws-sqs'); | ||
|
||
/** | ||
* Customize the SQS Queue Event Target | ||
*/ | ||
export interface SqsQueueProps { | ||
|
||
/** | ||
* Message Group ID for messages sent to this queue | ||
* | ||
* Required for FIFO queues, leave empty for regular queues. | ||
* | ||
* @default - no message group ID (regular queue) | ||
*/ | ||
readonly messageGroupId?: string; | ||
|
||
/** | ||
* The message to send to the queue. | ||
* | ||
* Must be a valid JSON text passed to the target queue. | ||
* | ||
* @default the entire CloudWatch event | ||
*/ | ||
readonly message?: events.RuleTargetInput; | ||
|
||
} | ||
|
||
/** | ||
* Use an SQS Queue as a target for AWS CloudWatch event rules. | ||
* | ||
* @example | ||
* | ||
* // publish to an SQS queue every time code is committed | ||
* // to a CodeCommit repository | ||
* repository.onCommit(new targets.SqsQueue(queue)); | ||
* | ||
*/ | ||
export class SqsQueue implements events.IRuleTarget { | ||
|
||
constructor(public readonly queue: sqs.IQueue, private readonly props: SqsQueueProps = {}) { | ||
} | ||
|
||
/** | ||
* Returns a RuleTarget that can be used to trigger this SQS queue as a | ||
* result from a CloudWatch event. | ||
* | ||
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/resource-based-policies-cwe.html#sqs-permissions | ||
*/ | ||
public bind(rule: events.IRule): events.RuleTargetProperties { | ||
// deduplicated automatically | ||
this.queue.grantSendMessages(new iam.ServicePrincipal('events.amazonaws.com', | ||
{ | ||
conditions: { | ||
ArnEquals: { "aws:SourceArn": rule.ruleArn } | ||
} | ||
}) | ||
); | ||
|
||
const result = { | ||
id: this.queue.node.id, | ||
arn: this.queue.queueArn, | ||
input: this.props.message, | ||
}; | ||
if (!!this.props.messageGroupId) { | ||
Object.assign(result, { sqsParameters: { messageGroupId: this.props.messageGroupId } }); | ||
} | ||
return result; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.