DEV Community

Cover image for AWS - Decouple configuration from code
Prabusah
Prabusah

Posted on • Edited on

6 4

AWS - Decouple configuration from code

Requirement

  • Separate configuration from application code.
  • Application code has to pickup any changes to configuration data immediately.

AWS AppConfig

  • Feature of AWS Systems Manager service.
  • AppConfig allows to quickly deploy configurations.
  • Allows configuration formats like text / json / file from s3 etc.
  • Applications that was using Tridion and migrating to AWS can make use of AWS AppConfig which is powerful than Tridion.
  • AWS AppConfig can be used with applications hosted on Amazon EC2 instances, AWS Lambda, containers etc.

Creating configuration data in AWS AppConfig
This link walks through step by step creation and deployment of Configuration using AWS AppConfig.
To follow along this blog - use below settings while setup.

  • Use Freeform Configuration profile type
  • Application Name: BlogConfig
  • Environment Name: prod
  • Configuration Name: productRollout

Configuration data
Follow above link to deploy below JSON configuration data to AppConfig.

{
  "newProductRolloutFlag": true,  
  "newProductRolloutStates": ["IL","CA"]
}
Enter fullscreen mode Exit fullscreen mode

How to access configuration data from AppConfig
Two ways - API from AWS SDK and Lambda Extension

AWS SDK
It's a two step process:
Without diving into extensive details, on a high level...

  1. Establish configuration session using StartConfigurationSession API - this returns InitialConfigurationToken to be passed to the below API for first time.
  2. GetLatestConfiguration API - this returns the latest configuration data from AppConfig along with NextPollConfigurationToken to be passed in subsequent call.

My personal choice is using AppConfig Lambda Extension - this is a performant and developer friendly way.

AppConfig Lambda extension
AWS has built a lambda extension for AppConfig - Below are the steps to add AppConfig Lambda Extension as a lambda layer in AWS console UI.
Goto AWS Lambda Service -> Add Lambda layer -> Choose a Layer -> AWS layers -> AWS-AppConfig-Extension -> Add the latest version shown.

Sample Code

//index.js (NodeJS runtime)
const http = require('http');
const params = process.env.APPCONFIG_PROFILE.split('/')
//Sample APPCONFIG_PROFILE value: "BlogConfig/prod/productRollout"
const AppConfigApplication = params [0]; //BlogConfig
const AppConfigEnvironment = params [1]; //prod
const AppConfigConfiguration = params [2] //productRollout

function getConfiguration(application, environment, configuration) {
    return new Promise((resolve, reject) => {
        const req = http.get(`http://localhost:2772/applications/${application}/environments/${environment}/configurations/${configuration}`, (res) => {
            if (res.statusCode < 200 || res.statusCode >= 300) {
                return reject(new Error('statusCode=' + res.statusCode));
            }
            var body = [];
            res.on('data', function(chunk) {
                body.push(chunk);
            });
            res.on('end', function() {
                resolve(Buffer.concat(body).toString());
            });
        });
        req.on('error', (e) => {
            reject(e.message);
        });
        req.end();
    });
}

exports.handler = async (event) => {
  try {
    const configData = await getConfiguration(AppConfigApplication, AppConfigEnvironment, AppConfigConfiguration);    
    const parsedConfigData = JSON.parse(configData);
    console.log(parsedConfigData);

    if(parsedConfigData.newProductRolloutFlag && parsedConfigData.newProductRolloutStates.includes("IL")) {
        console.log("Running newProduct feature");
        /*
        NEW PRODUCT ROLLOUT IMPLEMENTATION
        */
    }
  } catch (err) {
      console.error(err);
      return err;
  } 
}; 
Enter fullscreen mode Exit fullscreen mode

Above example show cased the Seperation of Configuration data from source code as well as how Lambda immediately picks up changes to configuration data from AWS AppConfig.

Image by Gino Crescoli from Pixabay

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay