Feature Toggles with AWS AppConfig: A Complete Guide
Feature toggles (or feature flags) are a powerful technique that allows developers to modify system behavior without changing code. By separating deployment from release, teams can merge code to production safely and turn features on or off dynamically. AWS AppConfig, a capability of AWS Systems Manager, provides a robust and scalable way to manage these configurations.
In this guide, we'll explore how to set up feature toggles with AWS AppConfig and consume them in a TypeScript application.
<div class="toc"> <h3>Table of Contents</h3> <ul> <li><a href="#what-is-aws-appconfig">What is AWS AppConfig?</a></li> <li><a href="#setting-up-feature-toggles">Setting Up Feature Toggles</a></li> <li><a href="#retrieving-flags-with-typescript">Retrieving Flags with TypeScript</a></li> <li><a href="#best-practices">Best Practices</a></li> <li><a href="#faq">Frequently Asked Questions</a></li> </ul> </div> <h2 id="what-is-aws-appconfig">What is AWS AppConfig?</h2>AWS AppConfig is designed to manage application configurations at runtime. Unlike simple key-value stores (like Parameter Store), AppConfig offers:
- Validation: Ensure your configuration is syntactically and semantically correct before deployment.
- Deployment Strategies: Roll out changes gradually (e.g., linear, exponential) to minimize blast radius.
- Monitoring: Automatically roll back changes if CloudWatch alarms are triggered.
To use AppConfig, you need to define three main resources:
- Application: A logical container for your configurations.
- Environment: Represents the deployment stage (e.g.,
Development,Production). - Configuration Profile: The actual data source. For feature flags, choose the "Feature Flag" profile type.
Step-by-Step
- Go to the AWS AppConfig console.
- Create an Application named
MyService. - Create an Environment named
Production. - Create a Configuration Profile named
FeatureFlagsand select "Feature Flag" as the type. - Add a flag (e.g.,
new_checkout_flow) and enable it. - Start a deployment to push this configuration to the
Productionenvironment.
To fetch these flags in your application, use the AWS SDK for JavaScript v3. Specifically, we use the @aws-sdk/client-appconfigdata package.
First, install the client:
npm install @aws-sdk/client-appconfigdata
Here is a robust pattern to retrieve and decode the configuration:
import {
AppConfigDataClient,
StartConfigurationSessionCommand,
GetLatestConfigurationCommand
} from "@aws-sdk/client-appconfigdata";
const client = new AppConfigDataClient({ region: "us-east-1" });
// Store the token in memory
let nextPollToken: string | undefined;
async function getFeatureFlag() {
// 1. Start a session if we don't have a token
if (!nextPollToken) {
const startCommand = new StartConfigurationSessionCommand({
ApplicationIdentifier: "MyService",
EnvironmentIdentifier: "Production",
ConfigurationProfileIdentifier: "FeatureFlags",
RequiredMinimumPollIntervalInSeconds: 60, // Enforce caching
});
const startResponse = await client.send(startCommand);
nextPollToken = startResponse.InitialConfigurationToken;
}
// 2. Poll for the latest configuration
const getCommand = new GetLatestConfigurationCommand({
ConfigurationToken: nextPollToken,
});
const response = await client.send(getCommand);
// Update the token for the next call
nextPollToken = response.NextPollConfigurationToken;
// 3. Process the configuration if available
if (response.Configuration) {
const strConfig = new TextDecoder("utf-8").decode(response.Configuration);
const config = JSON.parse(strConfig);
console.log("Current Flags:", config);
return config;
}
console.log("No changes received.");
return null;
}
// Usage
getFeatureFlag().then(flags => {
if (flags && flags.new_checkout_flow?.enabled) {
console.log("New checkout flow is active!");
} else {
console.log("Using legacy checkout.");
}
});
<h2 id="best-practices">Best Practices</h2>
- Cache the Token: Always store
NextPollConfigurationToken. Do not callStartConfigurationSessionon every request. - Respect Poll Interval: AppConfig works best when clients poll at intervals (e.g., every 30-60 seconds), not on every user request.
- Local Fallbacks: Ensure your application has default values if AppConfig is unreachable.
Q: How fast do changes propagate? A: Changes usually propagate within seconds after the deployment strategy completes.
Q: Does AppConfig cost money? A: Yes, you pay for API calls and data received. Using correct polling intervals keeps costs low.
Q: Can I validate flags? A: Yes, AppConfig supports JSON Schema validators to ensure flag payloads meet your requirements.
<br> <p>Kaynak / Source: https://awsfundamentals.com/blog/feature-toggles-with-appconfig</p>