aws / aws/aws-sam-cli

sam-beta-cdk local invoke can't find Lambda function defined in Stack nested inside cdk.Stage

Open
#3,068 7 comments 3 reactions 0 assignees View on GitHub
area/cdk area/lambda-invoke maintainer/need-followup
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

I'm following the CDK pattern of packaging all my application stacks into a cdk.Stage so that I can easily parameterize my application deployment for different environments and leverage cdk-pipelines for continuous deployment: https://aws.amazon.com/blogs/developer/cdk-pipelines-continuous-delivery-for-aws-cdk-applications/.

When I do this though, my lambda functions can't be found by the SAM cli when I try to use local invoke. For an app stage named 'MyApplication' with a stack 'MyStack' where I define 'MyLambdaFunction', I get the following error:
```
Synthesizing CDK App
MyApplication/MyStack/MyLambdaFunction not found. Possible options in your template: []
Error: Function MyApplication/MyStack/MyLambdaFunction not found in template
```
When I don't bundle my app stacks into a cdk.Stage construct then SAM cli can find the function and has no issues invoking it. I'm not sure if using cdk.Stage is the issue or if any stack nesting resulting in more than one level to get to the Lambda function would cause the same issue.

Here's some example cdk code to reproduce:

bin/my-application.ts
```
import * as cdk from '@aws-cdk/core';
import { MyApplication } from '../lib/my-application';

const app = new cdk.App();
new MyApplication(app, 'MyApplication', {});
```
lib/my-application.ts
```
import * as cdk from '@aws-cdk/core';
import { MyStack } from './my-stack';

export class MyApplication extends cdk.Stage {
constructor(scope: cdk.Construct, id: string, props?: cdk.StageProps) {
super(scope, id, props);

new MyStack(this, 'MyStack', {});
}
}
```
lib/my-stack.ts
```
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';

export class MyStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

new lambda.Function(this, 'MyLambdaFunction', {
runtime: lambda.Runtime.NODEJS_14_X,
code: lambda.Code.fromAsset('src/lambda/my-lambda-function'),
handler: 'index.handler'
});
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.