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,
- Create parameters in Parameter Store using Console
- Create a sample ASP.NET Core Web API
- Testing
- Clean up resources
Pre-requisites
- Download and install Visual Studio 2022.
- Download and install AWS Toolkit for Visual Studio 2022.
- An Active AWS Account. (See AWS Account Page.)
- User with sufficient access to create AWS resources for this article.(See IAM role in AWS IAM Roles Documentation.)
- User should have programmatic access keys. (See IAM user and Access in the AWS IAM documentation.)
- Download and install Amazon command line interface (AWS CLI). Configure AWS CLI.
Tools
- AWS Console
- Visual Studio 2022
Related Resources
- https://docs.aws.amazon.com/systems-manager/latest/userguide/param-create-cli.html
- https://docs.aws.amazon.com/systems-manager/latest/userguide/param-create-ps.html
- 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.
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.
Step 3
Enter the project name as AWSParameterStoreNETAPIDemo. Click Next.
Step 4
Select .NET 6.0 (Long-term support) as Framework. Click Create.
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.
Step 8
Leave the default name and click Add.
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
});
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
};
}
}
}
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.
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.