How To Access Parameters From AWS Systems Manager Parameter Store In .NET 6 Web API

Introduction

In this article, you will learn how to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API. Parameter Store, a capability of AWS Systems Manager, provides secure, hierarchical storage for configuration data management and secrets management. You can store data such as passwords, database strings, Amazon Machine Image (AMI) IDs, and license codes as parameter values. You can store values as plain text or encrypted data.

Topics Covered

This article demonstrates how to build the following,

  1. Create parameters in Parameter Store using Console
  2. Create a sample ASP.NET Core Web API
  3. Testing
  4. Clean up resources

Pre-requisites

  1. Download and install Visual Studio 2022.
  2. Download and install AWS Toolkit for Visual Studio 2022.
  3. An Active AWS Account. (See AWS Account Page.)
  4. User with sufficient access to create AWS resources for this article.(See IAM role in AWS IAM Roles Documentation.)
  5. User should have programmatic access keys. (See IAM user and Access in the AWS IAM documentation.)
  6. Download and install Amazon command line interface (AWS CLI). Configure AWS CLI.

Tools

  1. AWS Console
  2. Visual Studio 2022

Related Resources

  1. https://docs.aws.amazon.com/systems-manager/latest/userguide/param-create-cli.html
  2. https://docs.aws.amazon.com/systems-manager/latest/userguide/param-create-ps.html
  3. https://docs.aws.amazon.com/systems-manager/latest/userguide/deleting-parameters.html

Task 1: Create parameters in Parameter store using Console

In this task, you will see how to create sample parameters using Console.

Step 1

Follow this link to create the parameters using Console.

Step 2

The following parameters are created for this article.

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

Task 2: Create a sample ASP.NET Core Web API

In this task, you will see how to create a new sample .NET 6 ASP.NET Core Web API using Visual Studio 2022.

Step 1

Open Visual Studio 2022, click Create a new project.

Step 2

Search ASP.NET in the search bar, select ASP.NET Core Web API project template and click Next.

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

Step 3

Enter the project name as AWSParameterStoreNETAPIDemo. Click Next.

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

Step 4

Select .NET 6.0 (Long-term support) as Framework. Click Create.

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

Step 5

Expand Controller folder in the solution explorer, right click WeatherForecastController.cs file and click Delete. Right click WeatherForecast.cs file and click Delete.

Step 6

Right click Controllers folder, click Add and then click Controller.

Step 7

Select API -> API Controller with read/write actions. Click Add.

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

Step 8

Leave the default name and click Add.

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

Step 9

Add Amazon.Extensions.Configuration.SystemsManager NuGet package.

Step 10

Open Program.cs file and add the following code snippet as shown below.

builder.Configuration.AddSystemsManager("/myapp/dev/", new AWSOptions {
    Region = RegionEndpoint.USWest2
});

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

NOTE
Parameter name (“/myapp/dev”) and RegionEndPoint should be coming from Environment variables (Region = RegionEndpoint.GetBySystemName("us-west-2")) or it should be dynamic instead of hard-code values in production environment.

Step 11

Open ValuesController.cs file and replace the code with following as shown below.

using Microsoft.AspNetCore.Mvc;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace AWSParameterStoreNETAPIDemo.Controllers {
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController: ControllerBase {
        private readonly IConfiguration _configuration;
        public ValuesController(IConfiguration configuration) {
            _configuration = configuration;
        }
        // GET: api/<ValuesController>
        [HttpGet]
        public IEnumerable < string > Get() {
            var connectionString = _configuration.GetValue < string > ("connectionstring");
            var key1 = _configuration.GetValue < string > ("key1");
            var key2 = _configuration.GetValue < string > ("key2");
            return new string[] {
                connectionString,
                key1,
                key2
            };
        }
    }
}

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

Task 3: Testing

In this task, you will see how to test the API to retrieve the values from Parameter store.

Hit F5 to run the API locally and Swagger will be displayed. Expand /api/values and click Try it out. Click Execute to get the response as shown below.

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

How to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API

Task 4: Clean up resources

Delete all the resources to ensure that you're not charged for any services you aren't using.

Summary

This article describes how to access parameters from AWS Systems Manager Parameter store in .NET 6 Web API.


Recommended Free Ebook
Similar Articles