AWS: Retrieve AppConfig Feature Flag using Lambda

Itsuki
AWS Tip
Published in
5 min readFeb 3, 2024

--

If we want to use AppConfig to manage configurations for a Lambda function without AWS AppConfig Agent Lambda extension, then we must configure our Lambda function to receive configuration updates by integrating with the StartConfigurationSession and GetLatestConfiguration API actions.

You might be wondering why you cannot just call GetLatestConfiguration every time you need the configuration profile.

I mean, technically speaking, you could if you want to pay for the bill as AppConfig pricing is based on the number of times a configuration is called and received. I don’t. Therefore, only call when there are configuration updates is a much better solution, at least for me.

Integrating the AWS AppConfig Agent Lambda extension with our Lambda simplifies this entire process.

The extension takes care of the following for us! Automatically!

  • calling the AWS AppConfig service,
  • managing a local cache of retrieved data,
  • tracking the configuration tokens needed for the next service calls
  • periodically checking for configuration updates in the background

In this article, let’s see how we can use Lambda to retrieve our feature flags stored in AppConfig.

We will be using the example we have created in this previous article so make sure you have your AppConfig set up before you move on!

Create Lambda

Let’s create a simple Lambda Function from scratch.

From Lambda Console, Choose Function and Create function.

Use the configurations above and Choose Create function.

Configure Permission

Our Lambda will need the permissions to call appconfig:StartConfigurationSession andappconfig:GetLatestConfiguration, so let’s add it to our Execution role.

Choose Configuration tab and select Permissions. Click on the super link under Role name. This should take you the to role detail page in IAM Console.

Scroll down and under Permissions, Choose Add permissions > Create inline policy.

Choose JSON, Copy and paste the following policy.

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Effect": "Allow",
"Action": [
"appconfig:GetLatestConfiguration",
"appconfig:StartConfigurationSession"
],
"Resource": ["*"]
}
]
}

Choose Next. Give your policy a name and choose Create policy.

Add AWS AppConfig Agent Lambda extension

To use the AWS AppConfig Agent Lambda extension, we will add the extension to the Lambda function as a layer.

Under Function overview, Choose Layers.

Choose Add a layer.

For Layer source, choose AWS layers. and Choose AWS-AppConfig-Extension. The version will be base on the runtime of our Lambda, in this case it will be 84 (which is the only option here).

Choose Add.

If you are creating your lambda for example, in CDK as a Docker image, you can also add the AWS AppConfig Agent Lambda extension as a container image as describe here.

Retrieve Configurations

Now that we have added the extension, let’s retrieve our configuration and check out some of the specifications we can make.

Retrieve an Entire Configuration

To Retrieve an Entire Configuration, We will simply read from the url in the following format http://localhost:2772/applications/{application_name}/environments/{environment_name}/configurations/{configuration_name}.

In our example, it will look like the following.

import urllib.request

application_name = "appconfig_demo"
environment_name = "production"
configuration_name = "prod_feature_flag"

def lambda_handler(event, context):
url = f"http://localhost:2772/applications/{application_name}/environments/{environment_name}/configurations/{configuration_name}"
config = urllib.request.urlopen(url).read()
return config

Deploy the code change and choose Test.

You should see the following.

{
"functionality1": {
"allowed_app_list": [
"app1",
"app2"
],
"enabled": true
}
}

Prefetching configuration data

As most of you know, Lambda is notorious (may be not that much) for its cold-start. By configuring the environment variable AWS_APPCONFIG_EXTENSION_PREFETCH_LIST, we can make some improvement on that.

To use the prefetch capability, set the value of the environment variable to the path corresponding to your configuration data in the form of /applications/{application_name}/environments/{environment_name}/configurations/{configuration_name}

In our case, that would be /applications/appconfig_demo/environments/production/configurations/prod_feature_flag.

Choose Configuration > Environment variables. Choose Edit.

Add our variable.

Choose Save.

We can also specify multiple configuration items by listing them as a comma-separated list (If you have a resource name that includes a comma, use the resource’s ID value instead of its name).

Retrieve Specific Flags from configuration

For feature flag configurations (configurations of typeAWS.AppConfig.FeatureFlags), the Lambda extension enables us to retrieve a single flag or a subset of flags in a configuration. It is useful if we only need to use a few specific flags from the configuration profile.

We only have created 1 feature flag in our example, but here is what we can do if we want to retrieve that specific flag instead of the entire configuration.

We will basically add it as a url query like following.

import urllib.request

application_name = "appconfig_demo"
environment_name = "production"
configuration_name = "prod_feature_flag"
flag_name = "functionality1"


def lambda_handler(event, context):
url = f"http://localhost:2772/applications/{application_name}/environments/{environment_name}/configurations/{configuration_name}?flag={flag_name}"
config = urllib.request.urlopen(url).read()
return config

This time, you should only get the detail for this specific flag as your returned value.

{
"allowed_app_list": [
"app1",
"app2"
],
"enabled": true
}

You can also choose to access multiple specific flags and their attributes.

Suppose we also have a flag named functionality2 and to get the attributes for both of the flags

import urllib.request

application_name = "appconfig_demo"
environment_name = "production"
configuration_name = "prod_feature_flag"
flag_name_1 = "functionality1"
flag_name_2 = "functionality2"

def lambda_handler(event, context):
url = f"http://localhost:2772/applications/{application_name}/environments/{environment_name}/configurations/{configuration_name}?flag={flag_name_1}&flag={flag_name_2}"
config = urllib.request.urlopen(url).read()
return config

That’s all I have for today!

As you can see, using the AWS AppConfig Agent Lambda extension makes work with AWS AppConfig feature flags from Lambda a lot easier!

Have a nice day and happy coding!

--

--