Bug: sam local commands cannot handle CDK-defined Lambda functions with same name, but different parents
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
### Description:
I tried to execute a Lambda function defined by CDK locally, but an error occurred.
### Steps to reproduce:
$ cdk --version
2.29.0 (build 47d7ec4)
Execute "sam local invoke" against the CloudFormation template generated by "cdk synth".
The sample code is here.
```TypeScript
import * as cdk from "aws-cdk-lib";
import { Stack, StackProps, Duration } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
import * as origins from "aws-cdk-lib/aws-cloudfront-origins";
import * as s3 from "aws-cdk-lib/aws-s3";
import * as acm from "aws-cdk-lib/aws-certificatemanager";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as path from "path";
export class TestStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const originBucketArn = "hogehoge";
const acmArn = "fugafuga";
const domainName = "example.com";
const viewerRequestFunction = new cloudfront.experimental.EdgeFunction(
this,
"ViewerRequestFunction",
{
runtime: lambda.Runtime.NODEJS_14_X,
handler: "app.lambdaHandler",
code: lambda.Code.fromAsset(
path.join(__dirname, "./src/viewer-request-function")
),
memorySize: 128,
timeout: Duration.seconds(5),
}
);
const originResponseFunction = new cloudfront.experimental.EdgeFunction(
this,
"OriginResponseFunction",
{
runtime: lambda.Runtime.NODEJS_14_X,
handler: "app.lambdaHandler",
code: lambda.Code.fromAsset(
path.join(__dirname, "./src/origin-response-function")
),
memorySize: 512,
timeout: Duration.seconds(10),
}
);
const customCachePolicy = new cloudfront.CachePolicy(
this,
"TestCachePolicy",
{
cachePolicyName: "TestCachingOptimized",
comment: "Custom policy optimized for xxxxx",
defaultTtl: Duration.days(30),
minTtl: Duration.days(30),
maxTtl: Duration.days(30),
queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
enableAcceptEncodingGzip: true,
enableAcceptEncodingBrotli: true,
}
);
const TestDistribution = new cloudfront.Distribution(
this,
"TestDistribution",
{
defaultBehavior: {
origin: new origins.S3Origin(
s3.Bucket.fromBucketAttributes(this, "TestBucket", {
bucketArn: originBucketArn,
region: "ap-northeast-1",
})
),
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD,
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD,
cachePolicy: customCachePolicy,
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.HTTPS_ONLY,
edgeLambdas: [
{
eventType: cloudfront.LambdaEdgeEventType.VIEWER_REQUEST,
functionVersion: viewerRequestFunction.currentVersion,
},
{
eventType: cloudfront.LambdaEdgeEventType.ORIGIN_RESPONSE,
functionVersion: originResponseFunction.currentVersion,
},
],
},
domainNames: [domainName],
minimumProtocolVersion: cloudfront.SecurityPolicyProtocol.TLS_V1_2_2021,
sslSupportMethod: cloudfront.SSLMethod.SNI,
certificate: acm.Certificate.fromCertificateArn(
this,
"TestCertificate",
acmArn
),
comment: "Distribution for xxxxxxxxxxx",
}
);
}
}
new TestStack(new cdk.App(), "TestStack", {
env: {
region: "us-east-1",
},
});
```
### Observed result:
```Log
$ sam local invoke --event test/events/test.json ViewerRequestFunctionFn3C72C1E6 -t cdk.out/TestStack.template.json --debug
2022-06-27 19:18:30,315 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-06-27 19:18:30,315 | Using config file: samconfig.toml, config environment: default
2022-06-27 19:18:30,316 | Expand command line arguments to:
2022-06-27 19:18:30,316 | --template_file=/Users/hogehoge/Develop/test-repo/cdk.out/TestStack.template.json --event=test/events/test.json --function_logical_id=ViewerRequestFunctionFn3C72C1E6 --no_event --layer_cache_basedir=/Users/hogehoge/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2022-06-27 19:18:30,316 | local invoke command is called
2022-06-27 19:18:30,316 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2022-06-27 19:18:30,331 | CDK Path for resource ViewerRequestFunctionFnServiceRole6454EA9B is ['TestStack', 'ViewerRequestFunction', 'Fn', 'ServiceRole', 'Resource']
2022-06-27 19:18:30,331 | CDK Path for resource ViewerRequestFunctionFn3C72C1E6 is ['TestStack', 'ViewerRequestFunction', 'Fn', 'Resource']
2022-06-27 19:18:30,331 | CDK Path for resource ViewerRequestFunctionFnCurrentVersionBC4C8D9B35352a7097c357decbd052ba62c914b1 is ['TestStack', 'ViewerRequestFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-06-27 19:18:30,331 | CDK Path for resource OriginResponseFunctionFnServiceRole7876B1B6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'ServiceRole', 'Resource']
2022-06-27 19:18:30,331 | CDK Path for resource OriginResponseFunctionFnA3A6B6E6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'Resource']
2022-06-27 19:18:30,331 | CDK Path for resource OriginResponseFunctionFnCurrentVersion48582B395bb6e18834b55dbdabc1dbf4dd56fb21 is ['TestStack', 'OriginResponseFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-06-27 19:18:30,331 | CDK Path for resource TestCachePolicy97B85C12 is ['TestStack', 'TestCachePolicy', 'Resource']
2022-06-27 19:18:30,331 | CDK Path for resource TestDistributionOrigin1S3OriginC5F1AFA6 is ['TestStack', 'TestDistribution', 'Origin1', 'S3Origin', 'Resource']
2022-06-27 19:18:30,332 | CDK Path for resource TestDistributionF120A1C5 is ['TestStack', 'TestDistribution', 'Resource']
2022-06-27 19:18:30,332 | CDK Path for resource CDKMetadata is ['TestStack', 'CDKMetadata', 'Default']
2022-06-27 19:18:30,333 | 0 stacks found in the template
2022-06-27 19:18:30,333 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2022-06-27 19:18:30,346 | CDK Path for resource ViewerRequestFunctionFnServiceRole6454EA9B is ['TestStack', 'ViewerRequestFunction', 'Fn', 'ServiceRole', 'Resource']
2022-06-27 19:18:30,346 | CDK Path for resource ViewerRequestFunctionFn3C72C1E6 is ['TestStack', 'ViewerRequestFunction', 'Fn', 'Resource']
2022-06-27 19:18:30,346 | CDK Path for resource ViewerRequestFunctionFnCurrentVersionBC4C8D9B35352a7097c357decbd052ba62c914b1 is ['TestStack', 'ViewerRequestFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-06-27 19:18:30,346 | CDK Path for resource OriginResponseFunctionFnServiceRole7876B1B6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'ServiceRole', 'Resource']
2022-06-27 19:18:30,346 | CDK Path for resource OriginResponseFunctionFnA3A6B6E6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'Resource']
2022-06-27 19:18:30,347 | CDK Path for resource OriginResponseFunctionFnCurrentVersion48582B395bb6e18834b55dbdabc1dbf4dd56fb21 is ['TestStack', 'OriginResponseFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-06-27 19:18:30,347 | CDK Path for resource TestCachePolicy97B85C12 is ['TestStack', 'TestCachePolicy', 'Resource']
2022-06-27 19:18:30,347 | CDK Path for resource TestDistributionOrigin1S3OriginC5F1AFA6 is ['TestStack', 'TestDistribution', 'Origin1', 'S3Origin', 'Resource']
2022-06-27 19:18:30,347 | CDK Path for resource TestDistributionF120A1C5 is ['TestStack', 'TestDistribution', 'Resource']
2022-06-27 19:18:30,347 | CDK Path for resource CDKMetadata is ['TestStack', 'CDKMetadata', 'Default']
2022-06-27 19:18:30,348 | 10 resources found in the stack
2022-06-27 19:18:30,348 | Found Lambda function with name='ViewerRequestFunctionFn3C72C1E6' and CodeUri='asset.5ba995aab434928ae9979f296017cab7d2464273c27ce807a38e5d4cc1b907e7'
2022-06-27 19:18:30,348 | --base-dir is not presented, adjusting uri asset.5ba995aab434928ae9979f296017cab7d2464273c27ce807a38e5d4cc1b907e7 relative to /Users/hogehoge/Develop/test-repo/cdk.out/TestStack.template.json
2022-06-27 19:18:30,348 | Found Lambda function with name='OriginResponseFunctionFnA3A6B6E6' and CodeUri='asset.a853a57d69312600236f900dd438977c8bb58fa9656e5a516d12f56fc08ba8e8'
2022-06-27 19:18:30,348 | --base-dir is not presented, adjusting uri asset.a853a57d69312600236f900dd438977c8bb58fa9656e5a516d12f56fc08ba8e8 relative to /Users/hogehoge/Develop/test-repo/cdk.out/TestStack.template.json
2022-06-27 19:18:30,369 | ViewerRequestFunctionFn3C72C1E6 not found. Possible options in your template: ['Fn']
2022-06-27 19:18:30,370 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '245aade0-f7d9-4c72-ac64-e8117d298c3b', 'installationId': '92dc9904-5d64-4da0-9ee9-248620c346f4', 'sessionId': 'f3a450b7-ecd5-4618-9e53-cf7106b83667', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion': '1.50.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local invoke', 'metricSpecificAttributes': {'projectType': 'CDK'}, 'duration': 54, 'exitReason': 'FunctionNotFound', 'exitCode': 1}}]}
2022-06-27 19:18:30,886 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: Function ViewerRequestFunctionFn3C72C1E6 not found in template
```
Note that local execution succeeded after removing the "aws:cdk:path" metadata from the generated CloudFormation template.
### Expected result:
We expect to be able to execute Lambda functions defined by CDK locally.
### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
1. OS: Mac
2. `sam --version`: SAM CLI, version 1.50.0
3. AWS region: us-east-1
Contributor guide
Assessment
This issue has not been assessed yet.