Package to read parameters from AWS System Manager (SSM) parameter store. We can use "@nestjs/config" config service to read parameters from file and environment variables. And use paramStoreService to read environment specific parameters like password or endpoint URL on application start up.
Module exports 'ParamStoreService' to access downloaded parameters. Service has method 'get' to read property.
Example:
paramStoreService.get('nameOfProperty');
Note:
- If region (awsRegion) is not provided it defaults to 'us-east-1'.
- awsParamSorePath is required.
npm i param-store-service
Two ways to configure param-store-service module:
- Static configuration
@Module({ imports: [ ParamStoreModule.register({ awsRegion: 'us-east-1', awsParamSorePath: '/application/config', }), ], controllers: [AppController], providers: [AppService], }) export class AppModule { constructor( private paramStoreService: ParamStoreService, ) { console.log('name', paramStoreService.get('name')); } }
- Async configuration using config service
- Config service required properties:
- param-store.awsRegion
- param-store.awsParamStorePath
- Config service required properties:
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [configServiceParamStoreConfiguration],
envFilePath: `config/${process.env.NODE_ENV || 'development'}.env`,
validate,
}),
ParamStoreModule.registerAsync({
import: ConfigModule,
useClass: ConfigService,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {
constructor(
private paramStoreService: ParamStoreService,
) {
console.log('name', paramStoreService.get('name'));
}
}
Configuration to load for Config service
import { registerAs } from '@nestjs/config';
export default registerAs('param-store', () => ({
awsRegion: process.env.AWS_REGION,
awsParamStorePath: process.env.AWS_PARAM_STORE_PATH,
}));
Validator (Optional)
import { plainToClass } from 'class-transformer';
import { IsEnum, IsNumber, IsString, validateSync } from 'class-validator';
enum Environment {
Development = 'development',
Production = 'production',
Test = 'test',
}
class EnvironmentVariables {
@IsEnum(Environment)
NODE_ENV: Environment = Environment.Test;
@IsNumber()
PORT = 3000;
@IsString()
AWS_REGION = 'us-east-1';
@IsString()
AWS_PARAM_STORE_PATH: string;
}
export function validate(config: Record<string, unknown>) {
const validatedConfig = plainToClass(EnvironmentVariables, config, {
enableImplicitConversion: true,
});
const errors = validateSync(validatedConfig, {
skipMissingProperties: false,
});
if (errors.length > 0) {
throw new Error(errors.toString());
}
return validatedConfig;
}
.env file
message='hello'
name='Old name'
description='Hello there!'
AWS_REGION='us-east-1'
AWS_PARAM_STORE_PATH='/application/config'
Contributions welcome!
PM
Licensed under the MIT License.