Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@aws-cdk/aws-events-triggers): introducing sqs target support in event rule #2683

Merged
merged 10 commits into from
Jun 4, 2019
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
71 changes: 71 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,71 @@
import events = require('@aws-cdk/aws-events');
import iam = require('@aws-cdk/aws-iam');
import sqs = require('@aws-cdk/aws-sqs');

const fifoQualifier = "fifo";

/**
* Customize the SQS Queue Event Target
*/
export interface SqsQueueProps {

/**
* The messageGroupId for the queue
*
* @default empty string
made2591 marked this conversation as resolved.
Show resolved Hide resolved
*/
readonly messageGroupId?: string;

/**
* The message to send to the queue
*
* @default the entire CloudWatch event
*/
readonly message?: events.RuleTargetInput;
made2591 marked this conversation as resolved.
Show resolved Hide resolved

}

/**
* 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.queue.queueName.split('.').pop() === fifoQualifier && this.props.messageGroupId !== undefined) {
made2591 marked this conversation as resolved.
Show resolved Hide resolved
Object.assign(result, { sqsParameters: { messageGroupId: this.props.messageGroupId } });
}
return result;

}

}
Loading