Skip to content

feat: Add AppConfig parameter provider #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion docs/utilities/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Utility
---

<!-- markdownlint-disable MD013 -->
The Parameters utility provides high-level functionality to retrieve one or multiple parameter values from [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html){target="_blank"}, [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/){target="_blank"}, or [Amazon DynamoDB](https://aws.amazon.com/dynamodb/){target="_blank"}. We also provide extensibility to bring your own providers.
The Parameters utility provides high-level functionality to retrieve one or multiple parameter values from [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html){target="_blank"}, [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/){target="_blank"}, [Amazon DynamoDB](https://aws.amazon.com/dynamodb/){target="_blank"}, or [AWS AppConfig](https://docs.aws.amazon.com/appconfig/latest/userguide/what-is-appconfig.html){target="_blank"}. We also provide extensibility to bring your own providers.

## Key features

Expand Down Expand Up @@ -33,6 +33,7 @@ This utility requires additional permissions to work as expected. See the table
| Secrets Manager | `SecretsProvider.Get(string)` `SecretsProvider.Get<T>(string)` | `secretsmanager:GetSecretValue` |
| DynamoDB | `DynamoDBProvider.Get(string)` `DynamoDBProvider.Get<T>(string)` | `dynamodb:GetItem` |
| DynamoDB | `DynamoDBProvider.GetMultiple(string)` `DynamoDBProvider.GetMultiple<T>(string)` | `dynamodb:Query` |
| App Config | `AppConfigProvider.Get()` | `appconfig:StartConfigurationSession` `appconfig:GetLatestConfiguration` |

## SSM Parameter Store

Expand Down Expand Up @@ -341,6 +342,7 @@ You can retrieve multiple parameters sharing the same `id` by having a sort key
"param-b": "my-value-b",
"param-c": "my-value-c"
}
```

**Customizing DynamoDBProvider**

Expand Down Expand Up @@ -377,6 +379,106 @@ DynamoDB provider can be customized at initialization to match your table struct
}
```

## App Configurations

For application configurations in AWS AppConfig, use `AppConfigProvider`.

Alternatively, you can retrieve the instance of provider and configure its underlying SDK client,
in order to get data from other regions or use specific credentials.

=== "AppConfigProvider"

```c# hl_lines="10-13 16-18"
using AWS.Lambda.Powertools.Parameters;
using AWS.Lambda.Powertools.Parameters.AppConfig;

public class Function
{
public async Task<APIGatewayProxyResponse> FunctionHandler
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
{
// Get AppConfig Provider instance
IAppConfigProvider appConfigProvider = ParametersManager.AppConfigProvider
.DefaultApplication("MyApplicationId")
.DefaultEnvironment("MyEnvironmentId")
.DefaultConfigProfile("MyConfigProfileId");

// Retrieve a single configuration, latest version
IDictionary<string, string?> value = await appConfigProvider
.GetAsync()
.ConfigureAwait(false);
}
}
```

=== "AppConfigProvider with an explicit region"

```c# hl_lines="10-14"
using AWS.Lambda.Powertools.Parameters;
using AWS.Lambda.Powertools.Parameters.AppConfig;

public class Function
{
public async Task<APIGatewayProxyResponse> FunctionHandler
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
{
// Get AppConfig Provider instance
IAppConfigProvider appConfigProvider = ParametersManager.AppConfigProvider
.ConfigureClient(RegionEndpoint.EUCentral1)
.DefaultApplication("MyApplicationId")
.DefaultEnvironment("MyEnvironmentId")
.DefaultConfigProfile("MyConfigProfileId");

// Retrieve a single configuration, latest version
IDictionary<string, string?> value = await appConfigProvider
.GetAsync()
.ConfigureAwait(false);
}
}
```

**Using AWS AppConfig Feature Flags**

Feature flagging is a powerful tool that allows safely pushing out new features in a measured and usually gradual way. AppConfig provider offers helper methods to make it easier to work with feature flags.

=== "AppConfigProvider"

```c# hl_lines="10-13 16-18 23-25"
using AWS.Lambda.Powertools.Parameters;
using AWS.Lambda.Powertools.Parameters.AppConfig;

public class Function
{
public async Task<APIGatewayProxyResponse> FunctionHandler
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
{
// Get AppConfig Provider instance
IAppConfigProvider appConfigProvider = ParametersManager.AppConfigProvider
.DefaultApplication("MyApplicationId")
.DefaultEnvironment("MyEnvironmentId")
.DefaultConfigProfile("MyConfigProfileId");

// Check if feature flag is enabled
var isFeatureFlagEnabled = await appConfigProvider
.IsFeatureFlagEnabledAsync("MyFeatureFlag")
.ConfigureAwait(false);

if (isFeatureFlagEnabled)
{
// Retrieve an attribute value of the feature flag
var strAttValue = await appConfigProvider
.GetFeatureFlagAttributeValueAsync<string>("MyFeatureFlag", "StringAttribute")
.ConfigureAwait(false);

// Retrieve another attribute value of the feature flag
var numberAttValue = await appConfigProvider
.GetFeatureFlagAttributeValueAsync<int>("MyFeatureFlag", "NumberAttribute")
.ConfigureAwait(false);
}
}
}
```

## Advanced configuration

### Caching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
<ItemGroup>
<!-- Package versions are Centrally managed in Directory.Packages.props file -->
<!-- More info https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management -->
<PackageReference Include="AWSSDK.DynamoDBv2"/>
<PackageReference Include="AWSSDK.SecretsManager"/>
<PackageReference Include="AWSSDK.SimpleSystemsManagement"/>
<PackageReference Include="Microsoft.Extensions.Configuration"/>
<PackageReference Include="AWSSDK.AppConfig" />
<PackageReference Include="AWSSDK.AppConfigData" />
<PackageReference Include="AWSSDK.DynamoDBv2" />
<PackageReference Include="AWSSDK.SecretsManager" />
<PackageReference Include="AWSSDK.SimpleSystemsManagement" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<ProjectReference Include="..\AWS.Lambda.Powertools.Common\AWS.Lambda.Powertools.Common.csproj" PrivateAssets="All" />
</ItemGroup>

Expand Down
Loading