Load AWS SSM Parameter Store values into .NET configuration

How do I load AWS SSM Parameter Store values into .NET configuration (IConfiguration)? Call AddSystemsManager("/my-app/dev/") from the Amazon.Extensions.Configuration.SystemsManager package to load every parameter under a path prefix, or list each variable and its parameter name in envilder.json and call AddEnvilder from the Envilder NuGet package. Both put the values in IConfiguration, so you read them as config["Database:Password"]. This guide shows both and when to pick each.

Create the parameters

Both approaches read the same two parameters. Create them with the AWS CLI in the region your app will use (the values are test values; Api/Token is a plain String and Database/Password a SecureString):

aws ssm put-parameter --name /my-app/dev/Database/Password --type SecureString --value local-db-password-1234
aws ssm put-parameter --name /my-app/dev/Api/Token --type String --value local-api-token-5678

The names are chosen so that both approaches produce the same configuration keys: /my-app/dev/Database/Password becomes Database:Password either way, so the code that reads the values does not change when you switch.

Option 1: the AWS provider

Amazon.Extensions.Configuration.SystemsManager is the Systems Manager provider that AWS publishes in the aws/aws-dotnet-extensions-configuration repository. It also covers AppConfig. The snippets in this section compile against version 7.1.1, which targets netstandard2.0 and net8.0.

dotnet add package Amazon.Extensions.Configuration.SystemsManager

Pass the path prefix to AddSystemsManager. The provider loads every parameter below it:

Program.cscsharp
using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder()
    // Loads every parameter under the prefix and drops the prefix from the keys
    .AddSystemsManager("/my-app/dev/")
    .Build();

foreach (var key in new[] { "Database:Password", "Api:Token" })
{
    Console.WriteLine($"{key}: {(string.IsNullOrEmpty(config[key]) ? "missing" : "set")}");
}

The prefix is stripped from the parameter name and each / becomes a : in the configuration key:

Parameter nameConfiguration key
/my-app/dev/Database/PasswordDatabase:Password
/my-app/dev/Api/TokenApi:Token

Because the keys do not contain the prefix, moving from /my-app/dev/ to /my-app/prod/ changes one string and nothing else.

Other settings go through other overloads of AddSystemsManager. This block sets the AWS profile and region in code and reloads every five minutes, then loads only a list of names:

Program.cscsharp
using Amazon;
using Amazon.Extensions.NETCore.Setup;
using Microsoft.Extensions.Configuration;

var awsOptions = new AWSOptions
{
    Profile = "dev",
    Region = RegionEndpoint.EUWest1,
};

var config = new ConfigurationBuilder()
    // Profile and region in code, reload every five minutes
    .AddSystemsManager("/my-app/dev/", awsOptions, reloadAfter: TimeSpan.FromMinutes(5))
    .Build();

var namedConfig = new ConfigurationBuilder()
    // Only these parameters, read with GetParameters instead of GetParametersByPath
    .AddSystemsManager("/my-app/dev/", new[] { "Database/Password" })
    .Build();
  • reloadAfter is a TimeSpan after which the provider reloads in a background task. Without it the provider does not reload.
  • The overload that takes a list of names calls GetParameters, in batches of ten, instead of GetParametersByPath. The README says this can reduce throttling.
  • The AWS client settings can also come from AWSOptions in the AWS section of the configuration (Profile, Region), as the README describes.
  • The options object, SystemsManagerConfigurationSource, also has an Optional flag and an OnLoadException callback. They decide what a failed load does, so read the repository before you rely on them.

The README grants ssm:GetParametersByPath on the parameter ARN of the prefix followed by *. The provider requests values with decryption, so a SecureString also needs kms:Decrypt on its key:

policy.jsonjson
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "ssm:GetParametersByPath",
      "Resource": "arn:aws:ssm:eu-west-1:123456789012:parameter/my-app/dev/*"
    },
    {
      "Effect": "Allow",
      "Action": "kms:Decrypt",
      "Resource": "arn:aws:kms:eu-west-1:123456789012:key/<key-id>"
    }
  ]
}
  • If you load by name with the overload above, the call is GetParameters, which the README policy example does not list. Grant ssm:GetParameters on the same ARN for that case.
  • Adjust the region, the account ID and the key ID to your own.

Option 2: the Envilder .NET SDK

Envilder is a NuGet package that reads a JSON map file and exposes the values as IConfiguration keys. It targets netstandard2.0, so it runs on any in-support .NET release.

dotnet add package Envilder

List each variable with the full name of its parameter. A / in a key becomes a : in the configuration key, and the provider is aws unless the file says otherwise:

envilder.jsonjson
{
  "$schema": "https://envilder.com/schema/map-file.v1.json",
  "$config": {
    "provider": "aws"
  },
  "Database/Password": "/my-app/dev/Database/Password",
  "Api/Token": "/my-app/dev/Api/Token"
}

Then add the map file to the configuration builder:

Program.cscsharp
using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder()
    // Loads the variables listed in envilder.json
    .AddEnvilder("envilder.json")
    .Build();

foreach (var key in new[] { "Database:Password", "Api:Token" })
{
    Console.WriteLine($"{key}: {(string.IsNullOrEmpty(config[key]) ? "missing" : "set")}");
}

How it behaves

  • The map file is read when you call AddEnvilder: a missing file throws FileNotFoundException and a key with characters outside letters, digits, _ . - and / throws FormatException.
  • Values are read once, when the configuration is built, with one GetParameter call per variable and decryption on, so SecureString values come back in plain text. There is no reload.
  • A parameter that does not exist is skipped without an error. The key is absent from IConfiguration, and an appsettings value for the same key stays in effect, so check the keys you depend on.
  • Any other failure stops the build with an exception: an unreachable endpoint, access denied, an expired SSO session (SsoSessionExpiredException) or expired credentials (ExpiredCredentialsException).
  • The same envilder.json works with the Envilder CLI, with envilder exec and with the GitHub Action, so scripts and pipelines read the same list of variables.

The map file picks the provider. With "provider": "azure" and a "vaultUrl" in $config, the same C# reads Azure Key Vault secrets and authenticates with DefaultAzureCredential. "profile" selects an AWS named profile. A vaultUrl with the AWS provider, or a profile with Azure, throws InvalidOperationException.

Set AWS_REGION yourself rather than relying on a default. With a profile, Envilder uses the profile region first, then AWS_REGION or AWS_DEFAULT_REGION, and ends at us-east-1 when it finds none of them.

Envilder calls ssm:GetParameter with decryption once per mapped parameter, so it needs the policy below, the same one as in the SSM to .env guide:

policy.jsonjson
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "ssm:GetParameter",
      "Resource": "arn:aws:ssm:eu-west-1:123456789012:parameter/my-app/dev/*"
    },
    {
      "Effect": "Allow",
      "Action": "kms:Decrypt",
      "Resource": "arn:aws:kms:eu-west-1:123456789012:key/<key-id>"
    }
  ]
}

It needs no list permission, and it grants only the parameters you can name.

Keep non-secret settings in appsettings.json

Both providers are ordinary configuration sources, so the order decides who wins. Add the secrets after appsettings.json and keep only non-secret defaults in the file:

AddJsonFile comes from its own package. A console project needs it installed (ASP.NET Core already includes it):

dotnet add package Microsoft.Extensions.Configuration.Json
appsettings.jsonjson
{
  "Database": {
    "Host": "localhost"
  }
}
AddSystemsManagercsharp
var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    // Non-secret defaults first, the parameters on top
    .AddSystemsManager("/my-app/dev/")
    .Build();
AddEnvildercsharp
var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    // Non-secret defaults first, the parameters on top
    .AddEnvilder("envilder.json")
    .Build();

A source added later overrides the ones before it. With Envilder, a key whose parameter does not exist adds nothing, so the appsettings value stays.

Comparison

What each one does, from the official README and source and from the Envilder SDK source:

AspectAddSystemsManagerAddEnvilder
PackageAmazon.Extensions.Configuration.SystemsManager (Apache 2.0)Envilder (MIT)
What you listA path prefix, optionally with names under itOne entry per variable with the full parameter name
Key namesThe parameter name without the prefix, with : for /The key in the map file, with : for /
AWS callsGetParametersByPath (recursive, paged) or GetParameters in batches of tenGetParameter once per variable
IAM actionsssm:GetParametersByPath (ssm:GetParameters for names), kms:Decrypt for SecureStringssm:GetParameter, kms:Decrypt for SecureString
SecureStringDecrypted when loadedDecrypted when loaded
Reload while runningYes, with reloadAfterNo, read once when built
New parameter under the prefixPicked up without a code changeNeeds a new line in the map file
Other AWS sourcesAppConfig, and Secrets Manager through reserved pathsNone
Azure Key VaultNoYes, with provider azure in the map file
Same file for CLI and GitHub ActionNoYes

When to choose which

The AWS provider fits when

  • Your app runs only on AWS and loads a whole path prefix. The prefix is the contract, and a new parameter under it appears without touching code.
  • Values must change while the app runs and you want reloadAfter to pick them up.
  • You already use AppConfig, or Secrets Manager through Parameter Store reserved paths, and want one package for them.
  • You prefer a package from AWS and no extra tooling.

Envilder fits when

  • The same map file must also drive local development (envilder exec or a .env file) and CI (the GitHub Action), so the app, the scripts and the pipelines read one list.
  • You map variable by variable, with names that do not share one prefix, and want that list in Git.
  • Azure Key Vault is also in use and you want to switch the provider in the map file instead of the C#.

If you only run on AWS and only need configuration inside the .NET process, the official provider is less to adopt. Envilder pays off when the map file is shared beyond the app. .NET SDK reference.

Try it locally with LocalStack

You can run both programs without an AWS account. LocalStack emulates Parameter Store, and AWS_ENDPOINT_URL makes the AWS SDK for .NET send its calls there. The output below was produced with localstack/localstack:4.14.0, which starts without an auth token. Newer images need LOCALSTACK_AUTH_TOKEN.

docker run -d --name localstack -p 4566:4566 localstack/localstack:4.14.0
$env:AWS_ENDPOINT_URL = 'http://localhost:4566'
$env:AWS_REGION = 'us-east-1'
$env:AWS_DEFAULT_REGION = 'us-east-1'
$env:AWS_ACCESS_KEY_ID = 'test'
$env:AWS_SECRET_ACCESS_KEY = 'test'
⚠️
Set all five variables in the same shell (AWS CLI v1 reads AWS_DEFAULT_REGION, not AWS_REGION), and run the aws ssm put-parameter commands from the first section in it. Without AWS_ENDPOINT_URL, both providers call real AWS with the dummy credentials and fail.

Each program prints only whether a value is set, so no secret reaches the console. Both print the same:

Database:Password: set
Api:Token: set

The two differ when a parameter is missing. With the map file entry "Stripe/Key": "/my-app/dev/Stripe/Key" for a parameter that does not exist, Envilder skips it and the key reads as missing, while the prefix approach simply has no such key. In both cases a failure to reach AWS throws.

To run a whole test suite this way, with dotnet test started through envilder exec, see Run integration tests against LocalStack without a .env file.