Skip to content

Commit

Permalink
feat(events): support SQS queues as rule targets (#2683)
Browse files Browse the repository at this point in the history
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
made2591 authored and rix0rrr committed Jun 4, 2019
1 parent 1c13faa commit 078e34a
Show file tree
Hide file tree
Showing 8 changed files with 5,372 additions and 4,979 deletions.
75 changes: 75 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-events-targets/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './codepipeline';
export * from './sns';
export * from './sqs';
export * from './codebuild';
export * from './lambda';
export * from './ecs-task-properties';
Expand Down
73 changes: 73 additions & 0 deletions packages/@aws-cdk/aws-events-targets/lib/sqs.ts
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;

}

}
Loading

0 comments on commit 078e34a

Please sign in to comment.