AWS Systems Manager Parameter Store for Managing Configuration and Retrieve at Runtime using C#
sunilkumarmedium
Posted on November 28, 2020
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.
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:
search for 'Systems Manager' and Click on Create Parameter
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.
Create C# console or Web application
Reference the AWS SDK SimpleSystemsManager Package using nuget package console
<PackageReference Include="AWSSDK.SimpleSystemsManagement" Version="3.5.5.6" />
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;
}
The Name and the Region can be read from the appsettings.json configuration.
You can see the implementation in the below Github project