-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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(scheduler-targets): SqsSendMessage Target #27774
Changes from all commits
269b0bd
e397c2c
41fbf8b
48dca93
32452a6
ec9a162
e1b18f3
d1ab879
38c443f
173f2ed
13489af
4fc9c97
5b2ed77
97acac6
736e359
3bf56da
c585d6c
276298a
3060470
bc9bddc
84e77f6
c5bb455
6ad3e0c
84951af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ISchedule, IScheduleTarget, ScheduleTargetConfig } from '@aws-cdk/aws-scheduler-alpha'; | ||
import { Names, Token } from 'aws-cdk-lib'; | ||
import { IRole } from 'aws-cdk-lib/aws-iam'; | ||
import * as sqs from 'aws-cdk-lib/aws-sqs'; | ||
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target'; | ||
import { sameEnvDimension } from './util'; | ||
|
||
/** | ||
* Properties for a SQS Queue Target | ||
*/ | ||
export interface SqsSendMessageProps extends ScheduleTargetBaseProps { | ||
/** | ||
* The FIFO message group ID to use as the target. | ||
* | ||
* This must be specified when the target is a FIFO queue. If you specify | ||
* a FIFO queue as a target, the queue must have content-based deduplication enabled. | ||
* | ||
* A length of `messageGroupId` must be between 1 and 128. | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-scheduler-schedule-sqsparameters.html#cfn-scheduler-schedule-sqsparameters-messagegroupid | ||
* | ||
* @default - no message group ID | ||
*/ | ||
readonly messageGroupId?: string; | ||
} | ||
|
||
/** | ||
* Use an Amazon SQS Queue as a target for AWS EventBridge Scheduler. | ||
*/ | ||
export class SqsSendMessage extends ScheduleTargetBase implements IScheduleTarget { | ||
constructor( | ||
private readonly queue: sqs.IQueue, | ||
private readonly props: SqsSendMessageProps, | ||
) { | ||
super(props, queue.queueArn); | ||
|
||
if (props.messageGroupId !== undefined) { | ||
if (!Token.isUnresolved(props.messageGroupId) && (props.messageGroupId.length < 1 || props.messageGroupId.length > 128)) { | ||
throw new Error(`messageGroupId length must be between 1 and 128, got ${props.messageGroupId.length}`); | ||
} | ||
if (!queue.fifo) { | ||
throw new Error('target must be a FIFO queue if messageGroupId is specified'); | ||
} | ||
if (!(queue.node.defaultChild as sqs.CfnQueue).contentBasedDeduplication) { | ||
throw new Error('contentBasedDeduplication must be true if the target is a FIFO queue'); | ||
} | ||
Comment on lines
+44
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} else if (queue.fifo) { | ||
throw new Error('messageGroupId must be specified if the target is a FIFO queue'); | ||
} | ||
Comment on lines
+47
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void { | ||
if (!sameEnvDimension(this.queue.env.region, schedule.env.region)) { | ||
throw new Error(`Cannot assign queue in region ${this.queue.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the queue must be in the same region.`); | ||
} | ||
|
||
if (!sameEnvDimension(this.queue.env.account, schedule.env.account)) { | ||
throw new Error(`Cannot assign queue in account ${this.queue.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the queue must be in the same account.`); | ||
} | ||
|
||
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.queue.env.account)) { | ||
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.queue.node)} in account ${this.queue.env.account}. Both the target and the execution role must be in the same account.`); | ||
} | ||
|
||
this.queue.grantSendMessages(role); | ||
} | ||
|
||
protected bindBaseTargetConfig(_schedule: ISchedule): ScheduleTargetConfig { | ||
return { | ||
...super.bindBaseTargetConfig(_schedule), | ||
sqsParameters: { | ||
messageGroupId: this.props.messageGroupId, | ||
}, | ||
}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CFn occurs a following error when the queue is not FIFO queue if messageGroupId is specified. So added this validation.