Skip to content

(aws-scheduler-targets-alpha): Cannot use imported function ARN with LambdaInvoke target #29284

@blimmer

Description

@blimmer

Describe the bug

I'm trying to invoke a "singleton" lambda created in another Stack. I expose the Function ARN via a CloudFormation output. If the lambda function ARN is being imported from a CFN output (e.g., Fn.importValue), even with sameEnvironment: true, an error is thrown at synth time.

Expected Behavior

I expected the validation for the region to be ignored if I use Lambda.fromFunctionAttributes with the sameEnvironment value set to true.

Current Behavior

I get this error:

/private/tmp/bug-report/node_modules/@aws-cdk/aws-scheduler-targets-alpha/lib/lambda-invoke.ts: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.`);
            ^
Error: Cannot assign function in region ${Token[TOKEN.22]} to the schedule BugReportStackSchedule10380292 in region us-east-2. Both the schedule and the function must be in the same region.
    at LambdaInvoke.addTargetActionToRole (/private/tmp/bug-report/node_modules/@aws-cdk/aws-scheduler-targets-alpha/lib/lambda-invoke.ts:21:13)
    at LambdaInvoke.bindBaseTargetConfig (/private/tmp/bug-report/node_modules/@aws-cdk/aws-scheduler-targets-alpha/lib/target.ts:80:10)
    at LambdaInvoke.bind (/private/tmp/bug-report/node_modules/@aws-cdk/aws-scheduler-targets-alpha/lib/target.ts:103:17)
    at new Schedule (/private/tmp/bug-report/node_modules/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts:315:39)
    at new BugReportStack (/private/tmp/bug-report/lib/bug-report-stack.ts:29:5)
    at Object.<anonymous> (/private/tmp/bug-report/bin/bug-report.ts:7:1)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module.m._compile (/private/tmp/bug-report/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Object.require.extensions.<computed> [as .ts] (/private/tmp/bug-report/node_modules/ts-node/src/index.ts:1621:12)

Reproduction Steps

import { Schedule, ScheduleExpression } from '@aws-cdk/aws-scheduler-alpha';
import { ScheduleTargetInput } from '@aws-cdk/aws-scheduler-alpha/lib/input';
import { LambdaInvoke } from '@aws-cdk/aws-scheduler-targets-alpha';
import * as cdk from 'aws-cdk-lib';
import { Fn } from 'aws-cdk-lib';
import { Function } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';

export class BugReportStack extends cdk.Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id, {
      env: {
        account: '123456789012',
        region: 'us-east-2',
      }
    });

    const functionArn = Fn.importValue("MyImportArn");
    const lambda = Function.fromFunctionAttributes(this, "ImportedFunction", {
      functionArn,
      sameEnvironment: true,
    });

    const target = new LambdaInvoke(lambda, {
      input: ScheduleTargetInput.fromObject({
        foo: "bar"
      }),
    });
    new Schedule(this, "Schedule", {
      schedule: ScheduleExpression.rate(cdk.Duration.minutes(5)),
      target,
    });
  }
}

Possible Solution

If sameEnvironment is set to true on the imported lambda, the validations should be skipped.

Additional Information/Context

Alpha versions are latest as well

{
  "name": "bug-report",
  "version": "0.1.0",
  "bin": {
    "bug-report": "bin/bug-report.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@types/jest": "^29.5.12",
    "@types/node": "20.11.19",
    "aws-cdk": "2.130.0",
    "jest": "^29.7.0",
    "ts-jest": "^29.1.2",
    "ts-node": "^10.9.2",
    "typescript": "~5.3.3"
  },
  "dependencies": {
    "@aws-cdk/aws-scheduler-alpha": "^2.130.0-alpha.0",
    "@aws-cdk/aws-scheduler-targets-alpha": "^2.130.0-alpha.0",
    "aws-cdk-lib": "2.130.0",
    "constructs": "^10.0.0",
    "source-map-support": "^0.5.21"
  }
}

CDK CLI Version

2.130.0 (build bd6e5ee)

Framework Version

No response

Node.js Version

20.x

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-lambdaRelated to AWS LambdabugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions