DEV Community

Marcos Henrique
Marcos Henrique

Posted on

Using AppConfig Layer in your nodejs lambda with AWS CDK

The biggest problem with conventional configuration management is that the client depends on the server for config updates. This puts you at the mercy of your network stack and having to coordinate with an ops team to update configurations. AWS App Config is a managed service that attempts to solve this and other problems.

What is AWS App Config?

Use AWS AppConfig, a capability of AWS Systems Manager, to create, manage, and quickly deploy application configurations. A configuration is a collection of settings that influence the behavior of your application. You can use AWS AppConfig with applications hosted on Amazon Elastic Compute Cloud (Amazon EC2) instances, AWS Lambda, containers, mobile applications, or IoT devices.

AWS AppConfig use cases

AWS AppConfig can help you in the following use cases:

Application tuning – Introduce changes carefully to your application that can be tested with production traffic.
Feature toggle – Turn on new features that require a timely deployment, such as a product launch or announcement.
Allow list – Allow premium subscribers to access paid content.
Operational issues – Reduce stress on your application when a dependency or other external factor impacts the system.

If you want to deep dive into AppConfig you can read more about this here.

What is a lambda layer?

A Lambda layer is a .zip file archive that can contain additional code or other content. A layer can contain libraries, a custom runtime, data, or configuration files. Use layers to reduce deployment package size and to promote code sharing and separation of responsibilities so that you can iterate faster on writing business logic.

You can use some of lambda extensions to power up your lambda, an extension can be a layer as the app config layer for example, lambda supports external and internal extensions.
An external extension runs as an independent process in the execution environment and continues to run after the function invocation is fully processed. Because extensions run as separate processes, you can write them in a different language than the function. All Lambda runtimes support extensions.

after this brief introduction and recap, we can Bust one's buns xD

Let's Code

a woman dancing in front of her computer
In this example I'm going to use nodejs with typescript, but you can use another runtime if you want.

So we gonna create our node function class to export and abstract the configuration

The NodeFunction Class



import { Alias, Architecture, ILayerVersion, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda'

export interface INodeFunctionProps extends NodejsFunctionProps {
  layers: ILayerVersion[]
  environment: Record<string, string>
} 
export class NodeFunction extends NodejsFunction {
  readonly alias: Alias

  constructor(scope: Construct, id: string, props: INodeFunctionProps) {
    const defaultProps: NodejsFunctionProps = {
      timeout: Duration.seconds(300),
      runtime: Runtime.NODEJS_16_X,
      environment: {
        NODE_OPTIONS: '--enable-source-maps',
        AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
        ...props.environment
      },
      bundling: {
        externalModules: [
          // Use the 'aws-sdk' available in the Lambda runtime
          'aws-sdk',
        ],
        target: 'es2021',
        logLevel: LogLevel.ERROR,
        minify: true,
        keepNames: true,
        sourceMap: false,
      },
      architecture: Architecture.ARM_64,
      logRetention: isProduction ? RetentionDays.ONE_YEAR : RetentionDays.ONE_WEEK,
      tracing: Tracing.ACTIVE,
      layers: [...props.layers]
    }

    super(scope, id, defaultProps)

    this.alias = new Alias(this, id.concat('alias'), {
      version: this.currentVersion,
      aliasName: 'current',
    })
  }
}


Enter fullscreen mode Exit fullscreen mode

First of all we need to specify your lambda layer, AWS has some versions of the AWS AppConfig Lambda extension, in this case I'll use the ARM-64 layer because in the following code I'm using Graviton2 to improve the performance of my nodejs lambda.



//
const appConfig = LayerVersion.fromLayerVersionArn(
      this,
      `my-first-app-config-layer`,
      'arn:aws:lambda:us-east-1:027255383542:layer:AWS-AppConfig-Extension-Arm64:15', // app config extension arn
    )
new NodeFunction(this, 'my-lambda-with-appconfig', {
  layers: [appConfig],
  environment: {
   AWS_APPCONFIG_EXTENSION_PREFETCH_LIST: '/applications/MyApp/environments/Default/configurations/v1' //here you need to specify your app config created on console
  }
})


Enter fullscreen mode Exit fullscreen mode

When you deploy your lambda you will see something like that on cloudwatch:

cloudwatch with logs of app config loaded

That's all folks, enjoy your app config!

Top comments (0)