AWS Feature Flags with AppConfig: A Practical Guide

AWS Feature Flags Blog Banner

What Are Feature Flags? 

Feature flags, also known as feature toggles, are used in software development to enable or disable features of an application at runtime without deploying new code. This allows developers to manage feature visibility and test new functionalities in live environments with minimal risk. 

By toggling a feature on or off through a simple configuration change, teams can rapidly respond to issues or feedback without the need for immediate code changes or redeployments. Feature flags facilitate a more dynamic approach to releasing and managing software. 

Feature flags serve as switches that can be flipped to introduce new features to certain users, conduct A/B testing, or roll back problematic features. This enhances the development team’s control over the application and improves the user experience by ensuring that changes are seamless and minimally disruptive.

What Is AWS AppConfig and How Does It Enable Feature Flags? 

AWS AppConfig is a capability of AWS Systems Manager that enables developers to create, manage, and deploy application configurations. AppConfig supports applications hosted on Amazon Elastic Compute Cloud (Amazon EC2) instances, AWS Lambda, containers, mobile applications, or IoT devices. 

With AppConfig, developers can safely deploy changes to application configurations at runtime without the need for code deployments. The service simplifies the process of rolling out feature flags, application settings, and other configurations across an environment or set of environments. It ensures that the deployed configurations are validated against a schema or set of constraints to reduce the risk of configuration errors.

AWS AppConfig supports feature management, including feature flags and other operational practices like A/B testing, feature toggles, and canary deployments. It integrates with Amazon CloudWatch for real-time monitoring of configuration changes. 

How Do Feature Flags Work in AWS AppConfig?

Feature flags in AWS AppConfig work by allowing developers to create and manage dynamic configuration settings that can be adjusted at runtime. This is achieved through a series of steps involving the creation of configuration profiles, environments, and the definition of individual feature flags.

To start, developers create a configuration profile within AWS AppConfig, specifying the settings and constraints for the feature flags. These profiles act as containers for the feature flags and are associated with specific environments, such as development, staging, or production. Each feature flag within the profile can be toggled on or off, and changes are propagated through the environment in real-time.

Furthermore, AWS AppConfig validates configuration data before deployment, ensuring it meets predefined criteria and constraints. This validation step is crucial to prevent misconfigurations that could potentially disrupt the application. By integrating with AWS services like Amazon CloudWatch, AWS AppConfig also enables real-time monitoring of the impact of feature flags on application performance, providing feedback loops that help in making informed decisions about feature rollouts and rollbacks.

Quick Tutorial: Using AWS AppConfig Feature Flags

Here’s an overview of how to use feature flags in AWS with AppConfig. The example and instructions in this tutorial are adapted from an AWS blog.

Create a Feature Flag 

The first part of the process is to define an Application, which serves as a logical grouping for organizing feature flags and other configuration data relevant to your application’s needs. For this example, we’ll name the application MyFlags

Start by navigating to the AWS Management Console. From there, access AWS AppConfig:

Click on the Get Started button. In the Select configuration type screen, name your configuration MyFlags and click Next.

We’ll add a Flag name and Flag key. In this example, the feature flag allows customers to use a credit card in the checkout process, so we’ll call it: allow-credit-card-at-checkout

Click Next to review the changes. Set the application name to MyFlags:

Once the application is created, you can edit the MyFlags configuration by selecting Applications > MyFlags > MyFlags as shown below:

Use the Feature Flag 

Let’s see how to use the new feature flags in action. Initially, the allow-credit-card-at-checkout flag is disabled, meaning users won’t see this payment option on the checkout page. 

Note: Before proceeding, ensure that you have the latest version of Boto3 installed.

The following Python script demonstrates how an application might query AWS AppConfig to retrieve and apply feature flag settings:

import boto3, json
from datetime import datetime, timedelta

# Define AWS and AppConfig settings
AWS_REGION = "us-east-1"
APPCONFIG_APPLICATION_NAME = "MyFlags"
APPCONFIG_ENVIRONMENT_NAME = "test-environment"
APPCONFIG_CONFIG_PROFILE_NAME = "MyFlags"

# Initialize the AppConfig client
appconfig_client = boto3.client('appconfigdata', region_name=AWS_REGION)

# Function to fetch and return the feature flag configuration
def get_feature_flag_configuration():
    # Start a configuration session to fetch the latest configuration data
    start_session_response = appconfig_client.start_configuration_session(
        ApplicationIdentifier=APPCONFIG_APPLICATION_NAME,
        EnvironmentIdentifier=APPCONFIG_ENVIRONMENT_NAME,
        ConfigurationProfileIdentifier=APPCONFIG_CONFIG_PROFILE_NAME,
    )

    # Retrieve the initial configuration token from the response
    config_token = start_session_response["InitialConfigurationToken"]

    # Use the token to get the latest configuration data
    config_data_response = appconfig_client.get_latest_configuration(
        ConfigurationToken=config_token
    )

    # Read and parse the configuration data (assuming JSON format)
    config_data_json = config_data_response["Configuration"].read().decode("utf-8")
   
    return json.loads(config_data_json)

# Example usage of fetching feature flag data
feature_flags = get_feature_flag_configuration()
print(feature_flags)

This script queries AWS AppConfig for the current state of the feature flag. It starts by setting up necessary identifiers and initializing an AppConfig client with Boto3, AWS’s SDK for Python. It then defines a get_feature_flag_configuration() function that starts a new configuration session and retrieves the latest feature flag configurations using these identifiers.

The output should look like this:

{‘allow-credit-card-at-checkout’: {‘enabled’: False}}

When you are ready to enable the feature for users, you can enable the flag in AWS AppConfig, and your application can use this or similar code to identify the feature is active and enable it for users, without redeploying or modifying code directly.

Try Configu for free
Painless end-to-end configuration management platform
Get Started for Free