Skip to content

Runtime Configuration

Runtime configuration is the mechanism used by Nx Plugin for AWS to pass deploy-time values between generated projects and components so they can connect to one another. For example, when you generate an API, its URL is automatically registered in the runtime configuration so that a connected website can discover it.

Runtime configuration is organised into namespaces. Each namespace is a logical grouping of related configuration values. At deploy time, all namespaces are stored in AWS AppConfig as Configuration Profiles.

The built-in connection namespace is used for configuration that enables generated projects to connect to each other, for example:

  • API URLs — registered automatically by API constructs
  • Cognito settings — registered automatically by the UserIdentity construct
  • Agent runtime ARNs — registered automatically by agent constructs

The connection namespace is also deployed as a runtime-config.json file to your website’s S3 bucket, enabling client-side discovery of backend resources.

You can define as many additional namespaces as you like, providing a convenient alternative to environment variables for passing deploy-time values such as DynamoDB table names to your Lambda functions or other compute resources.

Generated constructs automatically write relevant configuration to the connection namespace. You can also write your own values to any namespace.

The RuntimeConfig CDK construct is a stage-scoped singleton. Use set() to write a key into a namespace:

packages/infra/src/stacks/application-stack.ts
import { RuntimeConfig } from ':my-scope/common-constructs';
const rc = RuntimeConfig.ensure(this);
// Built-in 'connection' namespace (written automatically by generated constructs)
rc.set('connection', 'apis', {
...rc.get('connection').apis,
MyApi: api.url,
});
// Custom namespaces for server-side configuration
rc.set('tables', 'users', {
tableName: usersTable.tableName,
tableArn: usersTable.tableArn,
});

At synth/deploy time, RuntimeConfig creates an AWS AppConfig application containing:

  • A Configuration Profile for each namespace
  • A Hosted Configuration Version with the JSON data for each profile
  • An instant Deployment to the default environment

Server-side consumers need the AppConfig Application ID and IAM permissions to read configuration at runtime. Generated constructs handle this automatically.

Use appConfigApplicationId to get the AppConfig Application ID, and grantReadAppConfig() to grant read permissions:

packages/infra/src/stacks/application-stack.ts
const rc = RuntimeConfig.ensure(this);
// Get the AppConfig Application ID (lazy token, resolved at synth time)
const appId = rc.appConfigApplicationId;
// Pass it as an environment variable to a Lambda function
const myFunction = new Function(this, 'MyFunction', {
// ...
environment: {
RUNTIME_CONFIG_APP_ID: appId,
},
});
// Grant the function permission to read from AppConfig
rc.grantReadAppConfig(myFunction);

Server-side consumers such as Lambda functions and agents can retrieve runtime configuration from AWS AppConfig using AWS Lambda Powertools.

All generated API and agent constructs are automatically configured with:

  • The RUNTIME_CONFIG_APP_ID environment variable (the AppConfig Application ID)
  • IAM permissions to read from AppConfig

Use getAppConfig from @aws-lambda-powertools/parameters:

import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
// Retrieve the 'connection' namespace as a parsed JSON object
const config = await getAppConfig('connection', {
application: process.env.RUNTIME_CONFIG_APP_ID!,
environment: 'default',
transform: 'json',
});
// Access values
const apiUrl = config.apis?.MyApi;
const cognitoProps = config.cognitoProps;

You can also retrieve custom namespaces:

// Retrieve a custom 'tables' namespace
const tablesConfig = await getAppConfig('tables', {
application: process.env.RUNTIME_CONFIG_APP_ID!,
environment: 'default',
transform: 'json',
});
const usersTableName = tablesConfig.users?.tableName;

For websites, the connection namespace is deployed as a runtime-config.json file to the S3 bucket. See the React Website Runtime Configuration guide for details on how to access these values from your frontend code.