Creating an AWS AppConfig Application using Terraform

Vaibhav Srivastava
5 min readApr 6, 2022

In a standard software development cycle, developers introduce new environment variables in application configuration in order to meet the need of the growing application. These variables are used to hold different values in multiple environments thus standardizing the code.

In my experience, there are quite a few instances everybody would be able to think of when the information or communication gets lost or is buried in some obscure documentation until a build that requires that environment variable fails.

One of the use-cases for me is to find a solution, which can be used as a shared responsibility between Developers and DevOps Engineering where they can add/update and use application-specific environment variables. AWS AppConfig provides an easy way to accomplish this.

AWS AppConfig enables the deployment of configuration changes independent of the application code deployment. This means that the applications are not required to be restarted or taken out of service, whenever an application configuration is updated.

Benefits and features

  • Create feature flags and free form configuration data —
    It can be used to create, update, and deploy feature flags and free form configurations, but having the capability to dynamically update the application in runtime is the icing on the cake. AppConfig supports applications hosted on Amazon EC2 instances, AWS Lambda, containers, mobile applications, on-prem servers, or IoT devices.
  • Minimize the risk of updates
    AppConfig also offers AWS AppConfig validators. which validate the configuration data before deployment, typos are always a nuisance in complex deployments.
  • Automatic rollback of configuration changes if errors occur
    Every invincible fails so there are chances that AWS AppConfig will also fail and deploy erroneous code. AppConfig offers a solution for that. The deployed configuration will automatically be rolled back through AWS AppConfig if an Amazon CloudWatch alarm is triggered.

AWS AppConfig use cases

AWS AppConfig is useful in the following use cases:

  • Application tuning — Introduce changes for load testing.
  • Feature toggle — Time-sensitive introduction of features like Product launches.
  • Allow list — Premium Subscription.
  • Operational issues — Reduce stress on your application when a dependency or other external factor impacts the system.

Creating & Setting up AppConfig

It’s more important to consider how you’d want to manage your infrastructure as a whole. Fortunately, Terraform shines at that and gives you everything you need out of the box. SAM can be a great fit if you are already using CloudFormation. For my implementation, I am using Terraform, so let's get started

You can follow your preferred way of the directory structure. So I have provided the filename and content I used in my implementation. First, in your project root, create a file provider.tf

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.7"
}
}
}
provider "aws" {
region = "us-east-1"
# access_key = var.aws_access_key_id
# secret_key = var.aws_secret_access_key
}

Create another file for creating the application and app config profile in dev_app_config_application.tf

resource "aws_appconfig_application" "v_app" {
name = "v_app_config_poc"
description = "PoC for testing AppConfig Application"
tags = {
Type = "AppConfig Application"
Environment = "Dev"
}
}
resource "aws_appconfig_configuration_profile" "develop" {
application_id = aws_appconfig_application.v_app.id
description = "Dev Configuration Profile"
name = "dev-profile"
location_uri = "hosted"

tags = {
Type = "Dev AppConfig Configuration Profile"
Environment = "Dev"
}
}

The output of the above terraform run will create application and configuration profile as shown here on your AWS console for AppConfig

create another file for creating the environment and hosted configuration in dev_app_config_environment.tf and run terraform apply

resource "aws_appconfig_environment" "develop" {
name = "dev"
description = "Dev AppConfig Environment"
application_id = aws_appconfig_application.terradatum.id
tags = {
Type = "Dev AppConfig Environment"
}
}
resource "aws_appconfig_hosted_configuration_version" "develop" {
application_id = aws_appconfig_application.terradatum.id
configuration_profile_id = aws_appconfig_configuration_profile.develop.configuration_profile_id
description = "Dev Hosted Configuration Version"
content_type = "application/json"
content = jsonencode({
Environment = "dev",
BillingUnit = "CompanyName",
isThingEnabled = true,
database_user = var.dev.database_name,
database_password = var.dev.database_password
})
}

The output of above terraform run will create environment and hosted configuration with environment variables as shown here on your AWS console for AppConfig

Now create another file for creating deployment strategies and deployment configuration in dev_app_config_deployment.tf and run terraform apply


resource "aws_appconfig_deployment_strategy" "develop" {
name = "develop-deployment-strategy"
description = "Dev Deployment Strategy"
deployment_duration_in_minutes = 1
final_bake_time_in_minutes = 1
growth_factor = 20
growth_type = "LINEAR"
replicate_to = "NONE"
tags = {
Type = "Dev AppConfig Deployment Strategy"
}
}
resource "aws_appconfig_deployment" "develop" {
application_id = aws_appconfig_application.terradatum.id
configuration_profile_id = aws_appconfig_configuration_profile.develop.configuration_profile_id
configuration_version = aws_appconfig_hosted_configuration_version.develop.version_number
deployment_strategy_id = aws_appconfig_deployment_strategy.develop.id
description = "Dev deployment"
environment_id = aws_appconfig_environment.develop.environment_id
tags = {
Type = "Dev AppConfig Deployment"
}
}

The output of the above terraform run will create deployment strategies (highlighted section is the one you just created along with AWS provided) as shown here on your AWS console for AppConfig

This completes our application configuration setup for the application in AppConfig on AWS Console. The same can be repeated to create more environments as per your company standards. I will be creating another article to demonstrate the use of AWS AppConfig.

Final Results

Here is the final result of the AppConfig diagram I have shown above for setting up individual profiles for each environment

Image to show final results of workflow diagram
Image to show final results of workflow diagram
Image to show we can have various deployment strategies for each environment based on the use case

Summary

This post demonstrates how quick and efficient application creation can be done on AWS AppConfig using Terraform.

This allows faster and seamless configuration deployments as the build stage is not required for the same. The implementation can be further extended by adding Validators and Monitors(Amazon CloudWatch alarms) as has been explained previously

If you are using CloudFormation Template then you can also enable AppConfig within the CloudFormation stack to access the value from AppConfig during infrastructure creation/update.

You can read more about AWS AppConfig in the User Guide.

--

--

Vaibhav Srivastava

Solutions Architect | AWS & Azure Certified | Hybrid & Multi-Cloud Exp. | Technophile