Skip to content

Commit

Permalink
feat(scheduler-targets): add SnsPublish target
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Nov 4, 2023
1 parent fce26b6 commit 7946c72
Show file tree
Hide file tree
Showing 15 changed files with 35,729 additions and 3 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The following targets are supported:
1. `targets.LambdaInvoke`: [Invoke an AWS Lambda function](#invoke-a-lambda-function))
2. `targets.StepFunctionsStartExecution`: [Start an AWS Step Function](#start-an-aws-step-function)
3. `targets.CodeBuildStartBuild`: [Start a CodeBuild job](#start-a-codebuild-job)
4. `targets.SnsPublish`: [Publish to a SNS topic](#publish-to-a-sns-topic)

## Invoke a Lambda function

Expand Down Expand Up @@ -121,3 +122,21 @@ new Schedule(this, 'Schedule', {
target: new targets.CodeBuildStartBuild(project),
});
```

## Publish to a SNS topic

Use the `SnsPublish` target to publish to a SNS topic.

The code snippet below creates an event rule with a SNS publish as target which is
called every hour by Event Bridge Scheduler.

```ts
import * as sns from 'aws-cdk-lib/aws-sns';

declare const topic: sns.Topic;

new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(Duration.minutes(60)),
target: new targets.SnsPublish(topic),
});
```
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './target';
export * from './codebuild-start-build';
export * from './lambda-invoke';
export * from './sns-publish';
export * from './stepfunctions-start-execution';
export * from './codebuild-start-build';
export * from './target';
34 changes: 34 additions & 0 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/lib/sns-publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
import { Names } from 'aws-cdk-lib';
import { IRole } from 'aws-cdk-lib/aws-iam';
import { ITopic } from 'aws-cdk-lib/aws-sns';
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
import { sameEnvDimension } from './util';

/**
* Use an SNS topic as a target for AWS EventBridge Scheduler.
*/
export class SnsPublish extends ScheduleTargetBase implements IScheduleTarget {
constructor(
private readonly topic: ITopic,
private readonly props: ScheduleTargetBaseProps = {},
) {
super(props, topic.topicArn);
}

protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
if (!sameEnvDimension(this.topic.env.region, schedule.env.region)) {
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.`);
}

if (!sameEnvDimension(this.topic.env.account, schedule.env.account)) {
throw new Error(`Cannot assign topic in account ${this.topic.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the topic must be in the same account.`);
}

if (this.props.role && !sameEnvDimension(this.props.role.env.account, schedule.env.account)) {
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.account}. Both the target and the execution role must be in the same account.`);
}

this.topic.grantPublish(role);
}
}
Loading

0 comments on commit 7946c72

Please sign in to comment.