Create test project

Create new empty folder:

mkdir cdk-lab
cd cdk-lab

You create a new AWS CDK project by invoking cdk init in an empty directory. cdk init uses the name of the project folder to name various elements of the project, including classes, subfolders, and files. The resulting project includes a reference to the Amazon.CDK NuGet package. It and its dependencies are installed automatically by NuGet.

Run cdk init from within that folder with following parameters:

  • sample-app is example CDK Application template with some constructs
  • --language csharp specifies that you want to use C# language
cdk init sample-app --language csharp

You will see the output like this (please note that project name CdkLab is based on the name of folder you created):

Applying project template sample-app for csharp
Project `CdkLab/CdkLab.csproj` added to the solution.
Initializing a new git repository...
# Welcome to your CDK C# project!

You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (`CdkLabStack`)
which contains an Amazon SQS queue that is subscribed to an Amazon SNS topic.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

It uses the [.NET Core CLI](https://docs.microsoft.com/dotnet/articles/core/) to compile and execute your project.

## Useful commands

* `dotnet build src` compile this app
* `cdk ls`           list all stacks in the app
* `cdk synth`       emits the synthesized CloudFormation template
* `cdk deploy`      deploy this stack to your default AWS account/region
* `cdk diff`        compare deployed stack with current state
* `cdk docs`        open CDK documentation

Enjoy!

Open newly generated CdkLab.sln in Visual Studio.

CDK project

Open Program.cs. It instantiates the CdkLabStack class from the CdkLabStack.cs file.

using Amazon.CDK;

namespace CdkLab
{
    sealed class Program
    {
        public static void Main(string[] args)
        {
            var app = new App();
            new CdkLabStack(app, "CdkLabStack");

            app.Synth();
        }
    }
}

Open CdkLabStack.cs.

using Amazon.CDK;
using Amazon.CDK.AWS.SNS;
using Amazon.CDK.AWS.SNS.Subscriptions;
using Amazon.CDK.AWS.SQS;

namespace CdkLab
{
    public class CdkLabStack : Stack
    {
        internal CdkLabStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
             // The CDK includes built-in constructs for most resource types, such as Queues and Topics.
            var queue = new Queue(this, "CdkLabQueue", new QueueProps
            {
                VisibilityTimeout = Duration.Seconds(300)
            });

            var topic = new Topic(this, "CdkLabTopic");

            topic.AddSubscription(new SqsSubscription(queue));
        }
    }
}

The sample-app template has created the following stack for you.

The stack includes:

  • Amazon SQS Queue (new Queue)
  • Amazon SNS Topic (new Topic)
  • Subscribing the queue to receive any messages published to the topic (topic.AddSubscription)