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-dynamodb: misreporting deployed table name when testing lambda function locally with sam #28824

Closed
johnjohntheleprechaun opened this issue Jan 23, 2024 · 6 comments
Assignees
Labels
@aws-cdk/aws-dynamodb Related to Amazon DynamoDB @aws-cdk/aws-sam Related to AWS Serverless Application Model cause/not-a-bug Not a bug (might still be a documentation issue, might still need work) effort/medium Medium work item – several days of effort p2

Comments

@johnjohntheleprechaun
Copy link

Describe the bug

When testing locally with SAM a TableV2's name property, when put into a NodeJsFunction's environment, is set incorrectly. The name matches the name in the synthesized template.json file, but does not match the actual deployed name of the table.

It is set correctly in the environment of the deployed lambda, but it hurts development speed to have to deploy before testing a lambda function

Expected Behavior

The name property of the TableV2 object should return the deployed name of the table, even when testing locally.

Current Behavior

environment variables are only set properly in the deployed lambda functions,

Reproduction Steps

run cdk init app --language typescript
add the @types/aws-lambda package

lib/test.ts:

import { APIGatewayProxyHandler } from "aws-lambda";
export const handler: APIGatewayProxyHandler = async () => {
    console.log("table name:", process.env.DYNAMO_TABLE);
    console.log("table ARN:", process.env.DYNAMO_ARN);
    return {
        statusCode: 200,
        body: JSON.stringify({
            tableName: process.env.DYNAMO_TABLE,
            tableARN: process.env.DYNAMO_ARN
        })
    }
}

constructor of the stack class in lib/(dirname}-stack.ts:

constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    // Define basic resources
    const api = new RestApi(this, "api");
    const database = new TableV2(this, "db", {
        partitionKey: { name: "partitionkeynameisnotimportant", type: cdk.aws_dynamodb.AttributeType.STRING }
    });

    const testFunction = new NodejsFunction(this, "test-function", {
        entry: path.join(__dirname, "test.ts"),
        runtime: Runtime.NODEJS_20_X, // doubtfully important, but this is the runtime/architecture I'm using in my project
        architecture: Architecture.ARM_64,
        environment: { "DYNAMO_TABLE": database.tableName, "DYNAMO_ARN": database.tableArn }
    });
    const integration = new LambdaIntegration(testFunction);

    api.root.addMethod("GET", integration);
}

run cdk deploy

run sam local invoke -t cdk.out/{stack-name}.template.json to get the output:

Invoking index.handler (nodejs20.x)                                                                                                          
Local image is up-to-date                                                                                                                    
Using local image: public.ecr.aws/lambda/nodejs:20-rapid-arm64.                                                                              
                                                                                                                                             
Mounting {project directory}/cdk.out/asset.be57583eb97bf91ca66ff97141910daaa72e0095fd15838c63eef26677ae12cb as                       
/var/task:ro,delegated, inside runtime container                                                                                             
START RequestId: c2fa8058-7e7c-4e0d-bf1b-ce0254e45e5a Version: $LATEST
2024-01-23T05:18:11.166Z        757c472b-0726-46be-b9a0-edfd0c3d331b    INFO    table name: dbECC37780
2024-01-23T05:18:11.198Z        757c472b-0726-46be-b9a0-edfd0c3d331b    INFO    table ARN: arn:aws:lambda:us-east-1:123456789012:function:dbECC37780
END RequestId: 757c472b-0726-46be-b9a0-edfd0c3d331b
REPORT RequestId: 757c472b-0726-46be-b9a0-edfd0c3d331b  Init Duration: 1.33 ms  Duration: 3478.11 ms    Billed Duration: 3479 ms        Memory Size: 128 MB  Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"tableName\":\"dbECC37780\",\"tableARN\":\"arn:aws:lambda:us-east-1:123456789012:function:dbECC37780\"}"}

Querying the deployed API endpoint gives (which is the correct info):

{"tableName":"CdkBugStack-dbECC37780-1PA814OISEA9U","tableARN":"arn:aws:dynamodb:us-west-2:{account-id}:table/CdkBugStack-dbECC37780-1PA814OISEA9U"}

Possible Solution

No response

Additional Information/Context

sam --version: SAM CLI, version 1.107.0

I'm using nvm to set my node version, the default installed version is v21.6.0 but all commands were run after nvm use 20, so unless there's something wrong with nvm it shouldn't matter

CDK CLI Version

2.122.0 (build 7e77e02)

Framework Version

No response

Node.js Version

v20.11.0

OS

Ubuntu 22.04.3 LTS

Language

TypeScript

Language Version

TypeScript 5.3.3

Other information

No response

@johnjohntheleprechaun johnjohntheleprechaun added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 23, 2024
@github-actions github-actions bot added the @aws-cdk/aws-dynamodb Related to Amazon DynamoDB label Jan 23, 2024
@pahud
Copy link
Contributor

pahud commented Jan 24, 2024

Looks like it only happens on SAM running localhost but actually it's correct when you deploy?

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 24, 2024
@johnjohntheleprechaun
Copy link
Author

Looks like it only happens on SAM running localhost but actually it's correct when you deploy?

Yep.

@GavinZZ GavinZZ self-assigned this Jan 25, 2024
@GavinZZ
Copy link
Contributor

GavinZZ commented Jan 25, 2024

I can reproduce this issue and can confirm that it only happens on sam local invoke. Tested by invoking the deployed lambda on AWS console and the output dyanmodb table name and arn are correct.

@GavinZZ
Copy link
Contributor

GavinZZ commented Jan 26, 2024

With a bit more investigation, I don't believe this issue is related to CDK. Going to close this issue. Feel free to cut an issue to aws-sam-cli repo if you would like more information.

Here is what I tried and came to conclusion that this is not related to CDK. I first followed your reproduction step and created a CDK app. I then used the synthesized template (removed all CDK related metadata and fields) using AWS SAM CLI. I built and deploy the YAML template using AWS SAM CLI. At this point, the template is basically a SAM template and has nothing to do with CDK. I am able to successfully deploy the template.

I can then reproduce the same issue as described where running sam local invoke shows

Mounting /Users/yuanhaoz/Desktop/Repo/SAMPlayGround/hello_world_function/hello_world as /var/task:ro,delegated, inside runtime container                                 
START RequestId: 89e4716f-423a-45f3-b9f9-508fffebb1a8 Version: $LATEST
2024-01-26T00:02:13.347Z        89e4716f-423a-45f3-b9f9-508fffebb1a8    INFO    table name: dbECC37780
2024-01-26T00:02:13.348Z        89e4716f-423a-45f3-b9f9-508fffebb1a8    INFO    table ARN: arn:aws:lambda:us-east-1:123456789012:function:dbECC37780
END RequestId: 89e4716f-423a-45f3-b9f9-508fffebb1a8
REPORT RequestId: 89e4716f-423a-45f3-b9f9-508fffebb1a8  Init Duration: 0.38 ms  Duration: 333.69 ms     Billed Duration: 334 ms Memory Size: 128 MB     Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"tableName\":\"dbECC37780\",\"tableARN\":\"arn:aws:lambda:us-east-1:123456789012:function:dbECC37780\"}", "headers": {"Content-Type": "application/json"}}

While if I invoke the function from AWS Console, it gives the correct table name and arn.

Appendix

Here's the YAML template file I used

Transform: AWS::Serverless-2016-10-31
Resources:
  apiC8550315:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: api
  apiDeployment149F129466d1e239c6512446741af1a84319eda5:
    Type: AWS::ApiGateway::Deployment
    Properties:
      Description: Automatically created by the RestApi construct
      RestApiId: !Ref 'apiC8550315'
    DependsOn:
      - apiGETECF0BD67
  apiDeploymentStageprod896C8101:
    Type: AWS::ApiGateway::Stage
    Properties:
      DeploymentId: !Ref 'apiDeployment149F129466d1e239c6512446741af1a84319eda5'
      RestApiId: !Ref 'apiC8550315'
      StageName: prod
  apiGETApiPermissionCdkInitTemplatesStackapiCF9EF0FEGETD35164E6:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt 'testfunction5B23D3B0.Arn'
      Principal: apigateway.amazonaws.com
      SourceArn: !Join
        - ''
        - - 'arn:'
          - !Ref 'AWS::Partition'
          - ':execute-api:'
          - !Ref 'AWS::Region'
          - ':'
          - !Ref 'AWS::AccountId'
          - ':'
          - !Ref 'apiC8550315'
          - /
          - !Ref 'apiDeploymentStageprod896C8101'
          - /GET/
  apiGETApiPermissionTestCdkInitTemplatesStackapiCF9EF0FEGETC92B545C:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt 'testfunction5B23D3B0.Arn'
      Principal: apigateway.amazonaws.com
      SourceArn: !Join
        - ''
        - - 'arn:'
          - !Ref 'AWS::Partition'
          - ':execute-api:'
          - !Ref 'AWS::Region'
          - ':'
          - !Ref 'AWS::AccountId'
          - ':'
          - !Ref 'apiC8550315'
          - /test-invoke-stage/GET/
  apiGETECF0BD67:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizationType: NONE
      HttpMethod: GET
      Integration:
        IntegrationHttpMethod: POST
        Type: AWS_PROXY
        Uri: !Join
          - ''
          - - 'arn:'
            - !Ref 'AWS::Partition'
            - ':apigateway:'
            - !Ref 'AWS::Region'
            - :lambda:path/2015-03-31/functions/
            - !GetAtt 'testfunction5B23D3B0.Arn'
            - /invocations
      ResourceId: !GetAtt 'apiC8550315.RootResourceId'
      RestApiId: !Ref 'apiC8550315'
  dbECC37780:
    Type: AWS::DynamoDB::GlobalTable
    Properties:
      AttributeDefinitions:
        - AttributeName: partitionkeynameisnotimportant
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      KeySchema:
        - AttributeName: partitionkeynameisnotimportant
          KeyType: HASH
      Replicas:
        - Region: !Ref 'AWS::Region'
    UpdateReplacePolicy: Retain
    DeletionPolicy: Retain
  testfunctionServiceRoleFB85AD63:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
        Version: '2012-10-17'
      ManagedPolicyArns:
        - !Join
          - ''
          - - 'arn:'
            - !Ref 'AWS::Partition'
            - :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
  testfunction5B23D3B0:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.handler
      Runtime: nodejs16.x
      CodeUri: hello_world_function/hello_world/
      Environment:
        Variables:
          DYNAMO_TABLE: !Ref 'dbECC37780'
          DYNAMO_ARN: !GetAtt 'dbECC37780.Arn'
          AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1'
      Role: !GetAtt 'testfunctionServiceRoleFB85AD63.Arn'
    DependsOn:
      - testfunctionServiceRoleFB85AD63

@GavinZZ GavinZZ added cause/not-a-bug Not a bug (might still be a documentation issue, might still need work) @aws-cdk/aws-sam Related to AWS Serverless Application Model and removed bug This issue is a bug. labels Jan 26, 2024
@GavinZZ
Copy link
Contributor

GavinZZ commented Jan 26, 2024

Closing this issue as this is unrelated to CDK.

@GavinZZ GavinZZ closed this as completed Jan 26, 2024
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-dynamodb Related to Amazon DynamoDB @aws-cdk/aws-sam Related to AWS Serverless Application Model cause/not-a-bug Not a bug (might still be a documentation issue, might still need work) effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

3 participants