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
47 changes: 47 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,47 @@
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 {
/**
* 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'));
made2591 marked this conversation as resolved.
Show resolved Hide resolved

return {
id: this.queue.node.id,
arn: this.queue.queueArn,
input: this.props.message,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"Resources": {
"EveryMinute2BBCEA8F": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(1 minute)",
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"MyQueueE6CA6235",
"Arn"
]
},
"Id": "MyQueue"
}
]
}
},
"MyQueueE6CA6235": {
"Type": "AWS::SQS::Queue"
},
"MyQueuePolicy6BBEDDAC": {
"Type": "AWS::SQS::QueuePolicy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"sqs:SendMessage",
"sqs:SendMessageBatch",
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl"
],
"Effect": "Allow",
"Principal": {
"Service": {
"Fn::Join": [
"",
[
"events.",
{
"Ref": "AWS::URLSuffix"
}
]
]
}
},
"Resource": {
"Fn::GetAtt": [
"MyQueueE6CA6235",
"Arn"
]
}
}
],
"Version": "2012-10-17"
},
"Queues": [
{
"Ref": "MyQueueE6CA6235"
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import events = require('@aws-cdk/aws-events');
import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/cdk');
import targets = require('../../lib');

// ---------------------------------
// Define a rule that triggers an SNS topic every 1min.
// Connect the topic with a queue. This means that the queue should have
// a message sent to it every minute.

const app = new cdk.App();

const stack = new cdk.Stack(app, 'aws-cdk-sqs-event-target');

const event = new events.Rule(stack, 'EveryMinute', {
scheduleExpression: 'rate(1 minute)',
});

const queue = new sqs.Queue(stack, 'MyQueue');
event.addTarget(new targets.SqsQueue(queue));

app.run();
84 changes: 84 additions & 0 deletions packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect, haveResource } from '@aws-cdk/assert';
import events = require('@aws-cdk/aws-events');
import sqs = require('@aws-cdk/aws-sqs');
import { Stack } from '@aws-cdk/cdk';
import targets = require('../../lib');

test('sqs queue as an event rule target', () => {
// GIVEN
const stack = new Stack();
const queue = new sqs.Queue(stack, 'MyQueue');
const rule = new events.Rule(stack, 'MyRule', {
scheduleExpression: 'rate(1 hour)',
});

// WHEN
rule.addTarget(new targets.SqsQueue(queue));

// THEN
expect(stack).to(haveResource('AWS::SQS::QueuePolicy', {
PolicyDocument: {
Statement: [
{
Action: [
"sqs:SendMessage",
"sqs:SendMessageBatch",
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl"
],
Effect: "Allow",
Principal: {
Service: { "Fn::Join": ["", ["events.", { Ref: "AWS::URLSuffix" }]] },
},
Resource: { "Fn::GetAtt": ["MyQueueE6CA6235", "Arn"] },
}
],
Version: "2012-10-17"
},
Queues: [{ Ref: "MyQueueE6CA6235" }]
}));

expect(stack).to(haveResource('AWS::Events::Rule', {
ScheduleExpression: "rate(1 hour)",
State: "ENABLED",
Targets: [
{
Arn: { "Fn::GetAtt": ["MyQueueE6CA6235", "Arn"] },
Id: "MyQueue"
}
]
}));
});

test('multiple uses of a queue as a target results in a single policy statement', () => {
// GIVEN
const stack = new Stack();
const queue = new sqs.Queue(stack, 'MyQueue');

// WHEN
for (let i = 0; i < 5; ++i) {
const rule = new events.Rule(stack, `Rule${i}`, { scheduleExpression: 'rate(1 hour)' });
rule.addTarget(new targets.SqsQueue(queue));
}

// THEN
expect(stack).to(haveResource('AWS::SQS::QueuePolicy', {
PolicyDocument: {
Statement: [
{
Action: [
"sqs:SendMessage",
"sqs:SendMessageBatch",
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl"
],
Effect: "Allow",
Principal: { Service: { "Fn::Join": ["", ["events.", { Ref: "AWS::URLSuffix" }]] } },
Resource: { "Fn::GetAtt": ["MyQueueE6CA6235", "Arn"] },
}
],
Version: "2012-10-17"
},
Queues: [{ Ref: "MyQueueE6CA6235" }]
}));
});