
In this post, we will understand, how to easily get started with CDK in .NET.
CDK stands for Cloud Development Kit. CDK allows developers to provision the resources in AWS cloud programmatically. CDK supports a variety of programming languages including C#, Java, Python, Go, JavaScript & TypeScript.
Step 1: Install CDK CLI
Run below command to install CDK CLI using NPM.
npm install -g aws-cdk
Step 2: Create .NET Core CDK App using CDK CLI
In this step, you will create a CDK App. An App
in the CDK represents the entry point for the entire CDK application. The CDK App is composed of one or more Stacks
(same as a CloudFormation stack).
The unit of deployment in the AWS CDK is called a stack
. All AWS resources defined in a stack, are provisioned as a single unit.
Run below commands to create new directory and then change your working directory to new one.
mkdir cdk-sample-app
cd cdk-sample-app
Now, run below command to quickly generate basic template to get started. This basic template includes .NET solution with one project.
cdk init app --language csharp
This is how the directory structure look like.
Inside, src folder.
Once you open the solution, you can see the Program.cs
file. This contains a CDK App along with one CDK Stack.
Step 3: Install NuGet packages
Install following NuGet packages to create an S3 bucket and DynamoDB table using CDK.
Install-Package Amazon.CDK
Install-Package Amazon.CDK.AWS.S3
Install-Package Amazon.CDK.AWS.DynamoDB
Amazon.CDK
is the base package, that must be installed whenever you are working with CDK.
Step 4: Modify CDK Stack - Create S3 Bucket & DynamoDB table using CDK
Go to Program.cs
and replace existing code with below:
public static void Main(string[] args)
{
var app = new App();
new CdkSampleAppStack(app, "CdkSampleAppStack");
app.Synth();
}
Next, go to CdkSampleAppStack.cs
file, and replace the code there as per below.
public class CdkSampleAppStack : Stack
{
internal CdkSampleAppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
// Create S3 bucket
new Bucket(this, "MyFirstBucket", new BucketProps
{
Versioned = true
});
// Create DynamoDB Table
new Table(this, "MyFirstDynamoDBTable", new TableProps
{
PartitionKey = new Attribute { Name = "PK", Type = AttributeType.STRING },
SortKey = new Attribute { Name = "SK", Type = AttributeType.STRING },
BillingMode = BillingMode.PAY_PER_REQUEST
});
}
}
Step 5: Build the application
Run following command to build this application.
cd src
dotnet build
Step 6: Bootstrapping AWS environment for CDK deployment
You need to provision few AWS resources in advance in order to deploy a CDK app in an environment. Those resources includes:
- Amazon S3 bucket for storing CloudFormation files
- IAM roles that grant permissions needed to perform deployments
The process of provisioning these initial resources is called bootstrapping.
Bootstrapping is required only once per environment, where environment is a combination of target AWS account & region into which the stack is intended to be deployed..
To bootstrap your AWS environment for CDK deployment, run below command.
cdk bootstrap
Running above command will create necessary resources in AWS. See below screenshot.
Here is another screenshot of the CDK Stack from AWS Console.
Step 5: Deploy the CDK stack
Run below command to go to the root folder, as you should be on src
folder due to previous step.
cd..
Next, run the following command to deploy the CDK stack.
cdk deploy
Running the above command will deploy the entire stack on AWS.
The same, we can verify from AWS Console. See below screenshot.
To delete all the resources created by this Stack, just press Delete button shown in the above picture.
Useful commands
dotnet build src
compile this app.cdk deploy
deploy this stack to your default AWS account/region.cdk diff
to compare the changes between your currently deployed stack, and the one generated by your code.cdk init
creates a new CDK project in the current directory from a specified template.cdk synth
command executes your app, which causes the resources defined in it to be translated into an AWS CloudFormation template.
Running cdk synth
command will generate JSON templates in cdk.out directory as shown below.
Conclusion
In this post, you learned how easily can you deploy resources programmatically using AWS CDK. Please let me know your thoughts and feedback in the comment section below.
Thank You ❤️