Skip to content

Commit ff203a1

Browse files
authored
feat(scheduler-targets-alpha): SnsPublish scheduler target (#27838)
Closes #27459 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent b09257c commit ff203a1

File tree

16 files changed

+35916
-4
lines changed

16 files changed

+35916
-4
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The following targets are supported:
2828
2. `targets.StepFunctionsStartExecution`: [Start an AWS Step Function](#start-an-aws-step-function)
2929
3. `targets.CodeBuildStartBuild`: [Start a CodeBuild job](#start-a-codebuild-job)
3030
4. `targets.SqsSendMessage`: [Send a Message to an Amazon SQS Queue](#send-a-message-to-sqs-queue)
31+
5. `targets.SnsPublish`: [Publish messages to an Amazon SNS topic](#publish-messages-to-an-amazon-sns-topic)
3132

3233
## Invoke a Lambda function
3334

@@ -151,3 +152,29 @@ new Schedule(this, 'Schedule', {
151152
target
152153
});
153154
```
155+
156+
## Publish messages to an Amazon SNS topic
157+
158+
Use the `SnsPublish` target to publish messages to an Amazon SNS topic.
159+
160+
The code snippets below create create an event rule with a Amazon SNS topic as a target.
161+
It's called every hour by Amazon Event Bridge Scheduler with custom payload.
162+
163+
```ts
164+
import * as sns from 'aws-cdk-lib/aws-sns';
165+
166+
const topic = new sns.Topic(this, 'Topic');
167+
168+
const payload = {
169+
message: 'Hello scheduler!',
170+
};
171+
172+
const target = new targets.SnsPublish(topic, {
173+
input: ScheduleTargetInput.fromObject(payload),
174+
});
175+
176+
new Schedule(this, 'Schedule', {
177+
schedule: ScheduleExpression.rate(Duration.hours(1)),
178+
target,
179+
});
180+
```
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export * from './target';
2-
export * from './lambda-invoke';
3-
export * from './stepfunctions-start-execution';
41
export * from './codebuild-start-build';
2+
export * from './lambda-invoke';
3+
export * from './sns-publish';
54
export * from './sqs-send-message';
5+
export * from './stepfunctions-start-execution';
6+
export * from './target';
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2+
import { Names } from 'aws-cdk-lib';
3+
import { IRole } from 'aws-cdk-lib/aws-iam';
4+
import * as sns from 'aws-cdk-lib/aws-sns';
5+
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6+
import { sameEnvDimension } from './util';
7+
8+
/**
9+
* Use an Amazon SNS topic as a target for AWS EventBridge Scheduler.
10+
*/
11+
export class SnsPublish extends ScheduleTargetBase implements IScheduleTarget {
12+
constructor(
13+
private readonly topic: sns.ITopic,
14+
private readonly props: ScheduleTargetBaseProps = {},
15+
) {
16+
super(props, topic.topicArn);
17+
}
18+
19+
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20+
// Check if target and schedule are in the region
21+
if (!sameEnvDimension(this.topic.env.region, schedule.env.region)) {
22+
throw new Error(`Cannot assign topic in region ${this.topic.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the topic must be in the same region.`);
23+
}
24+
25+
// Check if target and schedule are in the same account
26+
if (!sameEnvDimension(this.topic.env.account, schedule.env.account)) {
27+
throw new Error(`Cannot assign topic in account ${this.topic.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${role.env.account}. Both the schedule and the topic must be in the same account.`);
28+
}
29+
30+
// Check if target and role are in the same account
31+
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.topic.env.account)) {
32+
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to publish to target ${Names.nodeUniqueId(this.topic.node)} in account ${this.topic.env.account}. Both the target and the execution role must be in the same account.`);
33+
}
34+
35+
this.topic.grantPublish(role);
36+
}
37+
}

packages/@aws-cdk/aws-scheduler-targets-alpha/test/integ.sns-publish.js.snapshot/AwsSchedulerTargetsSnsPublish.assets.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
{
2+
"Resources": {
3+
"TopicBFC7AF6E": {
4+
"Type": "AWS::SNS::Topic"
5+
},
6+
"Queue4A7E3555": {
7+
"Type": "AWS::SQS::Queue",
8+
"UpdateReplacePolicy": "Delete",
9+
"DeletionPolicy": "Delete"
10+
},
11+
"QueuePolicy25439813": {
12+
"Type": "AWS::SQS::QueuePolicy",
13+
"Properties": {
14+
"PolicyDocument": {
15+
"Statement": [
16+
{
17+
"Action": "sqs:SendMessage",
18+
"Condition": {
19+
"ArnEquals": {
20+
"aws:SourceArn": {
21+
"Ref": "TopicBFC7AF6E"
22+
}
23+
}
24+
},
25+
"Effect": "Allow",
26+
"Principal": {
27+
"Service": "sns.amazonaws.com"
28+
},
29+
"Resource": {
30+
"Fn::GetAtt": [
31+
"Queue4A7E3555",
32+
"Arn"
33+
]
34+
}
35+
}
36+
],
37+
"Version": "2012-10-17"
38+
},
39+
"Queues": [
40+
{
41+
"Ref": "Queue4A7E3555"
42+
}
43+
]
44+
}
45+
},
46+
"QueueAwsSchedulerTargetsSnsPublishTopicCB9BF6E1C346AD60": {
47+
"Type": "AWS::SNS::Subscription",
48+
"Properties": {
49+
"Endpoint": {
50+
"Fn::GetAtt": [
51+
"Queue4A7E3555",
52+
"Arn"
53+
]
54+
},
55+
"Protocol": "sqs",
56+
"RawMessageDelivery": true,
57+
"TopicArn": {
58+
"Ref": "TopicBFC7AF6E"
59+
}
60+
},
61+
"DependsOn": [
62+
"QueuePolicy25439813"
63+
]
64+
},
65+
"Schedule83A77FD1": {
66+
"Type": "AWS::Scheduler::Schedule",
67+
"Properties": {
68+
"FlexibleTimeWindow": {
69+
"Mode": "OFF"
70+
},
71+
"ScheduleExpression": "rate(1 minute)",
72+
"ScheduleExpressionTimezone": "Etc/UTC",
73+
"State": "ENABLED",
74+
"Target": {
75+
"Arn": {
76+
"Ref": "TopicBFC7AF6E"
77+
},
78+
"Input": "\"Hello, Scheduler!\"",
79+
"RetryPolicy": {
80+
"MaximumEventAgeInSeconds": 86400,
81+
"MaximumRetryAttempts": 185
82+
},
83+
"RoleArn": {
84+
"Fn::GetAtt": [
85+
"SchedulerRoleForTarget1441a743A31888",
86+
"Arn"
87+
]
88+
}
89+
}
90+
}
91+
},
92+
"SchedulerRoleForTarget1441a743A31888": {
93+
"Type": "AWS::IAM::Role",
94+
"Properties": {
95+
"AssumeRolePolicyDocument": {
96+
"Statement": [
97+
{
98+
"Action": "sts:AssumeRole",
99+
"Condition": {
100+
"StringEquals": {
101+
"aws:SourceAccount": {
102+
"Ref": "AWS::AccountId"
103+
}
104+
}
105+
},
106+
"Effect": "Allow",
107+
"Principal": {
108+
"Service": "scheduler.amazonaws.com"
109+
}
110+
}
111+
],
112+
"Version": "2012-10-17"
113+
}
114+
}
115+
},
116+
"SchedulerRoleForTarget1441a7DefaultPolicy885B6BFD": {
117+
"Type": "AWS::IAM::Policy",
118+
"Properties": {
119+
"PolicyDocument": {
120+
"Statement": [
121+
{
122+
"Action": "sns:Publish",
123+
"Effect": "Allow",
124+
"Resource": {
125+
"Ref": "TopicBFC7AF6E"
126+
}
127+
}
128+
],
129+
"Version": "2012-10-17"
130+
},
131+
"PolicyName": "SchedulerRoleForTarget1441a7DefaultPolicy885B6BFD",
132+
"Roles": [
133+
{
134+
"Ref": "SchedulerRoleForTarget1441a743A31888"
135+
}
136+
]
137+
}
138+
}
139+
},
140+
"Outputs": {
141+
"ExportsOutputRefQueue4A7E3555425E8BD3": {
142+
"Value": {
143+
"Ref": "Queue4A7E3555"
144+
},
145+
"Export": {
146+
"Name": "AwsSchedulerTargetsSnsPublish:ExportsOutputRefQueue4A7E3555425E8BD3"
147+
}
148+
}
149+
},
150+
"Parameters": {
151+
"BootstrapVersion": {
152+
"Type": "AWS::SSM::Parameter::Value<String>",
153+
"Default": "/cdk-bootstrap/hnb659fds/version",
154+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
155+
}
156+
},
157+
"Rules": {
158+
"CheckBootstrapVersion": {
159+
"Assertions": [
160+
{
161+
"Assert": {
162+
"Fn::Not": [
163+
{
164+
"Fn::Contains": [
165+
[
166+
"1",
167+
"2",
168+
"3",
169+
"4",
170+
"5"
171+
],
172+
{
173+
"Ref": "BootstrapVersion"
174+
}
175+
]
176+
}
177+
]
178+
},
179+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
180+
}
181+
]
182+
}
183+
}
184+
}

packages/@aws-cdk/aws-scheduler-targets-alpha/test/integ.sns-publish.js.snapshot/IntegTestSnsPublishDefaultTestDeployAssert36D5D430.assets.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)