Skip to content
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

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

Open
blimmer opened this issue Feb 27, 2024 · 5 comments · May be fixed by #29615
Open

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

blimmer opened this issue Feb 27, 2024 · 5 comments · May be fixed by #29615
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/medium Medium work item – several days of effort p1

Comments

@blimmer
Copy link
Contributor

blimmer commented Feb 27, 2024

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

@blimmer blimmer added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 27, 2024
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Feb 27, 2024
@pahud
Copy link
Contributor

pahud commented Feb 27, 2024

I guess the Schedule is not aware of that attribute from the lambda target. We probably need to fix that in the Schedule.

@pahud pahud added p1 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Feb 27, 2024
@anan-k
Copy link

anan-k commented Mar 4, 2024

Hi, I'm working on this issue.

@pahud
Copy link
Contributor

pahud commented Mar 5, 2024

@anan-k Awesome! Let me know if you need any help. Have you submit the PR draft?

@anan-k
Copy link

anan-k commented Mar 6, 2024

@pahud Thank you! I have not written the PR draft yet. When I finish it I will inform you.

@anan-k
Copy link

anan-k commented Mar 26, 2024

@pahud
Hi Pahud,

I opened a PR to deal with this issue. I also mention @filletofish because you seem to know a lot about the scheduler-targets-alpha package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/medium Medium work item – several days of effort p1
Projects
None yet
3 participants