Search code examples
c#.net-core.net-6.0aws-parameter-store

Use IOption pattern with Aws Parameter Store and Array of Objects


In my local appsettings.Development.json I have this entry.

"AppInfo": {
    "AppList": [
      {
        "Id": 1,
        "Name": "DEVAPP1",
        "Description": "Description here1",
        "Status": true
      },
      {
        "Id": 2,
        "Name": "DEVAPP2",
        "Description": "Description here2",
        "Status": true
      }
    ]
  }

and my class

public class AppInfoOptions
    {
        public const string AppInfo = "AppInfo";
        public List <AppInfo>? AppList { get; set; }
    }
    public class AppInfo {
       
        public int Id { get; set; }
        public string? Name { get; set; }
        public string? Description { get; set; }
        public bool Status { get; set; }
    }

in program.cs

builder.Configuration.AddSystemsManager("/app/myapp", TimeSpan.FromSeconds(15));
builder.Services.Configure<AppInfoOptions>(builder.Configuration.GetSection(AppInfoOptions.AppInfo));

I can then access these values from my app and this works just fine when I read from my local appsettings{env}.json file.

public IndexModel(IOptions<AppInfoOptions> appInfoOptions)
        {
            this.appInfoOptions = appInfoOptions.Value;
        }
        public void OnGet()
        {
            var applist = appInfoOptions.AppList;
            foreach (var applistItem in applist)
            {
                var myNmae = applistItem.Name;
            };

        }

I would like to move these same structure to AWS parameter store using systemsmanager which I have it working with a simple key value pair but I can't figure it out how to setup the the aws parameter store with an array of objects. Currently I'm getting nulls.

Currently setup is like

/app/myapp/AppInfo/AppList
type:string
value:
[
      {
        "Id": 1,
        "Name": "DEVAWSAPP21",
        "Description": "Description here",
        "Status": true
      },
      {
        "Id": 2,
        "Name": "DEVAWSAPP2
        "Description": "Description here2",
        "Status": true
      }

    ]

Any ideas?


Solution

  • You need the following keys:

    /AppInfo/AppList/0/Id
    /AppInfo/AppList/0/Name
    /AppInfo/AppList/0/Description
    /AppInfo/AppList/0/Status
    /AppInfo/AppList/1/Id
    /AppInfo/AppList/1/Name
    /AppInfo/AppList/1/Description
    /AppInfo/AppList/1/Status
    

    These will be translated into an array through IConfiguration.