Deploy AWS AppConfig Configuration with the New L2 CDK Constructs
Tags: aws, appconfig, serverless, devops
Published: 2023-09-18 05:56:42

![Photo by Shashank Kumawat: [https://www.pexels.com/photo/assorted-color-banners-on-mountain-893975/](https://www.pexels.com/photo/assorted-color-banners-on-mountain-893975/)](https://cdn-images-1.medium.com/max/3212/1*CHbyDJkln8L_xjMmWCgj-A.png)

**In this post, I’ll guide you through deploying AWS AppConfig configurations using the AWS Cloud Development Kit (CDK) and the new AppConfig L2 [constructs](https://docs.aws.amazon.com/cdk/v2/guide/constructs.html) (higher abstraction).**

**We will deploy a JSON configuration for feature flags’ usage for serverless and non-serverless applications alike with Python CDK code.**

![[https://www.ranthebuilder.cloud/](https://www.ranthebuilder.cloud/)](https://cdn-images-1.medium.com/max/2000/0*1b9EOaXFuZhaszlP.png)

![[https://www.buymeacoffee.com/ranthebuilder](https://www.buymeacoffee.com/ranthebuilder)](https://cdn-images-1.medium.com/max/2000/0*aij2f7nxbmbSb58z.png)

**This blog post was originally published on my website, [“Ran The Builder.”](https://www.ranthebuilder.cloud/)**

## Quick AppConfig Introduction

AWS AppConfig is a self-managed service that stores plain TEXT/YAML/JSON configuration to be consumed by multiple clients.

Let’s look at AppConfig’s advantages:

 1. FedRAMP High certified

 2. Fully Serverless

 3. Out-of-the-box support for schema validations that run before a configuration update.

 4. Out-of-the-box integration with AWS CloudWatch alarms triggers an automatic configuration revert if a configuration update fails your AWS Lambda functions. Read more about it [here](https://aws.amazon.com/blogs/mt/proactive-monitoring-of-application-configuration-deployment-using-aws-appconfig-and-amazon-cloudwatch/).

 5. You can define configuration deployment strategies. Deployment strategies define how and when to change a configuration. Read more about it [here](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-creating-deployment-strategy.html).

 6. It provides a single API that fetches configuration.

If you’ve read my previous AppConfig [posts](https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-part-6-feature-flags-configuration-best-practices), you know that I use [AWS Lambda Powertools feature flags utility](https://docs.powertools.aws.dev/lambda/python/latest/utilities/feature_flags/) to fetch and evaluate AppConfig configurations.

## Our Goal

We aim to deploy a JSON [freeform](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-creating-configuration-and-profile.html) configuration that we can use with the AWS Lambda Powertools feature flags [utility](https://docs.powertools.aws.dev/lambda/python/latest/utilities/feature_flags/).

The construct I present here will deploy a JSON configuration to AppConfig as a freeform configuration with a zero wait time deployment strategy.

For production accounts, you should use a canary [deployment](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-creating-deployment-strategy.html) profile and configure JSON [validations](https://docs.aws.amazon.com/appconfig/2019-10-09/APIReference/API_Validator.html) and a CloudWatch [alarm](https://aws.amazon.com/blogs/mt/proactive-monitoring-of-application-configuration-deployment-using-aws-appconfig-and-amazon-cloudwatch/) to trigger an automatic rollback in case of service errors during configuration deployment.

If you want to learn about best practices for feature flags and how to use, build, and test feature flags, check out my two posts about them:

* [AWS Lambda Cookbook — Part 6 — Configuration & Feature Flags Best Practices](https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-part-6-feature-flags-configuration-best-practices)

* [Manage Your AWS Lambda Feature Flags Like a Boss](https://www.ranthebuilder.cloud/post/manage-your-aws-lambda-feature-flags-like-a-boss)

Let’s go ahead and write some code.

## Python Dependencies

To consume the new L2 construct, which contains better abstractions, add the following statements to your poetry file:
{% gist https://gist.github.com/ran-isenberg/9a32b3afc32193936b15c15c5e34c4e3.js %}
Be aware that we are using an alpha construct, which might break or contain bugs. Use it at your discretion. However, after testing it, I can confirm it works as expected at the time of writing, and it’s already part of the AWS Lambda Handler Cookbook [project](https://github.com/ran-isenberg/aws-lambda-handler-cookbook).

## Configuration Construct

Let’s go over my new construct, which uses L2 higher abstractions constructs, thus reducing the overall effort to define AppConfig configurations:
{% gist https://gist.github.com/ran-isenberg/c15f2d40bfc1c2412461aad7655c74af.js %}
Our construct requires several input parameters:

 1. id_ (str): The scoped construct ID. It must be unique.

 2. Environment (str): AppConfig environment name to create.

 3. service_name (str): AppConfig application name to create.

 4. configuration_name (str): AppConfig configuration name to create.

 5. configuration_str (str): AppConfig configuration content to create

In lines 33–40, we create the AppConfig application; in line 40, we set the removal policy to destroy. You will have a similar approach applied to all resources.

In lines 41–48, we define the AWS AppConfig environment.

In lines 51–61, we define the AppConfig deployment strategy. I chose an immediate deployment (zero minutes for bake and deployment times), but you should use the canary deployment options or define your own for production workloads. Read the docs [here](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-creating-deployment-strategy.html).

In lines 63–75, we define the configuration to deploy and connect all the resources.

Notice how I chose line 69, the freeform type. You must use freeform for usage with the Powertools feature flags utility.

In line 68, we provide the configuration string we got as a construct input parameter.

Line 75 is a workaround until the removal policy is set and exposed correctly (see the link to the CDK issue).

**Bottom line:**

The new L2 constructs are a welcome edition as they reduce the total amount of code and the overall complexity, but I suggest that you create your own construct that wraps these L2 constructs into one or use the example I provided here.

For other information and connecting advanced features such as JSON validators, extensions, and CloudWatch alarm, refer to the official documentation below:

[https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_appconfig_alpha.html](https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_appconfig_alpha.html)