Create Lambda to fetch the freeform feature flags

  • Last Modified: 20 Mar, 2022

Step 01: Create a Role for the lambda to access AppConfig.

For this tutorial, I will assign everything to all resources, but in the real world, you will need to follow the least privileged concept.


Get Yours Today

Discover our wide range of products designed for IT professionals. From stylish t-shirts to cutting-edge tech gadgets, we've got you covered.

Explore Our Collection 🚀


Step 01: Create a Role for the lambda to access AppConfig.

For this tutorial, I will assign everything to all resources, but in the real world, you will need to follow the least privileged concept.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "logs:CreateLogGroup",
            "Resource": "arn:aws:logs:us-east-1:<account-id>:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:us-east-1:<account-id>:log-group:/aws/lambda/test:*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": "appconfig:*",
            "Resource": "*"
        }
    ]
}

Step 02: Create a new Lambda function and assign the above Created IAM Role

Step 03: Add a new Layer to the Lambda function

Step 04: Add the following code to the Lamda function to read the config

const http = require('http');
exports.handler = async (event) => {
 
 const APP_CONFIG_PORT = 2772;
 const APP_CONFIG_NAME = 'MyCloudNativeApp';
 const APP_CONFIG_ENV = 'dev';
 const APP_CONFIG_CONFIG_PROFILE = 'CloudNativeFreeForm-AppConfigHosted';
 
 let url = `http://localhost:${APP_CONFIG_PORT}/applications/${APP_CONFIG_NAME}/environments/${APP_CONFIG_ENV}/configurations/${APP_CONFIG_CONFIG_PROFILE}`;
console.log(url); 
 const res = await new Promise((resolve, reject) => {
    http.get(
      url,
      resolve
    );
  });
  
  let configData = await new Promise((resolve, reject) => {
    let data = '';
    res.on('data', chunk => data += chunk);
    res.on('error', err => reject(err));
    res.on('end', () => resolve(data));
  });
console.log(configData);
  
  const parsedConfigData = JSON.parse(configData);
         
  const response = {
    statusCode: 200,
    body: parsedConfigData,
  };
  return response;
};

Step 05: Run the Lambda and you can see the fetched features.

...
Get Yours Today

Discover our wide range of products designed for IT professionals. From stylish t-shirts to cutting-edge tech gadgets, we've got you covered.

Explore Our Collection 🚀


See Also

comments powered by Disqus