Create parameter

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.

Please note the name of the parameter: /Northwind/ConnectionStrings/NorthwindDatabase.

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/3 | 5:16:25 AM | UPDATE_IN_PROGRESS   | AWS::CloudFormation::Stack            | NorthwindCdkStack User Initiated
 2/3 | 5:16:30 AM | CREATE_IN_PROGRESS   | AWS::SSM::Parameter                   | NorthwindDatabaseConnectionString (NorthwindDatabaseConnectionString2DB3D9C6)
 2/3 | 5:16:30 AM | UPDATE_IN_PROGRESS   | AWS::CDK::Metadata                    | CDKMetadata/Default (CDKMetadata)
 2/3 | 5:16:32 AM | UPDATE_COMPLETE      | AWS::CDK::Metadata                    | CDKMetadata/Default (CDKMetadata)
 2/3 | 5:16:32 AM | CREATE_IN_PROGRESS   | AWS::SSM::Parameter                   | NorthwindDatabaseConnectionString (NorthwindDatabaseConnectionString2DB3D9C6) Resource creation Initiated
 2/3 | 5:16:33 AM | CREATE_COMPLETE      | AWS::SSM::Parameter                   | NorthwindDatabaseConnectionString (NorthwindDatabaseConnectionString2DB3D9C6)
 3/3 | 5:16:35 AM | UPDATE_COMPLETE_CLEA | AWS::CloudFormation::Stack            | NorthwindCdkStack
 3/3 | 5:16:36 AM | UPDATE_COMPLETE      | AWS::CloudFormation::Stack            | NorthwindCdkStack

 ✅  NorthwindCdkStack

Outputs:
NorthwindCdkStack.SQLServerEndpointAddress = northwind-sqlserver.cn0kwmvargy2.eu-west-1.rds.amazonaws.com

Stack ARN:
arn:aws:cloudformation:eu-west-1:672916551513:stack/NorthwindCdkStack/962b7760-9070-11eb-9654-0a6ae1197ba5

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