aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.5k stars 1.17k forks source link

Bug: parsing Ref in template.json is incorrect #7413

Open axot opened 2 weeks ago

axot commented 2 weeks ago

Description:

sam can't parse the Ref key in the following Lambda definition. We expected TABLE_NAME to be the actual DynamoDB table name, but instead got demogametable2FE33C07.

  "GameHandler295CCC0B": {
   "Type": "AWS::Lambda::Function",
   "Properties": {
    "Code": {
     "S3Bucket": {
      "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-ap-northeast-1"
     },
     "S3Key": "2160ee90758ba745c338833566ade881a8ad5ff3693dcd6d9347e5fdb41b5da4.zip"
    },
    "Environment": {
     "Variables": {
      "TABLE_NAME": {
       "Ref": "demogametable2FE33C07"
      }
     }
    },

Steps to reproduce:

cdk python code as following

        # Create the Lambda function to receive the request
        api_hanlder = lambda_.Function(
            self,
            "GameHandler",
            function_name="game_handler",
            runtime=lambda_.Runtime.PYTHON_3_12,
            code=lambda_.Code.from_asset("src/game"),
            handler="index.handler",
            vpc=vpc,
            vpc_subnets=ec2.SubnetSelection(
                subnet_type=ec2.SubnetType.PRIVATE_ISOLATED
            ),
            memory_size=1024,
            timeout=Duration.minutes(5),
        )

        # grant permission to lambda to write to demo table
        demo_table.grant_write_data(api_hanlder)
        api_hanlder.add_environment("TABLE_NAME", demo_table.table_name)

Run local test

cdk synth --no-staging
sam local invoke GameHandler --no-event -t ./cdk.out/GameStack.template.json -e tests/game/event.json

event.json

{
    "body": "{\"year\":\"2023\",\"title\":\"kkkg\",\"id\":\"12\"}"
}

Observed result:

demogametable2FE33C07

Expected result:

real dynamodb table name

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Mac 14.6.1
  2. sam --version: SAM CLI, version 1.123.0
  3. AWS region: ap-northeast-1
{
  "version": "1.123.0",
  "system": {
    "python": "3.12.5",
    "os": "macOS-14.6.1-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.9",
    "aws_cdk": "2.154.1 (build febce9d)",
    "terraform": "1.5.7"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}
lucashuy commented 2 weeks ago

Hi, this is a current limitation of the intrinsic support within SAM CLI/SAM local capabilities. When deployed, the intrinsic should be resolved correctly. Unfortunately, I do not have a workaround for this. Please let us know if you come across any other issues.