-
Notifications
You must be signed in to change notification settings - Fork 4k
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): add support for universal target #32341
base: main
Are you sure you want to change the base?
Changes from 5 commits
59def06
58dfca3
0071ae2
4c49741
450a7a9
2ec0567
7b4a4ec
319ae62
3c1c2e3
209f6ea
316aed7
50c8858
2de7da1
c5bb21a
7297027
e0469c6
120eacb
e211940
bec0414
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,111 @@ | ||||||
import { IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha'; | ||||||
import { Aws, Token } from 'aws-cdk-lib'; | ||||||
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam'; | ||||||
import { awsSdkToIamAction } from 'aws-cdk-lib/custom-resources/lib/helpers-internal'; | ||||||
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target'; | ||||||
|
||||||
/** | ||||||
* AWS read-only API action name prefixes that are not supported by EventBridge Scheduler. | ||||||
* | ||||||
* @see https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html | ||||||
*/ | ||||||
const NOT_SUPPORTED_ACTION_PREFIX = [ | ||||||
'get', | ||||||
'describe', | ||||||
'list', | ||||||
'poll', | ||||||
'receive', | ||||||
'search', | ||||||
'scan', | ||||||
'query', | ||||||
'select', | ||||||
'read', | ||||||
'lookup', | ||||||
'discover', | ||||||
'validate', | ||||||
'batchGet', | ||||||
'batchDescribe', | ||||||
'batchRead', | ||||||
'transactGet', | ||||||
'adminGet', | ||||||
'adminList', | ||||||
'testMigration', | ||||||
'retrieve', | ||||||
'testConnection', | ||||||
'translateDocument', | ||||||
'isAuthorized', | ||||||
'invokeModel', | ||||||
]; | ||||||
|
||||||
/** | ||||||
* Properties for a AWS API Target | ||||||
*/ | ||||||
export interface AwsApiProps extends ScheduleTargetBaseProps { | ||||||
/** | ||||||
* The AWS service to call. | ||||||
* | ||||||
* This must be in lowercase. | ||||||
*/ | ||||||
readonly service: string; | ||||||
|
||||||
/** | ||||||
* The API action to call. | ||||||
* | ||||||
* You cannot use read-only API actions such as common GET operations. | ||||||
* For more information, see the {@link https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html}. | ||||||
* | ||||||
* ALso, This must be in camelCase. | ||||||
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.
Suggested change
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. Fixed. |
||||||
*/ | ||||||
readonly action: string; | ||||||
|
||||||
/** | ||||||
* The resource ARNs for the IAM statement that will be added to | ||||||
* the execution role's policy to allow the scheduler to make the API call. | ||||||
* | ||||||
* @default - ['*'] | ||||||
*/ | ||||||
readonly iamResources?: string[]; | ||||||
|
||||||
/** | ||||||
* The action for the IAM statement that will be added to | ||||||
* the execution role's policy to allow the scheduler to make the API call. | ||||||
* | ||||||
* Use in the case where the IAM action name does not match with the | ||||||
* API service/action name, e.g. `s3:listObjectV2` requires `s3:ListBucket`. | ||||||
* | ||||||
* @default - service:action | ||||||
*/ | ||||||
readonly iamAction?: string; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Send an event to an AWS EventBridge by AWS EventBridge Scheduler. | ||||||
*/ | ||||||
export class AwsApi extends ScheduleTargetBase implements IScheduleTarget { | ||||||
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. A class name of https://github.com/aws/aws-cdk-rfcs/blob/main/text/0474-event-bridge-scheduler-l2.md
const target = new targets.Universal('sqs', 'CreateQueue', { input: input }); https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html
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. I missed the RFC document. 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. Could you please change the description in this PR? 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. see:
|
||||||
constructor( | ||||||
private readonly props: AwsApiProps, | ||||||
) { | ||||||
const service = props.service; | ||||||
const action = props.action; | ||||||
|
||||||
if (!Token.isUnresolved(service) && service !== service.toLowerCase()) { | ||||||
throw new Error(`API service must be lowercase, got: ${service}`); | ||||||
} | ||||||
if (!Token.isUnresolved(action) && !action.startsWith(action[0]?.toLowerCase())) { | ||||||
throw new Error(`API action must be camelCase, got: ${action}`); | ||||||
} | ||||||
if (!Token.isUnresolved(action) && NOT_SUPPORTED_ACTION_PREFIX.some(prefix => action.startsWith(prefix))) { | ||||||
throw new Error(`Read-only API action is not supported by EventBridge Scheduler: ${service}:${action}`); | ||||||
} | ||||||
|
||||||
const arn = `arn:${Aws.PARTITION}:scheduler:::aws-sdk:${service}:${action}`; | ||||||
super(props, arn); | ||||||
} | ||||||
|
||||||
protected addTargetActionToRole(role: IRole): void { | ||||||
role.addToPrincipalPolicy(new PolicyStatement({ | ||||||
actions: [this.props.iamAction ?? awsSdkToIamAction(this.props.service, this.props.action)], | ||||||
resources: this.props.iamResources ?? ['*'], | ||||||
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. Are there any cases where more than one action is required? For example: S3 put with KMS key. 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. There may be cases where 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. We can also pass an array of 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. Yes, that could be the case.
|
||||||
})); | ||||||
} | ||||||
} |
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.
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.
My bad, fixed.
209f6ea