Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging lambda locally using cdk not sam

AWS CDK provides great features for developers. Using CDK deveolper can manage not only total infrastructure but also security, codepipeline, ...

However I recently struggling something. I used to debug lambda using SAM for local debugging. I know how to set up CDK environment, and debug CDK application itself. But I can't figure out how to debug lambda application inside CDK.

Can anyone help me?

like image 952
Eddy Avatar asked Oct 16 '25 06:10

Eddy


2 Answers

As of 4/29/2021, there's an additional option for debugging CDK apps via SAM. It's in preview but this blog post covers it : https://aws.amazon.com/blogs/compute/better-together-aws-sam-and-aws-cdk/.

Basically, install the AWS CLI and AWS CDK. The install the SAM CLI - beta, available here : https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-cdk-getting-started.html.

Then you can run command's like:

sam-beta-cdk build sam-beta-cdk local invoke sam-beta-cdk local invoke start-api and even emulate the Lambda service with sam-beta-cdk local start-lambda

like image 106
Mbuotidem Isaac Avatar answered Oct 19 '25 00:10

Mbuotidem Isaac


You can use SAM and CDK together as described here. In particular:

  1. Run your AWS CDK app and create a AWS CloudFormation template

    cdk synth --no-staging > template.yaml

  2. Find the logical ID for your Lambda function in template.yaml. It will look like MyFunction12345678, where 12345678 represents an 8-character unique ID that the AWS CDK generates for all resources. The line right after it should look like: Type: AWS::Lambda::Function

  3. Run the function by executing:

    sam local invoke MyFunction12345678 --no-event

like image 33
Nick Cox Avatar answered Oct 18 '25 23:10

Nick Cox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!