AWS Systems Manager Parameter Store for Managing Configuration and Retrieve at Runtime using C#

sunilkumarmedium

sunilkumarmedium

Posted on November 28, 2020

AWS Systems Manager Parameter Store for Managing Configuration and Retrieve at Runtime using C#

Introduction

One common challenge in application development is ensuring that any configuration data liable to change across deployments is separated from the code, allowing a single release to be configured for multiple environments.

AWS's solution for storing configuration data is called AWS Systems Manager Parameter Store.

Parameter Store provides a mechanism to store and manage configuration data, encrypted or plain text, using a hierarchical structure.

Parameter Store is ideal for storing passwords, database strings, and all other types of general configuration values.

Pre-requisites
To complete this learning, you will need:
✓ An AWS Account
✓ An IAM user with access key credentials
✓ Visual Studio Code or Visual Studio 2019+ for Windows

If you don't have an account visit AWS and click Sign Up.

You must have a set of valid AWS credentials, consisting of an access key and a secret key, which are used to sign programmatic requests to AWS. You can obtain a set of account credentials when you create your account, although we recommend you do not use these credentials and instead create an IAM user and use those credentials.

Installing the AWS CLI:

Install the AWS CLI for Windows, Mac, or Linux: https://aws.amazon.com/cli/

Once installed, you can configure the CLI by running the aws configure command in a terminal or command-line window.

When prompted, enter your AWS Access Key ID and press Enter.

Enter your AWS Secret Access Key when prompted and then press Enter.

For the default region name you should enter your chosen region code (e.g. ap-south-1)

Finally, for the default output format you can just press Enter.

Creating Parameter Store data using AWS CLI

To create an entry in the Parameter Store, execute the following command in the terminal or command-line window:

aws ssm put-parameter --name "/CleanArchitectureAppWebApi/postgresconnection" --type String --value "ConnectionString"

Adding Secure String

aws ssm put-parameter --name "secure-parameter-name" --type "SecureString" --value "secure-parameter-value"

The above command will create the parameter in the region you specified as your default profile configured.
To create a parameter in a different region, add the --region parameter, for example --region ap-south-1.

We can retrieve the parameter created by running the following command in the terminal or command-line window:

aws ssm get-parameter --name "/CleanArchitectureAppWebApi/postgresconnection"

To delete data from Parameter Store execute the following command in a terminal or command-line window:

aws ssm delete-parameter --name "/CleanArchitectureAppWebApi/postgresconnection"

Creating Parameter Store data using AWS Console

  1. Log-in to the AWS Management Console
  2. search for 'Systems Manager' and Click on Create Parameter Alt Text

Retrieving the parameter using C#

Now that you have created a configuration value, it's time to create a basic .NET C# application that can retrieve the data at runtime.

  1. Create C# console or Web application
  2. Reference the AWS SDK SimpleSystemsManager Package using nuget package console <PackageReference Include="AWSSDK.SimpleSystemsManagement" Version="3.5.5.6" />
  3. Add the below Assembly references using Amazon.SimpleSystemsManagement; using Amazon.SimpleSystemsManagement.Model;
var request = new GetParameterRequest()
{
     Name = "/CleanArchitectureAppWebApi/postgresconnection"
                    };

                    using (var client = new AmazonSimpleSystemsManagementClient(Amazon.RegionEndpoint.GetBySystemName("ap-south-1")))
                    {
                        var response =  client.GetParameterAsync(request).GetAwaiter().GetResult();
                        connectionString = response.Parameter.Value;
                    }
Enter fullscreen mode Exit fullscreen mode

The Name and the Region can be read from the appsettings.json configuration.

You can see the implementation in the below Github project

GitHub logo sunilkumarmedium / CleanArchitectureApp

Clean Architecture Application Design from Scratch using Dotnet Core 5 WebApi and Angular 11 FrontEnd

CleanArchitectureApp

Clean Architecture Application Design from Scratch using Dotnet Core 5 WebApi and Angular 11 FrontEnd

MIT license

Technologies

Pre-requisites

  1. .Net core 5 SDK
  2. Visual studio 2019 OR VSCode with C# extension
  3. NodeJs (Latest LTS)
  4. Microsoft SQL Server (Optional: If MS SQL server required instead of Sqlite during development)
  5. POSTGRESQL

Configuration

  1. Clone the repo: git clone https://github.com/sunilkumarmedium/CleanArchitectureApp.git
  2. Execute the sql scripts available in the folder /sql/
    • MSSQL use CleanArchitectureDB.sql
    • POSTGRES use CleanArchitectureDB-Postgres
  3. Change the database connectionstring in appsettings.json
    • Path : CleanArchitectureApp.WebApi/appsettings.Development.json or appsettings.json
    • "DBProvider": "MSSQL" , Use MSSQL to connect to Microsoft SqlServer Or POSTGRES to connect to PostgreSQL…

Happy Coding!

💖 💪 🙅 🚩
sunilkumarmedium
sunilkumarmedium

Posted on November 28, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related