From 2ddf67b45698565e5112ef11ae7954c3989eace3 Mon Sep 17 00:00:00 2001 From: Matteo Madeddu Date: Thu, 30 May 2019 13:25:53 +0200 Subject: [PATCH 1/8] Scaffolding SQS Target support in Event Rule --- package.json | 3 + .../@aws-cdk/aws-events-targets/lib/index.ts | 1 + .../@aws-cdk/aws-events-targets/lib/sqs.ts | 47 ++++ .../@aws-cdk/aws-events-targets/package.json | 213 +++++++++--------- .../integ.sqs-event-rule-target.expected.json | 103 +++++++++ .../test/sqs/integ.sqs-event-rule-target.ts | 39 ++++ .../aws-events-targets/test/sqs/sqs.test.ts | 84 +++++++ 7 files changed, 384 insertions(+), 106 deletions(-) create mode 100644 packages/@aws-cdk/aws-events-targets/lib/sqs.ts create mode 100644 packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json create mode 100644 packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts create mode 100644 packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts diff --git a/package.json b/package.json index ce32bc43b81c4..b58dca1fb3173 100644 --- a/package.json +++ b/package.json @@ -28,5 +28,8 @@ "author": { "name": "Amazon Web Services", "url": "https://aws.amazon.com" + }, + "dependencies": { + "@aws-cdk/aws-sqs": "^0.32.0" } } diff --git a/packages/@aws-cdk/aws-events-targets/lib/index.ts b/packages/@aws-cdk/aws-events-targets/lib/index.ts index cd31a43468f19..6a18485f3c655 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/index.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/index.ts @@ -1,5 +1,6 @@ export * from './codepipeline'; export * from './sns'; +export * from './sqs'; export * from './codebuild'; export * from './lambda'; export * from './ecs-task-properties'; diff --git a/packages/@aws-cdk/aws-events-targets/lib/sqs.ts b/packages/@aws-cdk/aws-events-targets/lib/sqs.ts new file mode 100644 index 0000000000000..791d68f59b29c --- /dev/null +++ b/packages/@aws-cdk/aws-events-targets/lib/sqs.ts @@ -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; +} + +/** + * 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')); + + return { + id: this.queue.node.id, + arn: this.queue.queueArn, + input: this.props.message, + }; + } +} diff --git a/packages/@aws-cdk/aws-events-targets/package.json b/packages/@aws-cdk/aws-events-targets/package.json index bc6414589242b..7b789af73fcac 100644 --- a/packages/@aws-cdk/aws-events-targets/package.json +++ b/packages/@aws-cdk/aws-events-targets/package.json @@ -1,110 +1,111 @@ { - "name": "@aws-cdk/aws-events-targets", - "version": "0.32.0", - "description": "Event targets for AWS CloudWatch Events", - "main": "lib/index.js", - "types": "lib/index.d.ts", - "jsii": { - "outdir": "dist", - "targets": { - "java": { - "package": "software.amazon.awscdk.services.events.targets", - "maven": { - "groupId": "software.amazon.awscdk", - "artifactId": "events-targets" + "name": "@aws-cdk/aws-events-targets", + "version": "0.32.0", + "description": "Event targets for AWS CloudWatch Events", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "jsii": { + "outdir": "dist", + "targets": { + "java": { + "package": "software.amazon.awscdk.services.events.targets", + "maven": { + "groupId": "software.amazon.awscdk", + "artifactId": "events-targets" + } + }, + "dotnet": { + "namespace": "Amazon.CDK.AWS.Events.Targets", + "packageId": "Amazon.CDK.AWS.Events.Targets", + "signAssembly": true, + "assemblyOriginatorKeyFile": "../../key.snk" + }, + "python": { + "distName": "aws-cdk.aws-events-targets", + "module": "aws_cdk.aws_events_targets" + } } - }, - "dotnet": { - "namespace": "Amazon.CDK.AWS.Events.Targets", - "packageId": "Amazon.CDK.AWS.Events.Targets", - "signAssembly": true, - "assemblyOriginatorKeyFile": "../../key.snk" - }, - "python": { - "distName": "aws-cdk.aws-events-targets", - "module": "aws_cdk.aws_events_targets" - } - } - }, - "repository": { - "type": "git", - "url": "https://github.com/awslabs/aws-cdk.git", - "directory": "packages/@aws-cdk/aws-events-targets" - }, - "scripts": { - "build": "cdk-build", - "watch": "cdk-watch", - "lint": "cdk-lint", - "test": "cdk-test", - "integ": "cdk-integ", - "pkglint": "pkglint -f", - "package": "cdk-package", - "awslint": "cdk-awslint", - "cfn2ts": "cfn2ts", - "build+test+package": "npm run build+test && npm run package", - "build+test": "npm run build && npm test" - }, - "jest": { - "moduleFileExtensions": [ - "js" + }, + "repository": { + "type": "git", + "url": "https://github.com/awslabs/aws-cdk.git", + "directory": "packages/@aws-cdk/aws-events-targets" + }, + "scripts": { + "build": "cdk-build", + "watch": "cdk-watch", + "lint": "cdk-lint", + "test": "cdk-test", + "integ": "cdk-integ", + "pkglint": "pkglint -f", + "package": "cdk-package", + "awslint": "cdk-awslint", + "cfn2ts": "cfn2ts", + "build+test+package": "npm run build+test && npm run package", + "build+test": "npm run build && npm test" + }, + "jest": { + "moduleFileExtensions": [ + "js" + ], + "coverageThreshold": { + "global": { + "branches": 30, + "statements": 80 + } + } + }, + "keywords": [ + "aws", + "cdk", + "cloudlib", + "aws-cloudlib", + "aws-clib", + "cloudwatch", + "events" ], - "coverageThreshold": { - "global": { - "branches": 30, - "statements": 80 - } + "author": { + "name": "Amazon Web Services", + "url": "https://aws.amazon.com", + "organization": true + }, + "license": "Apache-2.0", + "devDependencies": { + "@aws-cdk/assert": "^0.32.0", + "@aws-cdk/aws-codecommit": "^0.32.0", + "cdk-build-tools": "^0.32.0", + "cdk-integ-tools": "^0.32.0", + "jest": "^24.7.1", + "pkglint": "^0.32.0" + }, + "dependencies": { + "@aws-cdk/aws-codebuild": "^0.32.0", + "@aws-cdk/aws-codepipeline": "^0.32.0", + "@aws-cdk/aws-ec2": "^0.32.0", + "@aws-cdk/aws-ecs": "^0.32.0", + "@aws-cdk/aws-events": "^0.32.0", + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/aws-lambda": "^0.32.0", + "@aws-cdk/aws-sns": "^0.32.0", + "@aws-cdk/aws-sqs": "^0.32.0", + "@aws-cdk/aws-stepfunctions": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + }, + "homepage": "https://github.com/awslabs/aws-cdk", + "peerDependencies": { + "@aws-cdk/aws-codebuild": "^0.32.0", + "@aws-cdk/aws-codepipeline": "^0.32.0", + "@aws-cdk/aws-ec2": "^0.32.0", + "@aws-cdk/aws-ecs": "^0.32.0", + "@aws-cdk/aws-events": "^0.32.0", + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/aws-lambda": "^0.32.0", + "@aws-cdk/aws-sns": "^0.32.0", + "@aws-cdk/aws-sqs": "^0.32.0", + "@aws-cdk/aws-stepfunctions": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + }, + "engines": { + "node": ">= 8.10.0" } - }, - "keywords": [ - "aws", - "cdk", - "cloudlib", - "aws-cloudlib", - "aws-clib", - "cloudwatch", - "events" - ], - "author": { - "name": "Amazon Web Services", - "url": "https://aws.amazon.com", - "organization": true - }, - "license": "Apache-2.0", - "devDependencies": { - "@aws-cdk/assert": "^0.32.0", - "@aws-cdk/aws-codecommit": "^0.32.0", - "@aws-cdk/aws-sqs": "^0.32.0", - "cdk-build-tools": "^0.32.0", - "cdk-integ-tools": "^0.32.0", - "jest": "^24.7.1", - "pkglint": "^0.32.0" - }, - "dependencies": { - "@aws-cdk/aws-codebuild": "^0.32.0", - "@aws-cdk/aws-codepipeline": "^0.32.0", - "@aws-cdk/aws-ec2": "^0.32.0", - "@aws-cdk/aws-ecs": "^0.32.0", - "@aws-cdk/aws-events": "^0.32.0", - "@aws-cdk/aws-iam": "^0.32.0", - "@aws-cdk/aws-lambda": "^0.32.0", - "@aws-cdk/aws-sns": "^0.32.0", - "@aws-cdk/aws-stepfunctions": "^0.32.0", - "@aws-cdk/cdk": "^0.32.0" - }, - "homepage": "https://github.com/awslabs/aws-cdk", - "peerDependencies": { - "@aws-cdk/aws-codebuild": "^0.32.0", - "@aws-cdk/aws-codepipeline": "^0.32.0", - "@aws-cdk/aws-ec2": "^0.32.0", - "@aws-cdk/aws-ecs": "^0.32.0", - "@aws-cdk/aws-events": "^0.32.0", - "@aws-cdk/aws-iam": "^0.32.0", - "@aws-cdk/aws-lambda": "^0.32.0", - "@aws-cdk/aws-sns": "^0.32.0", - "@aws-cdk/aws-stepfunctions": "^0.32.0", - "@aws-cdk/cdk": "^0.32.0" - }, - "engines": { - "node": ">= 8.10.0" - } -} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json new file mode 100644 index 0000000000000..35556fd6fc110 --- /dev/null +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json @@ -0,0 +1,103 @@ +{ + "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" + ], + "Condition": { + "ArnEquals": { + "aws:SourceArn": { + "Ref": "EveryMinute2BBCEA8F" + } + } + }, + "Effect": "Allow", + "Principal": { + "Service": { + "Fn::Join": [ + "", + [ + "events.", + { + "Ref": "AWS::URLSuffix" + } + ] + ] + } + }, + "Resource": { + "Fn::GetAtt": [ + "MyQueueE6CA6235", + "Arn" + ] + } + }, + { + "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" + } + ] + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts new file mode 100644 index 0000000000000..db4d101a658dd --- /dev/null +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts @@ -0,0 +1,39 @@ +import events = require('@aws-cdk/aws-events'); +import iam = require('@aws-cdk/aws-iam'); +import { PolicyStatementEffect } from '@aws-cdk/aws-iam'; +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'); +const policy = new iam.PolicyStatement(PolicyStatementEffect.Allow); +policy.addActions( + "sqs:SendMessage", + "sqs:SendMessageBatch", + "sqs:GetQueueAttributes", + "sqs:GetQueueUrl" +); +policy.addResource(queue.queueArn); +policy.addCondition("ArnEquals", { + "aws:SourceArn": { + Ref: "EveryMinute2BBCEA8F" + } +}); +policy.addPrincipal(new iam.ServicePrincipal("events")); +queue.addToResourcePolicy(policy); +event.addTarget(new targets.SqsQueue(queue)); + +app.run(); diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts new file mode 100644 index 0000000000000..342eb27e88b82 --- /dev/null +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts @@ -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" }] + })); +}); From f2b4b0d1abb1c73ad1ffa2a2edeecfa5e77dbb79 Mon Sep 17 00:00:00 2001 From: Matteo Madeddu Date: Thu, 30 May 2019 14:37:49 +0200 Subject: [PATCH 2/8] feat(@aws-cdk/aws-events-triggers): introducing sqs target support in event rule --- .../test/sqs/integ.sqs-event-rule-target.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts index db4d101a658dd..332d82b8ece9d 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts @@ -6,8 +6,8 @@ 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 +// Define a rule that triggers an event every 1min. +// Connect the event with a queue. This means that the queue should have // a message sent to it every minute. const app = new cdk.App(); @@ -34,6 +34,6 @@ policy.addCondition("ArnEquals", { }); policy.addPrincipal(new iam.ServicePrincipal("events")); queue.addToResourcePolicy(policy); -event.addTarget(new targets.SqsQueue(queue)); +event.addTarget(new targets.SqsQueue(queue); app.run(); From 2ba8c5e1bf229170b0f7ca711769bf0b42268dc2 Mon Sep 17 00:00:00 2001 From: Matteo Madeddu Date: Thu, 30 May 2019 14:47:34 +0200 Subject: [PATCH 3/8] update package-lock.json --- package-lock.json | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/package-lock.json b/package-lock.json index e2c08882a8f6b..9ac5a56e60cde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3,6 +3,81 @@ "requires": true, "lockfileVersion": 1, "dependencies": { + "@aws-cdk/aws-autoscaling-api": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/aws-autoscaling-api/-/aws-autoscaling-api-0.32.0.tgz", + "integrity": "sha512-LSyXgDrAKS0DboM6lCHcBaw5CzvErtqHyBdgfWBPLyqiTnMUaM8m4Wkb32wWdtgMXOfaaRR8/sT6pkPcytFA/g==", + "requires": { + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + } + }, + "@aws-cdk/aws-cloudwatch": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/aws-cloudwatch/-/aws-cloudwatch-0.32.0.tgz", + "integrity": "sha512-crYMC+0vlRflbvhiAt91eC2j0EVG6aQ5rFHQw2zkd7PmZ//pLzw4ZwMF2p49zJuZ5NPTkDKdi+ndj+Xl+gP5yg==", + "requires": { + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + } + }, + "@aws-cdk/aws-iam": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/aws-iam/-/aws-iam-0.32.0.tgz", + "integrity": "sha512-oAJSJ6JRG52BFg3tgx+poCvAt0iFUS1dQiMUtZPnxAp/Ij5agOHLW2THjSHwUBtpUWFLQ4o8hElEUL+TQUG3Yw==", + "requires": { + "@aws-cdk/cdk": "^0.32.0", + "@aws-cdk/region-info": "^0.32.0" + } + }, + "@aws-cdk/aws-kms": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/aws-kms/-/aws-kms-0.32.0.tgz", + "integrity": "sha512-yIpHjfWSKmRtweToRRXqU3/Se6plW4iq4rSmq+uco6AMdmGcmkKYfTl8A+IxdkpO7J3I95UGSEk70Fc7XCuFWA==", + "requires": { + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + } + }, + "@aws-cdk/aws-s3-notifications": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/aws-s3-notifications/-/aws-s3-notifications-0.32.0.tgz", + "integrity": "sha512-rBXfz/NsMeLmZ3ANd2+pMMi4RgrebxBMq0kmE9iWMNfaW7gfgwcTcdY+S0Szh+VdEN/LiyCzwCJL4F7bihe05A==", + "requires": { + "@aws-cdk/cdk": "^0.32.0" + } + }, + "@aws-cdk/aws-sqs": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/aws-sqs/-/aws-sqs-0.32.0.tgz", + "integrity": "sha512-mq4eUe2dumEYJCKwRpF4VuFOf32UP4smtzvhWYMWYMBDCBW2WzcIDcmvv3PbcS43d7j86xp+VBGYjZGvRXNJGg==", + "requires": { + "@aws-cdk/aws-autoscaling-api": "^0.32.0", + "@aws-cdk/aws-cloudwatch": "^0.32.0", + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/aws-kms": "^0.32.0", + "@aws-cdk/aws-s3-notifications": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + } + }, + "@aws-cdk/cdk": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/cdk/-/cdk-0.32.0.tgz", + "integrity": "sha512-/OIp++CSI+BydfgaJeV7YY2oNWzpxQVYjkym/Ydeyzd8Zq0dr0fj0jgY1t0iBLt10QEkaxzlJrIjNxsBWdsn8A==", + "requires": { + "@aws-cdk/cx-api": "^0.32.0" + } + }, + "@aws-cdk/cx-api": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/cx-api/-/cx-api-0.32.0.tgz", + "integrity": "sha512-cuES2BrQqYchwA+t+ZYxgnqJQcKBaazLEujrsy+0IOXvqeHmCE1wlAC741H1z9Yi47fprUPsZO5GrKu7BPlahQ==" + }, + "@aws-cdk/region-info": { + "version": "0.32.0", + "resolved": "https://registry.npmjs.org/@aws-cdk/region-info/-/region-info-0.32.0.tgz", + "integrity": "sha512-4sUUMAua/yjI8BY/mfE6VUmUZGaTHdFw85Up/nf9xjecDNfr1yuPeaCrveVzzDE1/bUC7809PStzMqu866rFWw==" + }, "@babel/code-frame": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.0.0.tgz", From 30b04ab38af0622cf39e0abed2035b8e433b238b Mon Sep 17 00:00:00 2001 From: Matteo Madeddu Date: Thu, 30 May 2019 16:04:03 +0200 Subject: [PATCH 4/8] fix typo in package.json files. fix permission issue in test https://github.com/awslabs/aws-cdk/pull/2683\#discussion_r288987667 --- package.json | 3 - .../@aws-cdk/aws-events-targets/package.json | 213 +++++++++--------- .../integ.sqs-event-rule-target.expected.json | 35 --- .../test/sqs/integ.sqs-event-rule-target.ts | 23 +- 4 files changed, 109 insertions(+), 165 deletions(-) diff --git a/package.json b/package.json index b58dca1fb3173..ce32bc43b81c4 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,5 @@ "author": { "name": "Amazon Web Services", "url": "https://aws.amazon.com" - }, - "dependencies": { - "@aws-cdk/aws-sqs": "^0.32.0" } } diff --git a/packages/@aws-cdk/aws-events-targets/package.json b/packages/@aws-cdk/aws-events-targets/package.json index 7b789af73fcac..bc6414589242b 100644 --- a/packages/@aws-cdk/aws-events-targets/package.json +++ b/packages/@aws-cdk/aws-events-targets/package.json @@ -1,111 +1,110 @@ { - "name": "@aws-cdk/aws-events-targets", - "version": "0.32.0", - "description": "Event targets for AWS CloudWatch Events", - "main": "lib/index.js", - "types": "lib/index.d.ts", - "jsii": { - "outdir": "dist", - "targets": { - "java": { - "package": "software.amazon.awscdk.services.events.targets", - "maven": { - "groupId": "software.amazon.awscdk", - "artifactId": "events-targets" - } - }, - "dotnet": { - "namespace": "Amazon.CDK.AWS.Events.Targets", - "packageId": "Amazon.CDK.AWS.Events.Targets", - "signAssembly": true, - "assemblyOriginatorKeyFile": "../../key.snk" - }, - "python": { - "distName": "aws-cdk.aws-events-targets", - "module": "aws_cdk.aws_events_targets" - } + "name": "@aws-cdk/aws-events-targets", + "version": "0.32.0", + "description": "Event targets for AWS CloudWatch Events", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "jsii": { + "outdir": "dist", + "targets": { + "java": { + "package": "software.amazon.awscdk.services.events.targets", + "maven": { + "groupId": "software.amazon.awscdk", + "artifactId": "events-targets" } - }, - "repository": { - "type": "git", - "url": "https://github.com/awslabs/aws-cdk.git", - "directory": "packages/@aws-cdk/aws-events-targets" - }, - "scripts": { - "build": "cdk-build", - "watch": "cdk-watch", - "lint": "cdk-lint", - "test": "cdk-test", - "integ": "cdk-integ", - "pkglint": "pkglint -f", - "package": "cdk-package", - "awslint": "cdk-awslint", - "cfn2ts": "cfn2ts", - "build+test+package": "npm run build+test && npm run package", - "build+test": "npm run build && npm test" - }, - "jest": { - "moduleFileExtensions": [ - "js" - ], - "coverageThreshold": { - "global": { - "branches": 30, - "statements": 80 - } - } - }, - "keywords": [ - "aws", - "cdk", - "cloudlib", - "aws-cloudlib", - "aws-clib", - "cloudwatch", - "events" + }, + "dotnet": { + "namespace": "Amazon.CDK.AWS.Events.Targets", + "packageId": "Amazon.CDK.AWS.Events.Targets", + "signAssembly": true, + "assemblyOriginatorKeyFile": "../../key.snk" + }, + "python": { + "distName": "aws-cdk.aws-events-targets", + "module": "aws_cdk.aws_events_targets" + } + } + }, + "repository": { + "type": "git", + "url": "https://github.com/awslabs/aws-cdk.git", + "directory": "packages/@aws-cdk/aws-events-targets" + }, + "scripts": { + "build": "cdk-build", + "watch": "cdk-watch", + "lint": "cdk-lint", + "test": "cdk-test", + "integ": "cdk-integ", + "pkglint": "pkglint -f", + "package": "cdk-package", + "awslint": "cdk-awslint", + "cfn2ts": "cfn2ts", + "build+test+package": "npm run build+test && npm run package", + "build+test": "npm run build && npm test" + }, + "jest": { + "moduleFileExtensions": [ + "js" ], - "author": { - "name": "Amazon Web Services", - "url": "https://aws.amazon.com", - "organization": true - }, - "license": "Apache-2.0", - "devDependencies": { - "@aws-cdk/assert": "^0.32.0", - "@aws-cdk/aws-codecommit": "^0.32.0", - "cdk-build-tools": "^0.32.0", - "cdk-integ-tools": "^0.32.0", - "jest": "^24.7.1", - "pkglint": "^0.32.0" - }, - "dependencies": { - "@aws-cdk/aws-codebuild": "^0.32.0", - "@aws-cdk/aws-codepipeline": "^0.32.0", - "@aws-cdk/aws-ec2": "^0.32.0", - "@aws-cdk/aws-ecs": "^0.32.0", - "@aws-cdk/aws-events": "^0.32.0", - "@aws-cdk/aws-iam": "^0.32.0", - "@aws-cdk/aws-lambda": "^0.32.0", - "@aws-cdk/aws-sns": "^0.32.0", - "@aws-cdk/aws-sqs": "^0.32.0", - "@aws-cdk/aws-stepfunctions": "^0.32.0", - "@aws-cdk/cdk": "^0.32.0" - }, - "homepage": "https://github.com/awslabs/aws-cdk", - "peerDependencies": { - "@aws-cdk/aws-codebuild": "^0.32.0", - "@aws-cdk/aws-codepipeline": "^0.32.0", - "@aws-cdk/aws-ec2": "^0.32.0", - "@aws-cdk/aws-ecs": "^0.32.0", - "@aws-cdk/aws-events": "^0.32.0", - "@aws-cdk/aws-iam": "^0.32.0", - "@aws-cdk/aws-lambda": "^0.32.0", - "@aws-cdk/aws-sns": "^0.32.0", - "@aws-cdk/aws-sqs": "^0.32.0", - "@aws-cdk/aws-stepfunctions": "^0.32.0", - "@aws-cdk/cdk": "^0.32.0" - }, - "engines": { - "node": ">= 8.10.0" + "coverageThreshold": { + "global": { + "branches": 30, + "statements": 80 + } } -} \ No newline at end of file + }, + "keywords": [ + "aws", + "cdk", + "cloudlib", + "aws-cloudlib", + "aws-clib", + "cloudwatch", + "events" + ], + "author": { + "name": "Amazon Web Services", + "url": "https://aws.amazon.com", + "organization": true + }, + "license": "Apache-2.0", + "devDependencies": { + "@aws-cdk/assert": "^0.32.0", + "@aws-cdk/aws-codecommit": "^0.32.0", + "@aws-cdk/aws-sqs": "^0.32.0", + "cdk-build-tools": "^0.32.0", + "cdk-integ-tools": "^0.32.0", + "jest": "^24.7.1", + "pkglint": "^0.32.0" + }, + "dependencies": { + "@aws-cdk/aws-codebuild": "^0.32.0", + "@aws-cdk/aws-codepipeline": "^0.32.0", + "@aws-cdk/aws-ec2": "^0.32.0", + "@aws-cdk/aws-ecs": "^0.32.0", + "@aws-cdk/aws-events": "^0.32.0", + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/aws-lambda": "^0.32.0", + "@aws-cdk/aws-sns": "^0.32.0", + "@aws-cdk/aws-stepfunctions": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + }, + "homepage": "https://github.com/awslabs/aws-cdk", + "peerDependencies": { + "@aws-cdk/aws-codebuild": "^0.32.0", + "@aws-cdk/aws-codepipeline": "^0.32.0", + "@aws-cdk/aws-ec2": "^0.32.0", + "@aws-cdk/aws-ecs": "^0.32.0", + "@aws-cdk/aws-events": "^0.32.0", + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/aws-lambda": "^0.32.0", + "@aws-cdk/aws-sns": "^0.32.0", + "@aws-cdk/aws-stepfunctions": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + }, + "engines": { + "node": ">= 8.10.0" + } +} diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json index 35556fd6fc110..2a1625dfd1668 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json @@ -26,41 +26,6 @@ "Properties": { "PolicyDocument": { "Statement": [ - { - "Action": [ - "sqs:SendMessage", - "sqs:SendMessageBatch", - "sqs:GetQueueAttributes", - "sqs:GetQueueUrl" - ], - "Condition": { - "ArnEquals": { - "aws:SourceArn": { - "Ref": "EveryMinute2BBCEA8F" - } - } - }, - "Effect": "Allow", - "Principal": { - "Service": { - "Fn::Join": [ - "", - [ - "events.", - { - "Ref": "AWS::URLSuffix" - } - ] - ] - } - }, - "Resource": { - "Fn::GetAtt": [ - "MyQueueE6CA6235", - "Arn" - ] - } - }, { "Action": [ "sqs:SendMessage", diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts index 332d82b8ece9d..843ca7e170a2d 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts @@ -1,13 +1,11 @@ import events = require('@aws-cdk/aws-events'); -import iam = require('@aws-cdk/aws-iam'); -import { PolicyStatementEffect } from '@aws-cdk/aws-iam'; import sqs = require('@aws-cdk/aws-sqs'); import cdk = require('@aws-cdk/cdk'); import targets = require('../../lib'); // --------------------------------- -// Define a rule that triggers an event every 1min. -// Connect the event with a queue. This means that the queue should have +// 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(); @@ -19,21 +17,6 @@ const event = new events.Rule(stack, 'EveryMinute', { }); const queue = new sqs.Queue(stack, 'MyQueue'); -const policy = new iam.PolicyStatement(PolicyStatementEffect.Allow); -policy.addActions( - "sqs:SendMessage", - "sqs:SendMessageBatch", - "sqs:GetQueueAttributes", - "sqs:GetQueueUrl" -); -policy.addResource(queue.queueArn); -policy.addCondition("ArnEquals", { - "aws:SourceArn": { - Ref: "EveryMinute2BBCEA8F" - } -}); -policy.addPrincipal(new iam.ServicePrincipal("events")); -queue.addToResourcePolicy(policy); -event.addTarget(new targets.SqsQueue(queue); +event.addTarget(new targets.SqsQueue(queue)); app.run(); From 45dea030ea3e1cd3d0af832456ca2f46a6039d4f Mon Sep 17 00:00:00 2001 From: Matteo Madeddu Date: Fri, 31 May 2019 17:03:14 +0200 Subject: [PATCH 5/8] add condition in permission. add messageGroupId --- .../@aws-cdk/aws-events-targets/lib/sqs.ts | 30 ++- .../@aws-cdk/aws-events-targets/package.json | 213 +++++++++--------- .../integ.sqs-event-rule-target.expected.json | 12 +- .../test/sqs/integ.sqs-event-rule-target.ts | 2 +- .../aws-events-targets/test/sqs/sqs.test.ts | 77 ++++++- 5 files changed, 214 insertions(+), 120 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/lib/sqs.ts b/packages/@aws-cdk/aws-events-targets/lib/sqs.ts index 791d68f59b29c..e015f9d2b3bb0 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/sqs.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/sqs.ts @@ -2,16 +2,27 @@ 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 + */ + readonly messageGroupId?: string; + /** * The message to send to the queue * * @default the entire CloudWatch event */ readonly message?: events.RuleTargetInput; + } /** @@ -25,6 +36,7 @@ export interface SqsQueueProps { * */ export class SqsQueue implements events.IRuleTarget { + constructor(public readonly queue: sqs.IQueue, private readonly props: SqsQueueProps = {}) { } @@ -34,14 +46,26 @@ export class SqsQueue implements events.IRuleTarget { * * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/resource-based-policies-cwe.html#sqs-permissions */ - public bind(_rule: events.IRule): events.RuleTargetProperties { + public bind(rule: events.IRule): events.RuleTargetProperties { // deduplicated automatically - this.queue.grantSendMessages(new iam.ServicePrincipal('events.amazonaws.com')); + this.queue.grantSendMessages(new iam.ServicePrincipal('events.amazonaws.com', + { + conditions: { + ArnEquals: { "aws:SourceArn": rule.ruleArn } + } + }) + ); - return { + 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) { + Object.assign(result, { sqsParameters: { messageGroupId: this.props.messageGroupId } }); + } + return result; + } + } diff --git a/packages/@aws-cdk/aws-events-targets/package.json b/packages/@aws-cdk/aws-events-targets/package.json index bc6414589242b..394945b394ba2 100644 --- a/packages/@aws-cdk/aws-events-targets/package.json +++ b/packages/@aws-cdk/aws-events-targets/package.json @@ -1,110 +1,111 @@ { - "name": "@aws-cdk/aws-events-targets", - "version": "0.32.0", - "description": "Event targets for AWS CloudWatch Events", - "main": "lib/index.js", - "types": "lib/index.d.ts", - "jsii": { - "outdir": "dist", - "targets": { - "java": { - "package": "software.amazon.awscdk.services.events.targets", - "maven": { - "groupId": "software.amazon.awscdk", - "artifactId": "events-targets" + "name": "@aws-cdk/aws-events-targets", + "version": "0.32.0", + "description": "Event targets for AWS CloudWatch Events", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "jsii": { + "outdir": "dist", + "targets": { + "java": { + "package": "software.amazon.awscdk.services.events.targets", + "maven": { + "groupId": "software.amazon.awscdk", + "artifactId": "events-targets" + } + }, + "dotnet": { + "namespace": "Amazon.CDK.AWS.Events.Targets", + "packageId": "Amazon.CDK.AWS.Events.Targets", + "signAssembly": true, + "assemblyOriginatorKeyFile": "../../key.snk" + }, + "python": { + "distName": "aws-cdk.aws-events-targets", + "module": "aws_cdk.aws_events_targets" + } } - }, - "dotnet": { - "namespace": "Amazon.CDK.AWS.Events.Targets", - "packageId": "Amazon.CDK.AWS.Events.Targets", - "signAssembly": true, - "assemblyOriginatorKeyFile": "../../key.snk" - }, - "python": { - "distName": "aws-cdk.aws-events-targets", - "module": "aws_cdk.aws_events_targets" - } - } - }, - "repository": { - "type": "git", - "url": "https://github.com/awslabs/aws-cdk.git", - "directory": "packages/@aws-cdk/aws-events-targets" - }, - "scripts": { - "build": "cdk-build", - "watch": "cdk-watch", - "lint": "cdk-lint", - "test": "cdk-test", - "integ": "cdk-integ", - "pkglint": "pkglint -f", - "package": "cdk-package", - "awslint": "cdk-awslint", - "cfn2ts": "cfn2ts", - "build+test+package": "npm run build+test && npm run package", - "build+test": "npm run build && npm test" - }, - "jest": { - "moduleFileExtensions": [ - "js" + }, + "repository": { + "type": "git", + "url": "https://github.com/awslabs/aws-cdk.git", + "directory": "packages/@aws-cdk/aws-events-targets" + }, + "scripts": { + "build": "cdk-build", + "watch": "cdk-watch", + "lint": "cdk-lint", + "test": "cdk-test", + "integ": "cdk-integ", + "pkglint": "pkglint -f", + "package": "cdk-package", + "awslint": "cdk-awslint", + "cfn2ts": "cfn2ts", + "build+test+package": "npm run build+test && npm run package", + "build+test": "npm run build && npm test" + }, + "jest": { + "moduleFileExtensions": [ + "js" + ], + "coverageThreshold": { + "global": { + "branches": 30, + "statements": 80 + } + } + }, + "keywords": [ + "aws", + "cdk", + "cloudlib", + "aws-cloudlib", + "aws-clib", + "cloudwatch", + "events" ], - "coverageThreshold": { - "global": { - "branches": 30, - "statements": 80 - } + "author": { + "name": "Amazon Web Services", + "url": "https://aws.amazon.com", + "organization": true + }, + "license": "Apache-2.0", + "devDependencies": { + "@aws-cdk/assert": "^0.32.0", + "@aws-cdk/aws-codecommit": "^0.32.0", + "@aws-cdk/aws-sqs": "^0.32.0", + "cdk-build-tools": "^0.32.0", + "cdk-integ-tools": "^0.32.0", + "jest": "^24.7.1", + "pkglint": "^0.32.0" + }, + "dependencies": { + "@aws-cdk/aws-codebuild": "^0.32.0", + "@aws-cdk/aws-codepipeline": "^0.32.0", + "@aws-cdk/aws-ec2": "^0.32.0", + "@aws-cdk/aws-ecs": "^0.32.0", + "@aws-cdk/aws-events": "^0.32.0", + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/aws-lambda": "^0.32.0", + "@aws-cdk/aws-sns": "^0.32.0", + "@aws-cdk/aws-stepfunctions": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + }, + "homepage": "https://github.com/awslabs/aws-cdk", + "peerDependencies": { + "@aws-cdk/aws-codebuild": "^0.32.0", + "@aws-cdk/aws-codepipeline": "^0.32.0", + "@aws-cdk/aws-ec2": "^0.32.0", + "@aws-cdk/aws-ecs": "^0.32.0", + "@aws-cdk/aws-events": "^0.32.0", + "@aws-cdk/aws-iam": "^0.32.0", + "@aws-cdk/aws-lambda": "^0.32.0", + "@aws-cdk/aws-sns": "^0.32.0", + "@aws-cdk/aws-sqs": "^0.32.0", + "@aws-cdk/aws-stepfunctions": "^0.32.0", + "@aws-cdk/cdk": "^0.32.0" + }, + "engines": { + "node": ">= 8.10.0" } - }, - "keywords": [ - "aws", - "cdk", - "cloudlib", - "aws-cloudlib", - "aws-clib", - "cloudwatch", - "events" - ], - "author": { - "name": "Amazon Web Services", - "url": "https://aws.amazon.com", - "organization": true - }, - "license": "Apache-2.0", - "devDependencies": { - "@aws-cdk/assert": "^0.32.0", - "@aws-cdk/aws-codecommit": "^0.32.0", - "@aws-cdk/aws-sqs": "^0.32.0", - "cdk-build-tools": "^0.32.0", - "cdk-integ-tools": "^0.32.0", - "jest": "^24.7.1", - "pkglint": "^0.32.0" - }, - "dependencies": { - "@aws-cdk/aws-codebuild": "^0.32.0", - "@aws-cdk/aws-codepipeline": "^0.32.0", - "@aws-cdk/aws-ec2": "^0.32.0", - "@aws-cdk/aws-ecs": "^0.32.0", - "@aws-cdk/aws-events": "^0.32.0", - "@aws-cdk/aws-iam": "^0.32.0", - "@aws-cdk/aws-lambda": "^0.32.0", - "@aws-cdk/aws-sns": "^0.32.0", - "@aws-cdk/aws-stepfunctions": "^0.32.0", - "@aws-cdk/cdk": "^0.32.0" - }, - "homepage": "https://github.com/awslabs/aws-cdk", - "peerDependencies": { - "@aws-cdk/aws-codebuild": "^0.32.0", - "@aws-cdk/aws-codepipeline": "^0.32.0", - "@aws-cdk/aws-ec2": "^0.32.0", - "@aws-cdk/aws-ecs": "^0.32.0", - "@aws-cdk/aws-events": "^0.32.0", - "@aws-cdk/aws-iam": "^0.32.0", - "@aws-cdk/aws-lambda": "^0.32.0", - "@aws-cdk/aws-sns": "^0.32.0", - "@aws-cdk/aws-stepfunctions": "^0.32.0", - "@aws-cdk/cdk": "^0.32.0" - }, - "engines": { - "node": ">= 8.10.0" - } -} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json index 2a1625dfd1668..cf16e28d07764 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.expected.json @@ -1,6 +1,6 @@ { "Resources": { - "EveryMinute2BBCEA8F": { + "MyRuleA44AB831": { "Type": "AWS::Events::Rule", "Properties": { "ScheduleExpression": "rate(1 minute)", @@ -33,6 +33,16 @@ "sqs:GetQueueAttributes", "sqs:GetQueueUrl" ], + "Condition": { + "ArnEquals": { + "aws:SourceArn": { + "Fn::GetAtt": [ + "MyRuleA44AB831", + "Arn" + ] + } + } + }, "Effect": "Allow", "Principal": { "Service": { diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts index 843ca7e170a2d..f94b83deee11a 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts @@ -12,7 +12,7 @@ const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-cdk-sqs-event-target'); -const event = new events.Rule(stack, 'EveryMinute', { +const event = new events.Rule(stack, 'MyRule', { scheduleExpression: 'rate(1 minute)', }); diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts index 342eb27e88b82..24236fa910ef8 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts @@ -4,7 +4,7 @@ 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', () => { +test('sns topic as an event rule target', () => { // GIVEN const stack = new Stack(); const queue = new sqs.Queue(stack, 'MyQueue'); @@ -26,11 +26,24 @@ test('sqs queue as an event rule target', () => { "sqs:GetQueueAttributes", "sqs:GetQueueUrl" ], - Effect: "Allow", - Principal: { - Service: { "Fn::Join": ["", ["events.", { Ref: "AWS::URLSuffix" }]] }, + Condition: { + ArnEquals: { + "aws:SourceArn": { + "Fn::GetAtt": [ + "MyRuleA44AB831", + "Arn" + ] + } + } }, - Resource: { "Fn::GetAtt": ["MyQueueE6CA6235", "Arn"] }, + Effect: "Allow", + Principal: { Service: { "Fn::Join": ["", ["events.", { Ref: "AWS::URLSuffix" }]] } }, + Resource: { + "Fn::GetAtt": [ + "MyQueueE6CA6235", + "Arn" + ] + } } ], Version: "2012-10-17" @@ -43,20 +56,25 @@ test('sqs queue as an event rule target', () => { State: "ENABLED", Targets: [ { - Arn: { "Fn::GetAtt": ["MyQueueE6CA6235", "Arn"] }, + Arn: { + "Fn::GetAtt": [ + "MyQueueE6CA6235", + "Arn" + ] + }, Id: "MyQueue" } ] })); }); -test('multiple uses of a queue as a target results in a single policy statement', () => { +test('multiple uses of a queue as a target results in multi policy statement because of condition', () => { // GIVEN const stack = new Stack(); const queue = new sqs.Queue(stack, 'MyQueue'); // WHEN - for (let i = 0; i < 5; ++i) { + for (let i = 0; i < 2; ++i) { const rule = new events.Rule(stack, `Rule${i}`, { scheduleExpression: 'rate(1 hour)' }); rule.addTarget(new targets.SqsQueue(queue)); } @@ -72,9 +90,50 @@ test('multiple uses of a queue as a target results in a single policy statement' "sqs:GetQueueAttributes", "sqs:GetQueueUrl" ], + Condition: { + ArnEquals: { + "aws:SourceArn": { + "Fn::GetAtt": [ + "Rule071281D88", + "Arn" + ] + } + } + }, + Effect: "Allow", + Principal: { Service: { "Fn::Join": ["", ["events.", { Ref: "AWS::URLSuffix" }]] } }, + Resource: { + "Fn::GetAtt": [ + "MyQueueE6CA6235", + "Arn" + ] + } + }, + { + Action: [ + "sqs:SendMessage", + "sqs:SendMessageBatch", + "sqs:GetQueueAttributes", + "sqs:GetQueueUrl" + ], + Condition: { + ArnEquals: { + "aws:SourceArn": { + "Fn::GetAtt": [ + "Rule136483A30", + "Arn" + ] + } + } + }, Effect: "Allow", Principal: { Service: { "Fn::Join": ["", ["events.", { Ref: "AWS::URLSuffix" }]] } }, - Resource: { "Fn::GetAtt": ["MyQueueE6CA6235", "Arn"] }, + Resource: { + "Fn::GetAtt": [ + "MyQueueE6CA6235", + "Arn" + ] + } } ], Version: "2012-10-17" From 9094e86b3b10df781275e6d888c96cbd56418efc Mon Sep 17 00:00:00 2001 From: Matteo Madeddu Date: Mon, 3 Jun 2019 18:06:14 +0200 Subject: [PATCH 6/8] Fix fifo messageGroupId handling --- packages/@aws-cdk/aws-events-targets/lib/sqs.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/lib/sqs.ts b/packages/@aws-cdk/aws-events-targets/lib/sqs.ts index e015f9d2b3bb0..cab21170999cb 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/sqs.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/sqs.ts @@ -2,22 +2,24 @@ 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 + * Message Group ID for messages sent to this queue + * + * Required for FIFO queues, leave empty for regular queues. * - * @default empty string + * @default - no message group ID (regular queue) */ readonly messageGroupId?: string; /** - * The message to send to the queue + * The message to send to the queue. + * + * Must be a valid JSON text passed to the target queue. * * @default the entire CloudWatch event */ @@ -61,7 +63,7 @@ export class SqsQueue implements events.IRuleTarget { arn: this.queue.queueArn, input: this.props.message, }; - if (this.queue.queueName.split('.').pop() === fifoQualifier && this.props.messageGroupId !== undefined) { + if (!!this.props.messageGroupId) { Object.assign(result, { sqsParameters: { messageGroupId: this.props.messageGroupId } }); } return result; From d20780b31bf6d23adbb23a0fe632031ab9b74711 Mon Sep 17 00:00:00 2001 From: Matteo Madeddu Date: Mon, 3 Jun 2019 18:17:59 +0200 Subject: [PATCH 7/8] Fix package.json conflict --- packages/@aws-cdk/aws-events-targets/package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/@aws-cdk/aws-events-targets/package.json b/packages/@aws-cdk/aws-events-targets/package.json index 3a851ff0dc6b8..bc3186095fede 100644 --- a/packages/@aws-cdk/aws-events-targets/package.json +++ b/packages/@aws-cdk/aws-events-targets/package.json @@ -80,6 +80,7 @@ "pkglint": "^0.33.0" }, "dependencies": { + "@aws-cdk/aws-cloudformation": "^0.33.0", "@aws-cdk/aws-codebuild": "^0.33.0", "@aws-cdk/aws-codepipeline": "^0.33.0", "@aws-cdk/aws-ec2": "^0.33.0", @@ -93,6 +94,7 @@ }, "homepage": "https://github.com/awslabs/aws-cdk", "peerDependencies": { + "@aws-cdk/aws-cloudformation": "^0.33.0", "@aws-cdk/aws-codebuild": "^0.33.0", "@aws-cdk/aws-codepipeline": "^0.33.0", "@aws-cdk/aws-ec2": "^0.33.0", From 3cde86fcd7d16df866dbd369dc3db66ae178810b Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Tue, 4 Jun 2019 09:53:11 +0200 Subject: [PATCH 8/8] Reflow package.json to indent of 2 spaces --- .../@aws-cdk/aws-events-targets/package.json | 216 +++++++++--------- 1 file changed, 108 insertions(+), 108 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/package.json b/packages/@aws-cdk/aws-events-targets/package.json index bc3186095fede..1b1d12bc7e297 100644 --- a/packages/@aws-cdk/aws-events-targets/package.json +++ b/packages/@aws-cdk/aws-events-targets/package.json @@ -1,113 +1,113 @@ { - "name": "@aws-cdk/aws-events-targets", - "version": "0.33.0", - "description": "Event targets for AWS CloudWatch Events", - "main": "lib/index.js", - "types": "lib/index.d.ts", - "jsii": { - "outdir": "dist", - "targets": { - "java": { - "package": "software.amazon.awscdk.services.events.targets", - "maven": { - "groupId": "software.amazon.awscdk", - "artifactId": "events-targets" - } - }, - "dotnet": { - "namespace": "Amazon.CDK.AWS.Events.Targets", - "packageId": "Amazon.CDK.AWS.Events.Targets", - "signAssembly": true, - "assemblyOriginatorKeyFile": "../../key.snk" - }, - "python": { - "distName": "aws-cdk.aws-events-targets", - "module": "aws_cdk.aws_events_targets" - } + "name": "@aws-cdk/aws-events-targets", + "version": "0.33.0", + "description": "Event targets for AWS CloudWatch Events", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "jsii": { + "outdir": "dist", + "targets": { + "java": { + "package": "software.amazon.awscdk.services.events.targets", + "maven": { + "groupId": "software.amazon.awscdk", + "artifactId": "events-targets" } - }, - "repository": { - "type": "git", - "url": "https://github.com/awslabs/aws-cdk.git", - "directory": "packages/@aws-cdk/aws-events-targets" - }, - "scripts": { - "build": "cdk-build", - "watch": "cdk-watch", - "lint": "cdk-lint", - "test": "cdk-test", - "integ": "cdk-integ", - "pkglint": "pkglint -f", - "package": "cdk-package", - "awslint": "cdk-awslint", - "cfn2ts": "cfn2ts", - "build+test+package": "npm run build+test && npm run package", - "build+test": "npm run build && npm test" - }, - "jest": { - "moduleFileExtensions": [ - "js" - ], - "coverageThreshold": { - "global": { - "branches": 30, - "statements": 80 - } - } - }, - "keywords": [ - "aws", - "cdk", - "cloudlib", - "aws-cloudlib", - "aws-clib", - "cloudwatch", - "events" + }, + "dotnet": { + "namespace": "Amazon.CDK.AWS.Events.Targets", + "packageId": "Amazon.CDK.AWS.Events.Targets", + "signAssembly": true, + "assemblyOriginatorKeyFile": "../../key.snk" + }, + "python": { + "distName": "aws-cdk.aws-events-targets", + "module": "aws_cdk.aws_events_targets" + } + } + }, + "repository": { + "type": "git", + "url": "https://github.com/awslabs/aws-cdk.git", + "directory": "packages/@aws-cdk/aws-events-targets" + }, + "scripts": { + "build": "cdk-build", + "watch": "cdk-watch", + "lint": "cdk-lint", + "test": "cdk-test", + "integ": "cdk-integ", + "pkglint": "pkglint -f", + "package": "cdk-package", + "awslint": "cdk-awslint", + "cfn2ts": "cfn2ts", + "build+test+package": "npm run build+test && npm run package", + "build+test": "npm run build && npm test" + }, + "jest": { + "moduleFileExtensions": [ + "js" ], - "author": { - "name": "Amazon Web Services", - "url": "https://aws.amazon.com", - "organization": true - }, - "license": "Apache-2.0", - "devDependencies": { - "@aws-cdk/assert": "^0.33.0", - "@aws-cdk/aws-codecommit": "^0.33.0", - "@aws-cdk/aws-sqs": "^0.33.0", - "cdk-build-tools": "^0.33.0", - "cdk-integ-tools": "^0.33.0", - "jest": "^24.7.1", - "pkglint": "^0.33.0" - }, - "dependencies": { - "@aws-cdk/aws-cloudformation": "^0.33.0", - "@aws-cdk/aws-codebuild": "^0.33.0", - "@aws-cdk/aws-codepipeline": "^0.33.0", - "@aws-cdk/aws-ec2": "^0.33.0", - "@aws-cdk/aws-ecs": "^0.33.0", - "@aws-cdk/aws-events": "^0.33.0", - "@aws-cdk/aws-iam": "^0.33.0", - "@aws-cdk/aws-lambda": "^0.33.0", - "@aws-cdk/aws-sns": "^0.33.0", - "@aws-cdk/aws-stepfunctions": "^0.33.0", - "@aws-cdk/cdk": "^0.33.0" - }, - "homepage": "https://github.com/awslabs/aws-cdk", - "peerDependencies": { - "@aws-cdk/aws-cloudformation": "^0.33.0", - "@aws-cdk/aws-codebuild": "^0.33.0", - "@aws-cdk/aws-codepipeline": "^0.33.0", - "@aws-cdk/aws-ec2": "^0.33.0", - "@aws-cdk/aws-ecs": "^0.33.0", - "@aws-cdk/aws-events": "^0.33.0", - "@aws-cdk/aws-iam": "^0.33.0", - "@aws-cdk/aws-lambda": "^0.33.0", - "@aws-cdk/aws-sns": "^0.33.0", - "@aws-cdk/aws-sqs": "^0.33.0", - "@aws-cdk/aws-stepfunctions": "^0.33.0", - "@aws-cdk/cdk": "^0.33.0" - }, - "engines": { - "node": ">= 8.10.0" + "coverageThreshold": { + "global": { + "branches": 30, + "statements": 80 + } } + }, + "keywords": [ + "aws", + "cdk", + "cloudlib", + "aws-cloudlib", + "aws-clib", + "cloudwatch", + "events" + ], + "author": { + "name": "Amazon Web Services", + "url": "https://aws.amazon.com", + "organization": true + }, + "license": "Apache-2.0", + "devDependencies": { + "@aws-cdk/assert": "^0.33.0", + "@aws-cdk/aws-codecommit": "^0.33.0", + "@aws-cdk/aws-sqs": "^0.33.0", + "cdk-build-tools": "^0.33.0", + "cdk-integ-tools": "^0.33.0", + "jest": "^24.7.1", + "pkglint": "^0.33.0" + }, + "dependencies": { + "@aws-cdk/aws-cloudformation": "^0.33.0", + "@aws-cdk/aws-codebuild": "^0.33.0", + "@aws-cdk/aws-codepipeline": "^0.33.0", + "@aws-cdk/aws-ec2": "^0.33.0", + "@aws-cdk/aws-ecs": "^0.33.0", + "@aws-cdk/aws-events": "^0.33.0", + "@aws-cdk/aws-iam": "^0.33.0", + "@aws-cdk/aws-lambda": "^0.33.0", + "@aws-cdk/aws-sns": "^0.33.0", + "@aws-cdk/aws-stepfunctions": "^0.33.0", + "@aws-cdk/cdk": "^0.33.0" + }, + "homepage": "https://github.com/awslabs/aws-cdk", + "peerDependencies": { + "@aws-cdk/aws-cloudformation": "^0.33.0", + "@aws-cdk/aws-codebuild": "^0.33.0", + "@aws-cdk/aws-codepipeline": "^0.33.0", + "@aws-cdk/aws-ec2": "^0.33.0", + "@aws-cdk/aws-ecs": "^0.33.0", + "@aws-cdk/aws-events": "^0.33.0", + "@aws-cdk/aws-iam": "^0.33.0", + "@aws-cdk/aws-lambda": "^0.33.0", + "@aws-cdk/aws-sns": "^0.33.0", + "@aws-cdk/aws-sqs": "^0.33.0", + "@aws-cdk/aws-stepfunctions": "^0.33.0", + "@aws-cdk/cdk": "^0.33.0" + }, + "engines": { + "node": ">= 8.10.0" + } } \ No newline at end of file