aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Bug: sam local commands cannot handle CDK-defined Lambda functions with same name, but different parents

Open sinokuma opened this issue 3 years ago • 11 comments

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.

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:

$ 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

sinokuma avatar Jul 06 '22 11:07 sinokuma

Thanks for reporting this issue.

I remember there was a similar issue which was fixed in recent releases of SAM CLI. Can you try to update your SAM CLI version to latest and give it another try?

mndeveci avatar Jul 06 '22 12:07 mndeveci

I executed the same command with a newer version of the SAM CLI and got the same error.

% cdk --version
2.31.0 (build b67950d)

 % sam --version
SAM CLI, version 1.53.0

% sam local invoke ViewerRequestFunctionFn3C72C1E6 -t cdk.out/TestStack.template.json --debug
2022-07-07 17:49:59,044 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-07-07 17:49:59,044 | Using config file: samconfig.toml, config environment: default
2022-07-07 17:49:59,044 | Expand command line arguments to:
2022-07-07 17:49:59,044 | --template_file=/Users/sinokuma/Desktop/cdk-bug-test/cdk.out/TestStack.template.json --function_logical_id=ViewerRequestFunctionFn3C72C1E6 --no_event --layer_cache_basedir=/Users/sinokuma/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1 
2022-07-07 17:49:59,044 | local invoke command is called
2022-07-07 17:49:59,044 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2022-07-07 17:49:59,064 | CDK Path for resource ViewerRequestFunctionFnServiceRole6454EA9B is ['TestStack', 'ViewerRequestFunction', 'Fn', 'ServiceRole', 'Resource']
2022-07-07 17:49:59,065 | CDK Path for resource ViewerRequestFunctionFn3C72C1E6 is ['TestStack', 'ViewerRequestFunction', 'Fn', 'Resource']
2022-07-07 17:49:59,065 | CDK Path for resource ViewerRequestFunctionFnCurrentVersionBC4C8D9Bea90ba6386abe14233bd0d4de0c817bd is ['TestStack', 'ViewerRequestFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-07-07 17:49:59,065 | CDK Path for resource OriginResponseFunctionFnServiceRole7876B1B6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'ServiceRole', 'Resource']
2022-07-07 17:49:59,065 | CDK Path for resource OriginResponseFunctionFnA3A6B6E6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'Resource']
2022-07-07 17:49:59,065 | CDK Path for resource OriginResponseFunctionFnCurrentVersion48582B39392ed21bc91ceb4feaf1167d2733083b is ['TestStack', 'OriginResponseFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-07-07 17:49:59,066 | CDK Path for resource TestCachePolicy65E5B40F is ['TestStack', 'TestCachePolicy', 'Resource']
2022-07-07 17:49:59,066 | CDK Path for resource TestDistributionOrigin1S3OriginF767E587 is ['TestStack', 'TestDistribution', 'Origin1', 'S3Origin', 'Resource']
2022-07-07 17:49:59,066 | CDK Path for resource TestDistribution94EC811C is ['TestStack', 'TestDistribution', 'Resource']
2022-07-07 17:49:59,066 | CDK Path for resource CDKMetadata is ['TestStack', 'CDKMetadata', 'Default']
2022-07-07 17:49:59,067 | 0 stacks found in the template
2022-07-07 17:49:59,067 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2022-07-07 17:49:59,079 | CDK Path for resource ViewerRequestFunctionFnServiceRole6454EA9B is ['TestStack', 'ViewerRequestFunction', 'Fn', 'ServiceRole', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource ViewerRequestFunctionFn3C72C1E6 is ['TestStack', 'ViewerRequestFunction', 'Fn', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource ViewerRequestFunctionFnCurrentVersionBC4C8D9Bea90ba6386abe14233bd0d4de0c817bd is ['TestStack', 'ViewerRequestFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource OriginResponseFunctionFnServiceRole7876B1B6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'ServiceRole', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource OriginResponseFunctionFnA3A6B6E6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource OriginResponseFunctionFnCurrentVersion48582B39392ed21bc91ceb4feaf1167d2733083b is ['TestStack', 'OriginResponseFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource TestCachePolicy65E5B40F is ['TestStack', 'TestCachePolicy', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource TestDistributionOrigin1S3OriginF767E587 is ['TestStack', 'TestDistribution', 'Origin1', 'S3Origin', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource TestDistribution94EC811C is ['TestStack', 'TestDistribution', 'Resource']
2022-07-07 17:49:59,079 | CDK Path for resource CDKMetadata is ['TestStack', 'CDKMetadata', 'Default']
2022-07-07 17:49:59,080 | 10 resources found in the stack 
2022-07-07 17:49:59,080 | Found Lambda function with name='ViewerRequestFunctionFn3C72C1E6' and CodeUri='asset.433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a'
2022-07-07 17:49:59,080 | --base-dir is not presented, adjusting uri asset.433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a relative to /Users/sinokuma/Desktop/cdk-bug-test/cdk.out/TestStack.template.json
2022-07-07 17:49:59,080 | Found Lambda function with name='OriginResponseFunctionFnA3A6B6E6' and CodeUri='asset.433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a'
2022-07-07 17:49:59,080 | --base-dir is not presented, adjusting uri asset.433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a relative to /Users/sinokuma/Desktop/cdk-bug-test/cdk.out/TestStack.template.json
2022-07-07 17:49:59,094 | ViewerRequestFunctionFn3C72C1E6 not found. Possible options in your template: ['Fn']
2022-07-07 17:49:59,095 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'd9888adf-0201-431f-8785-0cfeb7288125', 'installationId': '92dc9904-5d64-4da0-9ee9-248620c346f4', 'sessionId': '51ed099c-7a21-4d25-a3da-5d260e652bfa', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion': '1.53.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local invoke', 'metricSpecificAttributes': {'projectType': 'CDK'}, 'duration': 51, 'exitReason': 'FunctionNotFound', 'exitCode': 1}}]}
2022-07-07 17:49:59,603 | 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

sinokuma avatar Jul 07 '22 08:07 sinokuma

Thanks for confirming that, I will check with the team and get back to you.

mndeveci avatar Jul 07 '22 12:07 mndeveci

@sinokuma .. could you please share with us the synthesized template.

moelasmar avatar Jul 07 '22 16:07 moelasmar

@moelasmar

The template is here.

{
  "Resources": {
    "ViewerRequestFunctionFnServiceRole6454EA9B": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Statement": [
            {
              "Action": "sts:AssumeRole",
              "Effect": "Allow",
              "Principal": {
                "Service": ["edgelambda.amazonaws.com", "lambda.amazonaws.com"]
              }
            }
          ],
          "Version": "2012-10-17"
        },
        "ManagedPolicyArns": [
          {
            "Fn::Join": [
              "",
              [
                "arn:",
                {
                  "Ref": "AWS::Partition"
                },
                ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
              ]
            ]
          }
        ]
      },
      "Metadata": {
        "aws:cdk:path": "TestStack/ViewerRequestFunction/Fn/ServiceRole/Resource"
      }
    },
    "ViewerRequestFunctionFn3C72C1E6": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "S3Bucket": {
            "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-us-east-1"
          },
          "S3Key": "433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a.zip"
        },
        "Role": {
          "Fn::GetAtt": ["ViewerRequestFunctionFnServiceRole6454EA9B", "Arn"]
        },
        "Handler": "app.lambdaHandler",
        "MemorySize": 128,
        "Runtime": "nodejs14.x",
        "Timeout": 5
      },
      "DependsOn": ["ViewerRequestFunctionFnServiceRole6454EA9B"],
      "Metadata": {
        "aws:cdk:path": "TestStack/ViewerRequestFunction/Fn/Resource",
        "aws:asset:path": "asset.433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a",
        "aws:asset:is-bundled": false,
        "aws:asset:property": "Code"
      }
    },
    "ViewerRequestFunctionFnCurrentVersionBC4C8D9Bea90ba6386abe14233bd0d4de0c817bd": {
      "Type": "AWS::Lambda::Version",
      "Properties": {
        "FunctionName": {
          "Ref": "ViewerRequestFunctionFn3C72C1E6"
        }
      },
      "Metadata": {
        "aws:cdk:path": "TestStack/ViewerRequestFunction/Fn/CurrentVersion/Resource"
      }
    },
    "OriginResponseFunctionFnServiceRole7876B1B6": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Statement": [
            {
              "Action": "sts:AssumeRole",
              "Effect": "Allow",
              "Principal": {
                "Service": ["edgelambda.amazonaws.com", "lambda.amazonaws.com"]
              }
            }
          ],
          "Version": "2012-10-17"
        },
        "ManagedPolicyArns": [
          {
            "Fn::Join": [
              "",
              [
                "arn:",
                {
                  "Ref": "AWS::Partition"
                },
                ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
              ]
            ]
          }
        ]
      },
      "Metadata": {
        "aws:cdk:path": "TestStack/OriginResponseFunction/Fn/ServiceRole/Resource"
      }
    },
    "OriginResponseFunctionFnA3A6B6E6": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "S3Bucket": {
            "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-us-east-1"
          },
          "S3Key": "433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a.zip"
        },
        "Role": {
          "Fn::GetAtt": ["OriginResponseFunctionFnServiceRole7876B1B6", "Arn"]
        },
        "Handler": "app.lambdaHandler",
        "MemorySize": 512,
        "Runtime": "nodejs14.x",
        "Timeout": 10
      },
      "DependsOn": ["OriginResponseFunctionFnServiceRole7876B1B6"],
      "Metadata": {
        "aws:cdk:path": "TestStack/OriginResponseFunction/Fn/Resource",
        "aws:asset:path": "asset.433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a",
        "aws:asset:is-bundled": false,
        "aws:asset:property": "Code"
      }
    },
    "OriginResponseFunctionFnCurrentVersion48582B39392ed21bc91ceb4feaf1167d2733083b": {
      "Type": "AWS::Lambda::Version",
      "Properties": {
        "FunctionName": {
          "Ref": "OriginResponseFunctionFnA3A6B6E6"
        }
      },
      "Metadata": {
        "aws:cdk:path": "TestStack/OriginResponseFunction/Fn/CurrentVersion/Resource"
      }
    },
    "TestCachePolicy65E5B40F": {
      "Type": "AWS::CloudFront::CachePolicy",
      "Properties": {
        "CachePolicyConfig": {
          "Comment": "Custom policy optimized for xxxxx",
          "DefaultTTL": 2592000,
          "MaxTTL": 2592000,
          "MinTTL": 2592000,
          "Name": "TestCachingOptimized",
          "ParametersInCacheKeyAndForwardedToOrigin": {
            "CookiesConfig": {
              "CookieBehavior": "none"
            },
            "EnableAcceptEncodingBrotli": true,
            "EnableAcceptEncodingGzip": true,
            "HeadersConfig": {
              "HeaderBehavior": "none"
            },
            "QueryStringsConfig": {
              "QueryStringBehavior": "all"
            }
          }
        }
      },
      "Metadata": {
        "aws:cdk:path": "TestStack/TestCachePolicy/Resource"
      }
    },
    "TestDistributionOrigin1S3OriginF767E587": {
      "Type": "AWS::CloudFront::CloudFrontOriginAccessIdentity",
      "Properties": {
        "CloudFrontOriginAccessIdentityConfig": {
          "Comment": "Identity for TestStackTestDistributionOrigin1C273C9E6"
        }
      },
      "Metadata": {
        "aws:cdk:path": "TestStack/TestDistribution/Origin1/S3Origin/Resource"
      }
    },
    "TestDistribution94EC811C": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "Aliases": ["example.com"],
          "Comment": "Distribution for xxxxxxxxxxx",
          "DefaultCacheBehavior": {
            "AllowedMethods": ["GET", "HEAD"],
            "CachePolicyId": {
              "Ref": "TestCachePolicy65E5B40F"
            },
            "CachedMethods": ["GET", "HEAD"],
            "Compress": true,
            "LambdaFunctionAssociations": [
              {
                "EventType": "viewer-request",
                "LambdaFunctionARN": {
                  "Ref": "ViewerRequestFunctionFnCurrentVersionBC4C8D9Bea90ba6386abe14233bd0d4de0c817bd"
                }
              },
              {
                "EventType": "origin-response",
                "LambdaFunctionARN": {
                  "Ref": "OriginResponseFunctionFnCurrentVersion48582B39392ed21bc91ceb4feaf1167d2733083b"
                }
              }
            ],
            "TargetOriginId": "TestStackTestDistributionOrigin1C273C9E6",
            "ViewerProtocolPolicy": "https-only"
          },
          "Enabled": true,
          "HttpVersion": "http2",
          "IPV6Enabled": true,
          "Origins": [
            {
              "DomainName": {
                "Fn::Join": [
                  "",
                  [
                    "hogehoge.s3.ap-northeast-1.",
                    {
                      "Ref": "AWS::URLSuffix"
                    }
                  ]
                ]
              },
              "Id": "TestStackTestDistributionOrigin1C273C9E6",
              "S3OriginConfig": {
                "OriginAccessIdentity": {
                  "Fn::Join": [
                    "",
                    [
                      "origin-access-identity/cloudfront/",
                      {
                        "Ref": "TestDistributionOrigin1S3OriginF767E587"
                      }
                    ]
                  ]
                }
              }
            }
          ]
        }
      },
      "Metadata": {
        "aws:cdk:path": "TestStack/TestDistribution/Resource"
      }
    },
    "CDKMetadata": {
      "Type": "AWS::CDK::Metadata",
      "Properties": {
        "Analytics": "v2:deflate64:H4sIAAAAAAAA/3VQy07DMBD8Fu7OQikHrm2hEidQkbhWznqTbuPYyGvzkJV/x04r6IXTzM7sY7S3sFzAzZX+lAbN0FhuIb9GjYMq0j6j9cl0wbsImb7eKfBILmoLj6anbXIY2Tu10XigF28Zv9Wmc5flA0sM3Ka57zlwz26FSCJPpizieBqoV7b1yn8dl2smZfXYGg35L0DnfvkbBTlrZzop1iPknbdU1YqTkuVei1AUWFUoNawTDhTXWoo9i+URJU0/D5H4FLA4zhuCo1x/LO7hrrzuKMxNSCXqSLA74Q8yUDUtVgEAAA=="
      },
      "Metadata": {
        "aws:cdk:path": "TestStack/CDKMetadata/Default"
      }
    }
  },
  "Parameters": {
    "BootstrapVersion": {
      "Type": "AWS::SSM::Parameter::Value<String>",
      "Default": "/cdk-bootstrap/hnb659fds/version",
      "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
    }
  },
  "Rules": {
    "CheckBootstrapVersion": {
      "Assertions": [
        {
          "Assert": {
            "Fn::Not": [
              {
                "Fn::Contains": [
                  ["1", "2", "3", "4", "5"],
                  {
                    "Ref": "BootstrapVersion"
                  }
                ]
              }
            ]
          },
          "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
        }
      ]
    }
  }
}

sinokuma avatar Jul 09 '22 10:07 sinokuma

@sinokuma Looking at the output the resource doesn't have a valid output path from the cdk synth

"ViewerRequestFunctionFn3C72C1E6": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "S3Bucket": {
            "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-us-east-1"
          },
          "S3Key": "433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a.zip"
        },
        "Role": {
          "Fn::GetAtt": ["ViewerRequestFunctionFnServiceRole6454EA9B", "Arn"]
        },
        "Handler": "app.lambdaHandler",
        "MemorySize": 128,
        "Runtime": "nodejs14.x",
        "Timeout": 5
      },
      "DependsOn": ["ViewerRequestFunctionFnServiceRole6454EA9B"],
      "Metadata": {
        "aws:cdk:path": "TestStack/ViewerRequestFunction/Fn/Resource",
        "aws:asset:path": "asset.433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a",
        "aws:asset:is-bundled": false,
        "aws:asset:property": "Code"
      }

We expect the aws:asset:path to be the full path and assume the asset.433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a file is not in the same directory as this template?

Our docs call out you need to do cdk synth --no-staging. Can you try that command instead of just cdk synth?

jfuss avatar Jul 12 '22 15:07 jfuss

@jfuss --no-staging option did not change the result. Please check the results. Still need additional information?

% cdk synth --no-staging
Resources:
  ViewerRequestFunctionFnServiceRole6454EA9B:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
                - edgelambda.amazonaws.com
                - lambda.amazonaws.com
        Version: "2012-10-17"
      ManagedPolicyArns:
        - Fn::Join:
            - ""
            - - "arn:"
              - Ref: AWS::Partition
              - :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
    Metadata:
      aws:cdk:path: TestStack/ViewerRequestFunction/Fn/ServiceRole/Resource
  ViewerRequestFunctionFn3C72C1E6:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket:
          Fn::Sub: cdk-hnb659fds-assets-${AWS::AccountId}-us-east-1
        S3Key: 433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a.zip
      Role:
        Fn::GetAtt:
          - ViewerRequestFunctionFnServiceRole6454EA9B
          - Arn
      Handler: app.lambdaHandler
      MemorySize: 128
      Runtime: nodejs14.x
      Timeout: 5
    DependsOn:
      - ViewerRequestFunctionFnServiceRole6454EA9B
    Metadata:
      aws:cdk:path: TestStack/ViewerRequestFunction/Fn/Resource
      aws:asset:path: /Users/sinokuma/Desktop/cdk-bug-test/src/viewer-request-function
      aws:asset:is-bundled: false
      aws:asset:property: Code
  ViewerRequestFunctionFnCurrentVersionBC4C8D9Bea90ba6386abe14233bd0d4de0c817bd:
    Type: AWS::Lambda::Version
    Properties:
      FunctionName:
        Ref: ViewerRequestFunctionFn3C72C1E6
    Metadata:
      aws:cdk:path: TestStack/ViewerRequestFunction/Fn/CurrentVersion/Resource
  OriginResponseFunctionFnServiceRole7876B1B6:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
                - edgelambda.amazonaws.com
                - lambda.amazonaws.com
        Version: "2012-10-17"
      ManagedPolicyArns:
        - Fn::Join:
            - ""
            - - "arn:"
              - Ref: AWS::Partition
              - :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
    Metadata:
      aws:cdk:path: TestStack/OriginResponseFunction/Fn/ServiceRole/Resource
  OriginResponseFunctionFnA3A6B6E6:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket:
          Fn::Sub: cdk-hnb659fds-assets-${AWS::AccountId}-us-east-1
        S3Key: 433b1ae261fd2563b9f6e554e04d9fdc7f52709f71767ca25ea5df71c9bb654a.zip
      Role:
        Fn::GetAtt:
          - OriginResponseFunctionFnServiceRole7876B1B6
          - Arn
      Handler: app.lambdaHandler
      MemorySize: 512
      Runtime: nodejs14.x
      Timeout: 10
    DependsOn:
      - OriginResponseFunctionFnServiceRole7876B1B6
    Metadata:
      aws:cdk:path: TestStack/OriginResponseFunction/Fn/Resource
      aws:asset:path: /Users/sinokuma/Desktop/cdk-bug-test/src/origin-response-function
      aws:asset:is-bundled: false
      aws:asset:property: Code
  OriginResponseFunctionFnCurrentVersion48582B39392ed21bc91ceb4feaf1167d2733083b:
    Type: AWS::Lambda::Version
    Properties:
      FunctionName:
        Ref: OriginResponseFunctionFnA3A6B6E6
    Metadata:
      aws:cdk:path: TestStack/OriginResponseFunction/Fn/CurrentVersion/Resource
  TestCachePolicy65E5B40F:
    Type: AWS::CloudFront::CachePolicy
    Properties:
      CachePolicyConfig:
        Comment: Custom policy optimized for xxxxx
        DefaultTTL: 2592000
        MaxTTL: 2592000
        MinTTL: 2592000
        Name: TestCachingOptimized
        ParametersInCacheKeyAndForwardedToOrigin:
          CookiesConfig:
            CookieBehavior: none
          EnableAcceptEncodingBrotli: true
          EnableAcceptEncodingGzip: true
          HeadersConfig:
            HeaderBehavior: none
          QueryStringsConfig:
            QueryStringBehavior: all
    Metadata:
      aws:cdk:path: TestStack/TestCachePolicy/Resource
  TestDistributionOrigin1S3OriginF767E587:
    Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: Identity for TestStackTestDistributionOrigin1C273C9E6
    Metadata:
      aws:cdk:path: TestStack/TestDistribution/Origin1/S3Origin/Resource
  TestDistribution94EC811C:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Aliases:
          - example.com
        Comment: Distribution for xxxxxxxxxxx
        DefaultCacheBehavior:
          AllowedMethods:
            - GET
            - HEAD
          CachePolicyId:
            Ref: TestCachePolicy65E5B40F
          CachedMethods:
            - GET
            - HEAD
          Compress: true
          LambdaFunctionAssociations:
            - EventType: viewer-request
              LambdaFunctionARN:
                Ref: ViewerRequestFunctionFnCurrentVersionBC4C8D9Bea90ba6386abe14233bd0d4de0c817bd
            - EventType: origin-response
              LambdaFunctionARN:
                Ref: OriginResponseFunctionFnCurrentVersion48582B39392ed21bc91ceb4feaf1167d2733083b
          TargetOriginId: TestStackTestDistributionOrigin1C273C9E6
          ViewerProtocolPolicy: https-only
        Enabled: true
        HttpVersion: http2
        IPV6Enabled: true
        Origins:
          - DomainName:
              Fn::Join:
                - ""
                - - hogehoge.s3.ap-northeast-1.
                  - Ref: AWS::URLSuffix
            Id: TestStackTestDistributionOrigin1C273C9E6
            S3OriginConfig:
              OriginAccessIdentity:
                Fn::Join:
                  - ""
                  - - origin-access-identity/cloudfront/
                    - Ref: TestDistributionOrigin1S3OriginF767E587
    Metadata:
      aws:cdk:path: TestStack/TestDistribution/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Analytics: v2:deflate64:H4sIAAAAAAAA/3VQy07DMBD8Fu7OQikHrm2hEidQkbhWznqTbuPYyGvzkJV/x04r6IXTzM7sY7S3sFzAzZX+lAbN0FhuIb9GjYMq0j6j9cl0wbsImb7eKfBILmoLj6anbXIY2Tu10XigF28Zv9Wmc5flA0sM3Ka57zlwz26FSCJPpizieBqoV7b1yn8dl2smZfXYGg35L0DnfvkbBTlrZzop1iPknbdU1YqTkuVei1AUWFUoNawTDhTXWoo9i+URJU0/D5H4FLA4zhuCo1x/LO7hrrzuKMxNSCXqSLA74Q8yUDUtVgEAAA==
    Metadata:
      aws:cdk:path: TestStack/CDKMetadata/Default
Parameters:
  BootstrapVersion:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /cdk-bootstrap/hnb659fds/version
    Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]
Rules:
  CheckBootstrapVersion:
    Assertions:
      - Assert:
          Fn::Not:
            - Fn::Contains:
                - - "1"
                  - "2"
                  - "3"
                  - "4"
                  - "5"
                - Ref: BootstrapVersion
        AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.

% sam local invoke ViewerRequestFunctionFn3C72C1E6 -t cdk.out/TestStack.template.json --debug
2022-07-13 13:48:37,475 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-07-13 13:48:37,475 | Using config file: samconfig.toml, config environment: default
2022-07-13 13:48:37,475 | Expand command line arguments to:
2022-07-13 13:48:37,475 | --template_file=/Users/sinokuma/Desktop/cdk-bug-test/cdk.out/TestStack.template.json --function_logical_id=ViewerRequestFunctionFn3C72C1E6 --no_event --layer_cache_basedir=/Users/sinokuma/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1 
2022-07-13 13:48:37,475 | local invoke command is called
2022-07-13 13:48:37,475 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2022-07-13 13:48:37,494 | CDK Path for resource ViewerRequestFunctionFnServiceRole6454EA9B is ['TestStack', 'ViewerRequestFunction', 'Fn', 'ServiceRole', 'Resource']
2022-07-13 13:48:37,494 | CDK Path for resource ViewerRequestFunctionFn3C72C1E6 is ['TestStack', 'ViewerRequestFunction', 'Fn', 'Resource']
2022-07-13 13:48:37,494 | CDK Path for resource ViewerRequestFunctionFnCurrentVersionBC4C8D9Bea90ba6386abe14233bd0d4de0c817bd is ['TestStack', 'ViewerRequestFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-07-13 13:48:37,494 | CDK Path for resource OriginResponseFunctionFnServiceRole7876B1B6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'ServiceRole', 'Resource']
2022-07-13 13:48:37,494 | CDK Path for resource OriginResponseFunctionFnA3A6B6E6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'Resource']
2022-07-13 13:48:37,494 | CDK Path for resource OriginResponseFunctionFnCurrentVersion48582B39392ed21bc91ceb4feaf1167d2733083b is ['TestStack', 'OriginResponseFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-07-13 13:48:37,496 | CDK Path for resource TestCachePolicy65E5B40F is ['TestStack', 'TestCachePolicy', 'Resource']
2022-07-13 13:48:37,496 | CDK Path for resource TestDistributionOrigin1S3OriginF767E587 is ['TestStack', 'TestDistribution', 'Origin1', 'S3Origin', 'Resource']
2022-07-13 13:48:37,496 | CDK Path for resource TestDistribution94EC811C is ['TestStack', 'TestDistribution', 'Resource']
2022-07-13 13:48:37,496 | CDK Path for resource CDKMetadata is ['TestStack', 'CDKMetadata', 'Default']
2022-07-13 13:48:37,497 | 0 stacks found in the template
2022-07-13 13:48:37,497 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2022-07-13 13:48:37,509 | CDK Path for resource ViewerRequestFunctionFnServiceRole6454EA9B is ['TestStack', 'ViewerRequestFunction', 'Fn', 'ServiceRole', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource ViewerRequestFunctionFn3C72C1E6 is ['TestStack', 'ViewerRequestFunction', 'Fn', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource ViewerRequestFunctionFnCurrentVersionBC4C8D9Bea90ba6386abe14233bd0d4de0c817bd is ['TestStack', 'ViewerRequestFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource OriginResponseFunctionFnServiceRole7876B1B6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'ServiceRole', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource OriginResponseFunctionFnA3A6B6E6 is ['TestStack', 'OriginResponseFunction', 'Fn', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource OriginResponseFunctionFnCurrentVersion48582B39392ed21bc91ceb4feaf1167d2733083b is ['TestStack', 'OriginResponseFunction', 'Fn', 'CurrentVersion', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource TestCachePolicy65E5B40F is ['TestStack', 'TestCachePolicy', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource TestDistributionOrigin1S3OriginF767E587 is ['TestStack', 'TestDistribution', 'Origin1', 'S3Origin', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource TestDistribution94EC811C is ['TestStack', 'TestDistribution', 'Resource']
2022-07-13 13:48:37,509 | CDK Path for resource CDKMetadata is ['TestStack', 'CDKMetadata', 'Default']
2022-07-13 13:48:37,510 | 10 resources found in the stack 
2022-07-13 13:48:37,510 | Found Lambda function with name='ViewerRequestFunctionFn3C72C1E6' and CodeUri='/Users/sinokuma/Desktop/cdk-bug-test/src/viewer-request-function'
2022-07-13 13:48:37,510 | --base-dir is not presented, adjusting uri /Users/sinokuma/Desktop/cdk-bug-test/src/viewer-request-function relative to /Users/sinokuma/Desktop/cdk-bug-test/cdk.out/TestStack.template.json
2022-07-13 13:48:37,510 | Found Lambda function with name='OriginResponseFunctionFnA3A6B6E6' and CodeUri='/Users/sinokuma/Desktop/cdk-bug-test/src/origin-response-function'
2022-07-13 13:48:37,510 | --base-dir is not presented, adjusting uri /Users/sinokuma/Desktop/cdk-bug-test/src/origin-response-function relative to /Users/sinokuma/Desktop/cdk-bug-test/cdk.out/TestStack.template.json
2022-07-13 13:48:37,721 | ViewerRequestFunctionFn3C72C1E6 not found. Possible options in your template: ['Fn']
2022-07-13 13:48:37,721 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'e78b33f8-3700-4827-8c97-65ff51dc0f6f', 'installationId': '92dc9904-5d64-4da0-9ee9-248620c346f4', 'sessionId': 'c7464fa4-85e6-49bb-8547-373976565d39', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion': '1.53.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local invoke', 'metricSpecificAttributes': {'projectType': 'CDK'}, 'duration': 246, 'exitReason': 'FunctionNotFound', 'exitCode': 1}}]}
2022-07-13 13:48:38,328 | 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

sinokuma avatar Jul 13 '22 04:07 sinokuma

I followed the example in our docs which works just fine and I get the expected template from cdk synth.

The difference here is the resource that is being used. In the example we use lambda.Function, while you use cloudfront.experimental.EdgeFunction.

The problem here is that CDK is not adding the right metadata to the output for "EdgeFunctions" like they do with "LambdaFunctions".

So this is a feature request to CDK to output the right data in the template and enable the interactions between the two tools. You can file an issue with CDK here.

jfuss avatar Jul 13 '22 20:07 jfuss

@sinokuma and @WinterYukky I miss understood the problem here. I thought it related to our resolving of the code not the name. Thanks to @WinterYukky for recognizing this.

I need to do a little more understand on why we don't allow the LogicalId at all (which seems wrong). If memory serves well, I think we made this 'reading the cdk path' in to make it easier for customers to invoke their functions as the logical ids generated by CDK always have a hash in them. In the PR raised, we will now combine to parts of the cdk:path but not sure if that is the right thing here. It is an option but want to make sure I understand what we have now more deeply but going further. I am thinking it may make sense to allow both the logical id and the cdk path name but not sure how we handle this exact case, as I don't think we knew about this when we built out the solution (I know I didn't).

jfuss avatar Jul 18 '22 12:07 jfuss

This still happens on 1.114.0

gastonsilva avatar Apr 05 '24 03:04 gastonsilva

Any update on this matter? What are the alternatives to test locally if AWS SAM is not working?

masterodi avatar Jun 14 '24 08:06 masterodi