AWS Developer Tools Blog

Tips & Tricks: Debugging your C# CDK project in Visual Studio

NOTE: This post assumes the reader has some familiarity with the AWS Cloud Development Kit (CDK). To get started with the AWS CDK, please visit the AWS CDK Developer Guide and follow the AWS CDK for .NET workshop.

The AWS CDK is an an open-source Infrastructure-as-Code (IaC) framework that allows developers to model and provision cloud application resources using the language of their choice. .NET developers can write their IaC project in C# using AWS CDK for .NET and add it to their Solution as a Console Application. This allows developers to leverage their existing skills and familiarity with Visual Studio and the .NET Ecosystem to build cloud infrastructure along side the rest of their application.

We often get questions from customers asking how they can debug their CDK projects directly from Visual Studio. Even though CDK does not support this functionality at this time, with a little judicious use of System.Diagnostics.Debugger, we’ll be debugging in no time! In this post, we will show you how to do this step-by-step.

Debugger.Launch()

To start debugging, add a call to Debugger.Launch() inside your CDK project’s Program Main method.

namespace Cdk
{
    class Program
    {
        public static void Main(string[] args)
        {
            // note: will launch debugger every time.  See below for full example.
            Debugger.Launch();

            var app = new App();
            new TVCampaignStack(app, "TVCampaignStack", new StackProps());
            app.Synth();
        }
     }
 }

When invoked, the debugger will pause execution and pop a dialog box inviting the user to attach Visual Studio.

Attach to Debugger Popup Window

Once attached, you can debug your Infrastructure directly from Visual Studio!

Breakpoint in Visual Studio

Config Driven

While it’s very handy to be able to launch and attach a Visual Studio debugger, we don’t want this to happen every time, especially if deploying from a CI/CD system or other automated tooling.

There is a number of ways to inject configuration into an AWS CDK for .NET project. The most straight forward way is to pass Context in from the command line. We can modify our example from above like this:

public static void Main(string[] args)
{
    var app = new App();
    
    bool.TryParse(app.Node.TryGetContext("debug")?.ToString(), out var debug);

    if (debug)
        Debugger.Launch();

    new TVCampaignStack(app, "TVCampaignStack", new StackProps());
    app.Synth();
}

Now we can control when the debugger will launch via the command line:

# Launches the Debugger
PS C:\Example> cdk deploy -c debug=true

# Does not launch debugger
PS C:\Example> cdk deploy

Summary

With a well placed call to Debugger.Launch() inside our Program’s Main method, debugging your C# CDK Infrastructure inside of Visual Studio becomes easy!

Further Reading

AWS CDK for .NET Workshop: https://cdkworkshop.com/40-dotnet.html
IaC in C# with AWS CDK: https://www.youtube.com/watch?v=5aBf0W0_FDY
CDK Context: https://docs.thinkwithwp.com/cdk/v2/guide/context.html
Alternative Configuration Example: https://github.com/ppittle/cdk-talk-2021/blob/main/CloudAutoGroup/cdk/Program.cs#L41