AWS Developer Tools Blog

AWS CDK for .NET

AWS CDK for .NET

The AWS Cloud Development Kit (AWS CDK) Developer Preview now has support for .NET!

The AWS CDK is a software development framework to define cloud infrastructure as code and provision it through AWS CloudFormation. This provides familiarity, IDE and tools support, and flexibility that are unavailable when using static text definitions. You can read the initial announcement here.

Now that .NET is supported, you can use C# and other CLR-based languages to construct your cloud infrastructure. The libraries target .NET Standard 2.0, so you can use macOS or Linux to write and run your CDK applications.

Why use the CDK?

  • Simplicity

    • Use the CDK constructs to define AWS infrastructure without having to master the full power and complexity of AWS resource configuration.
    • Use the power of your language to create complex infrastructure.
      • Use a loop to assign each Amazon EC2 instance to a subnet.
      • Dynamically look up your container ID in Amazon ECS to reference within your CDK app by making an API call.
  • Familiar tooling

    • Autocomplete and type checking!
    • Develop your infrastructure using Visual Studio, VS Code, and JetBrains Rider.
    • Dependency management using NuGet.
  • Built-in best practices

    • Most restrictive permissions are used by default for communication between resources.
    • Complex constructs give sensible, safe defaults, while still allowing the developer to change everything to fit their use case.
  • Abstraction, reuse, and sharing

    • You can create and publish your own CDK components to define and enable best practices and policies that are specific to your business. The CDK reuses packaging and distribution mechanisms common in each target language’s ecosystem – in the case of .NET, NuGet.
    • As the CDK community grows, expect more “higher-level” constructs to become available. For example, setting up an entire CI/CD pipeline with a single construct.

Getting started

The CDK for .NET is comprised of a collection of NuGet packages. The naming of these packages follows the pattern of “Amazon.CDK.*”. These packages contain the interface layer between the C# code and the toolkit. To use your CDK application, invoke the CDK CLI, a common interface for using the toolkit across CDK language implementations.

In this post we are going to invoke the “cdk init” command, which creates a starter CDK project in the language of your choice.

Prerequisites

  • Node.js
    • The CDK has a dependency on Node. In addition, NPM is required to install the CDK CLI. NPM comes bundled with Node, so install it, and get both the runtime and NPM package manager. If you don’t have it installed, you can get it here.
  • CDK CLI
    • Get the CDK CLI though NPM.
      npm install -g aws-cdk
  • dotnet CLI
    • The .NET init template assumes you have it installed. You can get it here.

Init

Now you are ready to generate the init template to quickly get started. Create an empty directory, and inside it, run:

cdk init app --language dotnet

The sample application will be generated and you will be greeted with instructions on how to run your project.


The template defines a stack that contains an Amazon SQS queue and an Amazon SNS topic. The queue is subscribed to the topic, so whenever a message is sent to the topic, the message is sent to the queue. The code to do this is simply the following:

var queue = new Queue(this, "MyFirstQueue", new QueueProps
{
    VisibilityTimeoutSec = 300
}
var topic = new Topic(this, "MyFirstTopic", new TopicProps
{
    DisplayName = "My First Topic Yeah"
});

The stack also uses a custom construct that creates a variable number of buckets, through which you can grant read permissions on as a collective.

HelloConstruct hello = new HelloConstruct(this, "Buckets", new HelloConstructProps()
{
    BucketCount = 5
});
User user = new User(this, "MyUser", new UserProps());
hello.GrantRead(user);

CDK at work

The general development cycle for CDK apps is to make a change, build your .NET code (dotnet build src), and then run any of the following:

  • “cdk synth” to see the generated template, to make sure the generated source matches your expectations, or to do code reviews.
  • “cdk diff” to compare the changes between your currently deployed stack, and the one generated by your code.
  • “cdk deploy” to deploy your changes to your account.

CDK uses JSII to invoke JavaScript to create AWS CloudFormation templates. Deploy sends those templates to AWS CloudFormation to deploy your stacks. Progress of the deployment is reported to you as it runs.

Notice

The CDK is in public developer preview. Be aware of the following:

Given the status of the preview, we do not currently recommend using AWS CDK in production.

Conclusion

If you want to be on this journey with us, star the AWS CDK and JSII repositories. Experiment with the toolkit, and submit any issues you have to the GitHub trackers. If you have a great idea for a general-purpose, high-level construct, why not make a repository and share it with the world? (Please do this, we would be thrilled!)

Thank you – we are excited to see what you create!