AWS Systems Manager Parameter Store for Managing Configuration and Retrieve at Runtime using C#
Tags: aws, csharp
Published: 2020-11-28 01:51:15

**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](https://aws.amazon.com) 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](https://dev-to-uploads.s3.amazonaws.com/i/qepg3q02w8svvcphayqg.png)

**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;
                    }
```

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

You can see the implementation in the below Github project
{% github https://github.com/sunilkumarmedium/CleanArchitectureApp %}

Happy Coding!