Skip to content

Commit

Permalink
fix(scheduler-targets-alpha): imported lambda function as schedule ta…
Browse files Browse the repository at this point in the history
…rget 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*
  • Loading branch information
samson-keung authored Oct 23, 2024
1 parent 2a48358 commit d1d179f
Show file tree
Hide file tree
Showing 14 changed files with 24,903 additions and 20,169 deletions.
23 changes: 6 additions & 17 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/lib/lambda-invoke.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
import { Names } from 'aws-cdk-lib';
import { IRole } from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
import { sameEnvDimension } from './util';

/**
* Use an AWS Lambda function as a target for AWS EventBridge Scheduler.
*/
export class LambdaInvoke extends ScheduleTargetBase implements IScheduleTarget {
private readonly func: lambda.IFunction;

constructor(
private readonly func: lambda.IFunction,
private readonly props: ScheduleTargetBaseProps,
func: lambda.IFunction,
props: ScheduleTargetBaseProps,
) {
super(props, func.functionArn);
this.func = func;
}

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

if (!sameEnvDimension(this.func.env.account, schedule.env.account)) {
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.`);
}

if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.func.env.account)) {
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.`);
}

protected addTargetActionToRole(_schedule: ISchedule, role: IRole): void {
this.func.grantInvoke(role);
}
}
Loading

0 comments on commit d1d179f

Please sign in to comment.