Skip to content

Commit 2f846b3

Browse files
authored
feat(pipes-targets): add SNS (#34159)
### Issue # (if applicable) N/A ### Reason for this change Add SNS as a Pipes target. ### Checklist - [X] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 904e3a3 commit 2f846b3

File tree

16 files changed

+32244
-1
lines changed

16 files changed

+32244
-1
lines changed

packages/@aws-cdk/aws-pipes-targets-alpha/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Pipe targets are the end point of an EventBridge Pipe. The following targets are
3434
* `targets.LambdaFunction`: [Send event source to a Lambda function](#aws-lambda-function)
3535
* `targets.SageMakerTarget`: [Send event source to a SageMaker pipeline](#amazon-sagemaker-pipeline)
3636
* `targets.SfnStateMachine`: [Invoke a Step Functions state machine from an event source](#aws-step-functions-state-machine)
37-
* `targets.SqsTarget`: [Send event source to an SQS queue](#amazon-sqs)
37+
* `targets.SnsTarget`: [Send event source to an SNS topic](#amazon-sns-topic)
38+
* `targets.SqsTarget`: [Send event source to an SQS queue](#amazon-sqs-queue)
3839

3940
### Amazon EventBridge API Destination
4041

@@ -383,6 +384,44 @@ const pipe = new pipes.Pipe(this, 'Pipe', {
383384
});
384385
```
385386

387+
### Amazon SNS Topic
388+
389+
An SNS topic can be used as a target for a pipe.
390+
The topic will receive the (enriched/filtered) source payload.
391+
392+
```ts
393+
declare const sourceQueue: sqs.Queue;
394+
declare const targetTopic: sns.Topic;
395+
396+
const pipeTarget = new targets.SnsTarget(targetTopic);
397+
398+
const pipe = new pipes.Pipe(this, 'Pipe', {
399+
source: new SqsSource(sourceQueue),
400+
target: pipeTarget
401+
});
402+
```
403+
404+
The target input can be transformed:
405+
406+
```ts
407+
declare const sourceQueue: sqs.Queue;
408+
declare const targetTopic: sns.Topic;
409+
410+
const pipeTarget = new targets.SnsTarget(targetTopic,
411+
{
412+
inputTransformation: pipes.InputTransformation.fromObject(
413+
{
414+
"SomeKey": pipes.DynamicInput.fromEventPath('$.body')
415+
})
416+
}
417+
);
418+
419+
const pipe = new pipes.Pipe(this, 'Pipe', {
420+
source: new SqsSource(sourceQueue),
421+
target: pipeTarget
422+
});
423+
```
424+
386425
### Amazon SQS Queue
387426

388427
An SQS queue can be used as a target for a pipe.

packages/@aws-cdk/aws-pipes-targets-alpha/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export * from './firehose';
66
export * from './kinesis';
77
export * from './lambda';
88
export * from './sagemaker';
9+
export * from './sns';
910
export * from './sqs';
1011
export * from './stepfunctions';
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha';
2+
import { IRole } from 'aws-cdk-lib/aws-iam';
3+
import { ITopic } from 'aws-cdk-lib/aws-sns';
4+
5+
/**
6+
* SNS target properties.
7+
*/
8+
export interface SnsTargetParameters {
9+
/**
10+
* The input transformation to apply to the message before sending it to the target.
11+
*
12+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetparameters.html#cfn-pipes-pipe-pipetargetparameters-inputtemplate
13+
* @default - none
14+
*/
15+
readonly inputTransformation?: IInputTransformation;
16+
}
17+
18+
/**
19+
* A EventBridge Pipes target that sends messages to an SNS topic.
20+
*/
21+
export class SnsTarget implements ITarget {
22+
private topic: ITopic;
23+
private topicParameters?: SnsTargetParameters;
24+
public readonly targetArn: string;
25+
26+
constructor(topic: ITopic, parameters?: SnsTargetParameters) {
27+
this.topic = topic;
28+
this.targetArn = topic.topicArn;
29+
this.topicParameters = parameters;
30+
}
31+
32+
grantPush(grantee: IRole): void {
33+
this.topic.grantPublish(grantee);
34+
}
35+
36+
bind(pipe: IPipe): TargetConfig {
37+
if (!this.topicParameters?.inputTransformation) {
38+
return {
39+
targetParameters: {},
40+
};
41+
}
42+
43+
return {
44+
targetParameters: {
45+
inputTemplate: this.topicParameters.inputTransformation?.bind(pipe).inputTemplate,
46+
},
47+
};
48+
}
49+
}

packages/@aws-cdk/aws-pipes-targets-alpha/rosetta/default.ts-fixture

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as kinesis from 'aws-cdk-lib/aws-kinesis';
66
import * as firehose from 'aws-cdk-lib/aws-kinesisfirehose';
77
import * as logs from 'aws-cdk-lib/aws-logs';
88
import * as sagemaker from 'aws-cdk-lib/aws-sagemaker';
9+
import * as sns from 'aws-cdk-lib/aws-sns';
910
import * as sqs from 'aws-cdk-lib/aws-sqs';
1011
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
1112
import * as lambda from 'aws-cdk-lib/aws-lambda';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`sns should grant pipe role publish access 1`] = `
4+
{
5+
"MyPipeRoleCBC8E9AB": {
6+
"Properties": {
7+
"AssumeRolePolicyDocument": {
8+
"Statement": [
9+
{
10+
"Action": "sts:AssumeRole",
11+
"Effect": "Allow",
12+
"Principal": {
13+
"Service": "pipes.amazonaws.com",
14+
},
15+
},
16+
],
17+
"Version": "2012-10-17",
18+
},
19+
},
20+
"Type": "AWS::IAM::Role",
21+
},
22+
}
23+
`;
24+
25+
exports[`sns should grant pipe role publish access 2`] = `
26+
{
27+
"MyPipeRoleDefaultPolicy31387C20": {
28+
"Properties": {
29+
"PolicyDocument": {
30+
"Statement": [
31+
{
32+
"Action": "sns:Publish",
33+
"Effect": "Allow",
34+
"Resource": {
35+
"Ref": "MyTopic86869434",
36+
},
37+
},
38+
],
39+
"Version": "2012-10-17",
40+
},
41+
"PolicyName": "MyPipeRoleDefaultPolicy31387C20",
42+
"Roles": [
43+
{
44+
"Ref": "MyPipeRoleCBC8E9AB",
45+
},
46+
],
47+
},
48+
"Type": "AWS::IAM::Policy",
49+
},
50+
}
51+
`;

0 commit comments

Comments
 (0)