Deploy a C# function with C#
When you want to deploy your application to the AWS cloud, there are several options available. You can use Cloudformation, Terraform and since a couple of years there is also the AWS CDK.
I haven’t played around with the CDK and after listening to a podcast episode of The Burning Monk I decided it was time to take a look.
The CDK is structured into several Stack which are 1 on 1 linked to a Cloudformation stack. Yes, in the backend Cloudformation is still used to deploy your stack.
AWS provides you with a lot of information about the CDK and you should get started with this information.
Commands
Most important commands, (till now)
be aware to execute these commands on the root of your CDK folder, where your cdk.json
resides.
Create an empty CDK project using C#
cdk init app --language csharp
As you are not authorized by default to deploy any application into your AWS account, you need to bootstrap. Bootstrapping will generate all the resources which are required for the CDK to deploy your applications. Think about IAM Roles, S3 bucket, …For More Information see aws bootstrap
Deploy your application using the CDK
cdk deploy
Create a Lambda Function with the CDK
In the code below we will create a C# Lambda function which is integrated with API Gateway which returns a static response for now.
Create the lambda function
If you haven’t installed the AWS Lambda Powertools, go ahead and install them
dotnet new -i "Amazon.Lambda.Templates::*"
A new Lambda function can now be created using the empty function template, use dotnet new list lambda
to see a list of available lambda templates.
dotnet new lambda.EmptyFunction
Create the infrastructure
We start by creating a new project using the CDK,
cdk init app --language csharp
in an new empty directory ( you will notice that the CDK doesn’t like you to use an existing dir)
In the stack