An open API service indexing awesome lists of open source software.

https://github.com/essential2189/nestjs-parameter-store

NestJS Parameter Store
https://github.com/essential2189/nestjs-parameter-store

aws nestjs param-store parameter-store

Last synced: 10 days ago
JSON representation

NestJS Parameter Store

Awesome Lists containing this project

README

          




nestjs-parameter-store


NestJS AWS Parameter Store · npm version


Use nestjs managed AWS Systems Manager.



## Table of Contents


  1. Installation

  2. Configuration

  3. Service

  4. License

## Installation

```sh
npm install nestjs-parameter-store @aws-sdk/client-ssm

pnpm install nestjs-parameter-store @aws-sdk/client-ssm
```

## Configuration

Configure the module `forRoot()` or `forRootAsync()` to access all the
AWS System Manager service in production.

Configure the module `registerParamStore()` or `registerParamStoreAsync()` to
loaded all the parameters in AWS Parameter Store in production using `@Inject(GET_PARAMETERS)`

### Static configuration

```typescript
import {Module} from "@nestjs/common";
import {NestjsParameterStoreModule} from "nestjs-parameter-store";

@Module({
imports: [
NestjsParameterStoreModule.forRoot({region: "region"}),
NestjsParameterStoreModule.registerParamStore({
Path: "/test",
Recursive: true,
WithDecryption: true,
}),
],
})
export class AppModule {
}
```

### Async configuration

```typescript
import {Module} from "@nestjs/common";
import {NestjsParameterStoreModule} from "nestjs-parameter-store";

@Module({
imports: [
NestjsParameterStoreModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
const {region, accessKeyId, secretAccessKey} = config.get("aws");
return {region, accessKeyId, secretAccessKey};
},
}),
NestjsParameterStoreModule.registerParamStoreAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
const {path} = config.get("aws-param-store");
return {Path: path, Recursive: true, WithDecryption: true};
},
}),
],
})
export class AppModule {
}
```

## Service

This module exposes the following services.

### AwsParamStoreService

The `AwsParamStoreService` allows you to access the configuration loaded from AWS Parameter Store. Use its own class
name as the injection token.

The `AwsParamStoreService` exposes the following methods:

- getParameter({ Name: String, WithDecryption: Boolean })
- getParameters({ Name: String[], WithDecryption: Boolean })
- getParametersByPath({ Path: String, Recursive: boolean, WithDecryption: boolean, OnlyValue: boolean })

You can use `OnlyValue` option to get the object like `{ [parameter name]: [value], ... }`.
```typescript
import {Injectable} from "@nestjs/common";
import {AwsParamStoreService} from "nestjs-parameter-store";

@Injectable()
export class TestService {
constructor(private readonly awsParameterStore: AwsParameterStore) {
console.log(awsParamStoreService.getParameter({Name: "/test/parameter"}));
console.log(
awsParamStoreService.getParameters({
Names: ["/test/parameter", "/test/secure"],
WithDecryption: true,
}),
);
console.log(
awsParamStoreService.getParametersByPath({
Path: "/test",
Recursive: true,
WithDecryption: true,
OnlyValue: true,
}),
);
}
}
```

### GET_PARAMETERS

You can access the parameters loaded from the Parameter Store by configuration `registerParamStore()`
or `registerParamStoreAsync()`

`@Inject(GET_PARAMETERS)` is functionally the same as `getParametersByPath()`.

```typescript
import {Inject, Injectable} from "@nestjs/common";
import {GET_PARAMETERS} from "nestjs-parameter-store";
import {Parameter} from "@aws-sdk/client-ssm";

@Injectable()
export class ParameterStoreService {
constructor(@Inject(GET_PARAMETERS) private readonly parameters: Parameter[]) {
console.log(parameters)
}
}
```