Open NorthwindCdk.sln
in Visual Studio.
Open NorthwindCdkStack.cs
.
Add following import statement to the NorthwindCdkStack.cs
.
using Amazon.CDK.AWS.SSM;
Please add the following changes at the end of NorthwindCdkStack constructor. The following code creates new parameter in AWS Systems Manager Parameter Store named /Northwind/ConnectionStrings/NorthwindDatabase
and adds full connection string to the SQL Server as it’s value.
namespace NorthwindCdk
{
public class NorthwindCdkStack : Stack
{
internal NorthwindCdkStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
.........
// SQL Server connection string in Systems Manager Parameter Store
new StringParameter(this, "NorthwindDatabaseConnectionString", new StringParameterProps {
ParameterName = "/Northwind/ConnectionStrings/NorthwindDatabase",
Type = ParameterType.STRING,
Description = "SQL Server connection string",
StringValue = string.Format("Server={0},1433;Integrated Security=false;User ID=adminuser;Password=Admin12345?;Initial Catalog=NorthwindTraders;", sql.DbInstanceEndpointAddress)
});
}
}
}
Save changes and compile the project.
Run cdk diff
to take a look at the changes that are going to be deployed.
cdk diff
The output should look like this:
Stack NorthwindCdkStack
Resources
[+] AWS::SSM::Parameter NorthwindDatabaseConnectionString NorthwindDatabaseConnectionString2DB3D9C6
Now it’s time to deploy the project using cdk deploy
command.
cdk deploy
The output should look like the following:
NorthwindCdkStack: deploying...
NorthwindCdkStack: creating CloudFormation changeset...
0/2 | 7:26:59 AM | CREATE_IN_PROGRESS | AWS::SSM::Parameter | NorthwindDatabaseConnectionString (NorthwindDatabaseConnectionString2DB3D9C6)
0/2 | 7:27:01 AM | CREATE_IN_PROGRESS | AWS::SSM::Parameter | NorthwindDatabaseConnectionString (NorthwindDatabaseConnectionString2DB3D9C6) Resource creation Initiated
1/2 | 7:27:02 AM | CREATE_COMPLETE | AWS::SSM::Parameter | NorthwindDatabaseConnectionString (NorthwindDatabaseConnectionString2DB3D9C6)
1/2 | 7:27:04 AM | UPDATE_COMPLETE_CLEA | AWS::CloudFormation::Stack | NorthwindCdkStack
2/2 | 7:27:04 AM | UPDATE_COMPLETE | AWS::CloudFormation::Stack | NorthwindCdkStack
✅ NorthwindCdkStack
Outputs:
NorthwindCdkStack.SQLServerEndpointAddress = northwind-sqlserver.cyhlwzws5aoz.eu-west-1.rds.amazonaws.com
Stack ARN:
arn:aws:cloudformation:eu-west-1:404486542784:stack/NorthwindCdkStack/f78fb1c0-8ed1-11ea-8a83-0a0af0d573f8
You can see the generated CloudFormation template by running cdk synth
command. Please note how C# string.Format
function is transformed into string concatenation using CloudFormation Fn::GetAtt
function.
NorthwindDatabaseConnectionString2DB3D9C6:
Type: AWS::SSM::Parameter
Properties:
Type: String
Value:
Fn::Join:
- ""
- - Server=
- Fn::GetAtt:
- NorthwindSQLServerB034B5AC
- Endpoint.Address
- ",1433;Integrated Security=false;User ID=adminuser;Password=Admin12345?;Initial Catalog=NorthwindTraders;"
Description: SQL Server connection string
Name: /Northwind/ConnectionStrings/NorthwindDatabase
Metadata:
aws:cdk:path: NorthwindCdkStack/NorthwindDatabaseConnectionString/Resource