Skip to content

Commit d1d179f

Browse files
authored
fix(scheduler-targets-alpha): imported lambda function as schedule target throws synth error (#31837)
### Issue # (if applicable) Closes #29284. ### Reason for this change Removed the same env check between the Schedule and the Lambda target to allow use of imported Lambda function as target. ### Description of changes Removed the check that forces the Schedule and the Lambda function to be in the same account and region. ### Description of how you validated changes - Unit test added to ensure no synth error when using imported Lambda function. - Integ test added to ensure imported function works. The test ensures the lambda is executed and did what it is supposed to do (i.e. added tag to itself). ### Checklist - [x] 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*
1 parent 2a48358 commit d1d179f

14 files changed

+24903
-20169
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names } from 'aws-cdk-lib';
32
import { IRole } from 'aws-cdk-lib/aws-iam';
43
import * as lambda from 'aws-cdk-lib/aws-lambda';
54
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
75

86
/**
97
* Use an AWS Lambda function as a target for AWS EventBridge Scheduler.
108
*/
119
export class LambdaInvoke extends ScheduleTargetBase implements IScheduleTarget {
10+
private readonly func: lambda.IFunction;
11+
1212
constructor(
13-
private readonly func: lambda.IFunction,
14-
private readonly props: ScheduleTargetBaseProps,
13+
func: lambda.IFunction,
14+
props: ScheduleTargetBaseProps,
1515
) {
1616
super(props, func.functionArn);
17+
this.func = func;
1718
}
1819

19-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20-
if (!sameEnvDimension(this.func.env.region, schedule.env.region)) {
21-
throw new Error(`Cannot assign function in region ${this.func.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the function must be in the same region.`);
22-
}
23-
24-
if (!sameEnvDimension(this.func.env.account, schedule.env.account)) {
25-
throw new Error(`Cannot assign function in account ${this.func.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the function must be in the same account.`);
26-
}
27-
28-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.func.env.account)) {
29-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.func.node)} in account ${this.func.env.account}. Both the target and the execution role must be in the same account.`);
30-
}
31-
20+
protected addTargetActionToRole(_schedule: ISchedule, role: IRole): void {
3221
this.func.grantInvoke(role);
3322
}
3423
}

0 commit comments

Comments
 (0)