-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scheduler-targets): add support for universal target (#32341)
### Issue # (if applicable) Closes #32328 ### Reason for this change EventBridge Scheduler has a mechanism called Universal Target that calls a wide range of AWS APIs. Supporting this mechanism in L2 Construct will make it easier to configure EventBridge Scheduler. https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html ### Description of changes Added Universal construct targeting AWS APIs. Users can execute any AWS API by passing service and action to Props. According to the following documentation, the service must be lowercase, and the action must be camelCase, so that you can validate it. `arn:aws:scheduler:::aws-sdk:service:apiAction` https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html#:~:text=schedule%20to%20target.-,Arn,-%E2%80%93%20The%20complete%20service ### Description of how you validated changes Added unit tests and integration tests. ### Checklist - [ ] 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*
- Loading branch information
1 parent
f0e2f2a
commit 021e6d6
Showing
14 changed files
with
32,958 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/@aws-cdk/aws-scheduler-targets-alpha/lib/universal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
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 Universal Target | ||
*/ | ||
export interface UniversalTargetProps extends ScheduleTargetBaseProps { | ||
/** | ||
* The AWS service to call. | ||
* | ||
* This must be in lowercase. | ||
*/ | ||
readonly service: string; | ||
|
||
/** | ||
* The API action to call. Must be camelCase. | ||
* | ||
* You cannot use read-only API actions such as common GET operations. | ||
* | ||
* @see https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html#unsupported-api-actions | ||
*/ | ||
readonly action: string; | ||
|
||
/** | ||
* The IAM policy statements needed to invoke the target. These statements are attached to the Scheduler's role. | ||
* | ||
* Note that the default may not be the correct actions as not all AWS services follows the same IAM action pattern, or there may be more actions needed to invoke the target. | ||
* | ||
* @default - Policy with `service:action` action only. | ||
*/ | ||
readonly policyStatements?: PolicyStatement[]; | ||
} | ||
|
||
/** | ||
* Use a wider set of AWS API as a target for AWS EventBridge Scheduler. | ||
* | ||
* @see https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html | ||
*/ | ||
export class Universal extends ScheduleTargetBase implements IScheduleTarget { | ||
constructor( | ||
private readonly props: UniversalTargetProps, | ||
) { | ||
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 { | ||
if (!this.props.policyStatements?.length) { | ||
role.addToPrincipalPolicy(new PolicyStatement({ | ||
actions: [awsSdkToIamAction(this.props.service, this.props.action)], | ||
resources: ['*'], | ||
})); | ||
return; | ||
} | ||
|
||
for (const statement of this.props.policyStatements) { | ||
role.addToPrincipalPolicy(statement); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...r-targets-alpha/test/integ.universal.js.snapshot/AwsSchedulerTargetsUniversal.assets.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
139 changes: 139 additions & 0 deletions
139
...targets-alpha/test/integ.universal.js.snapshot/AwsSchedulerTargetsUniversal.template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
{ | ||
"Resources": { | ||
"Schedule83A77FD1": { | ||
"Type": "AWS::Scheduler::Schedule", | ||
"Properties": { | ||
"FlexibleTimeWindow": { | ||
"Mode": "OFF" | ||
}, | ||
"ScheduleExpression": "rate(1 minute)", | ||
"ScheduleExpressionTimezone": "Etc/UTC", | ||
"State": "ENABLED", | ||
"Target": { | ||
"Arn": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":scheduler:::aws-sdk:sqs:createQueue" | ||
] | ||
] | ||
}, | ||
"Input": "{\"QueueName\":\"aws-scheduler-targets-create-queue\"}", | ||
"RetryPolicy": { | ||
"MaximumEventAgeInSeconds": 86400, | ||
"MaximumRetryAttempts": 185 | ||
}, | ||
"RoleArn": { | ||
"Fn::GetAtt": [ | ||
"SchedulerRoleForTarget5cddf726972933", | ||
"Arn" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"SchedulerRoleForTarget5cddf726972933": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Condition": { | ||
"StringEquals": { | ||
"aws:SourceAccount": { | ||
"Ref": "AWS::AccountId" | ||
}, | ||
"aws:SourceArn": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":scheduler:", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
":", | ||
{ | ||
"Ref": "AWS::AccountId" | ||
}, | ||
":schedule-group/default" | ||
] | ||
] | ||
} | ||
} | ||
}, | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "scheduler.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"SchedulerRoleForTarget5cddf7DefaultPolicy3159C97B": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sqs:CreateQueue", | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "SchedulerRoleForTarget5cddf7DefaultPolicy3159C97B", | ||
"Roles": [ | ||
{ | ||
"Ref": "SchedulerRoleForTarget5cddf726972933" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"Parameters": { | ||
"BootstrapVersion": { | ||
"Type": "AWS::SSM::Parameter::Value<String>", | ||
"Default": "/cdk-bootstrap/hnb659fds/version", | ||
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" | ||
} | ||
}, | ||
"Rules": { | ||
"CheckBootstrapVersion": { | ||
"Assertions": [ | ||
{ | ||
"Assert": { | ||
"Fn::Not": [ | ||
{ | ||
"Fn::Contains": [ | ||
[ | ||
"1", | ||
"2", | ||
"3", | ||
"4", | ||
"5" | ||
], | ||
{ | ||
"Ref": "BootstrapVersion" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." | ||
} | ||
] | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...integ.universal.js.snapshot/IntegTestUniversalDefaultTestDeployAssert42BBB3FD.assets.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.