ENV
ENV

Reputation: 1292

How to access Feature Flags from AWS AppConfig with TypeScript project?

I'm encountering an issue with my TypeScript application using the AWS SDK. My goal is to get the Feature Flag I created from my AWS AppConfig. The error message I'm seeing is:

Error getting AppConfig configuration: Error: connect ECONNREFUSED 169.254.169.254:80
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
  errno: -111,
  code: 'CredentialsError',
  syscall: 'connect',
  address: '169.254.169.254',
  port: 80,
  time: 2023-12-14T16:21:24.678Z,
  originalError: {
    message: 'Could not load credentials from any providers',
    errno: -111,
    code: 'CredentialsError',
    syscall: 'connect',
    address: '169.254.169.254',
    port: 80,
    time: 2023-12-14T16:21:24.678Z,
    originalError: {
      message: 'EC2 Metadata roleName request returned error',
      errno: -111,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '169.254.169.254',
      port: 80,
      time: 2023-12-14T16:21:24.677Z,
      originalError: [Object]
    }
  }
}

The code that I have is the following:

import { Request, Response, NextFunction } from 'express';
import AWS from "aws-sdk";
const { AppConfig } = AWS;

const appConfig = new AppConfig({region: "us-east-1"});


const params = {
    Application: '**********', 
    Configuration: '**********', 
    Environment: '**********',
    ClientId: "**********"
  };

  
const response = await appConfig.getConfiguration(params).promise();
const featureFlags: { [key: string]: boolean } = JSON.parse(response.Content!.toString());
    return featureFlags[featureName];
  } catch (error) {
console.error('Error getting AppConfig configuration:', error);
throw error;
  }

The approach I'm using here aligns with the AWS documentation, but I'm wondering if there's an alternative approach. If there is another way to get the Feature Flags from the AWS AppConfig, I'll gladly follow those steps. For now, I'm not sure what the issue is. The configs that I've provided are directly from AWS so they should all be accurate. Is there anything I need to enable in the AWS AppConfig to be able to access its Feature Flags that I created? Or is there an alternative approach?

Any assistance on this would be much appreciated!

Upvotes: 0

Views: 586

Answers (1)

Pierre Siza
Pierre Siza

Reputation: 5

If you are looking to use feature flags with Typescript in an easier way, and also have more flexibility for the future than AppConfig, give getjoystick.com a try. We have used them for several projects. They have a really good management interface for your feature flags / configs. Team is also very responsive on Discord.

// Import the Joystick Remote Configuration package.
import { Joystick } from "@getjoystick/joystick-js";

// Initialize
const joystickClient = new Joystick({
    apiKey: process.env.JOYSTICK_API_KEY,
});

async function myNewFeatureA(options) {
    console.log(options);
    // Get full JSON feature flags / configs
    // {
    //    "color": "green",
    //    "size": 100,
    //    "bold": true,
    //    "text": "Use Remote Configuration!"
    // }
}

// Get the Feature Flag configuration from Joystick. What you get is already parsed.
const featureFlagsConfig = await joystickClient.getContent<YourFeatureFlagsConfigType>("feature-flags-config");

if (featureFlagsConfig.newFeatureA.enabled) {
    // Call the function with options that were retrieved from Remote Configuration.
    await myNewFeatureA(featureFlagsConfig.newFeatureA.options);
}

Full Typescript docs here: https://docs.getjoystick.com/sdk-joystick-js/

Upvotes: -2

Related Questions