|
| 1 | +<!-- PROJECT LOGO --> |
| 2 | +<br /> |
| 3 | +<div align="center"> |
| 4 | +<h2>nestjs-parameter-store</h2> |
1 | 5 |
|
| 6 | + <p align="center"> |
| 7 | + NestJS AWS Parameter Store |
| 8 | + <br> |
| 9 | + Use nestjs managed <a href="https://docs.aws.amazon.com/ko_kr/systems-manager/latest/userguide/what-is-systems-manager.html">AWS Systems Manager</a>. |
| 10 | + </p> |
| 11 | +</div> |
| 12 | + |
| 13 | +<br> |
| 14 | + |
| 15 | +<!-- TABLE OF CONTENTS --> |
| 16 | + |
| 17 | +## Table of Contents |
| 18 | + |
| 19 | +<ol> |
| 20 | + <li><a href="#installation">Installation</a></li> |
| 21 | + <li><a href="#configuration">Configuration</a></li> |
| 22 | + <li><a href="#service">Service</a></li> |
| 23 | + <li><a href="#license">License</a></li> |
| 24 | +</ol> |
| 25 | + |
| 26 | +<!-- INSTALLATION --> |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +```sh |
| 31 | +npm install nestjs-parameter-store @aws-sdk/client-ssm |
| 32 | + |
| 33 | +pnpm install nestjs-parameter-store @aws-sdk/client-ssm |
| 34 | +``` |
| 35 | + |
| 36 | +## Configuration |
| 37 | + |
| 38 | +Configure the module `forRoot()` or `forRootAsync()` to access all the |
| 39 | +AWS System Manager service in production. |
| 40 | + |
| 41 | +Configure the module `registerParamStore()` or `registerParamStoreAsync()` to |
| 42 | +loaded all the parameters in AWS Parameter Store in production using `@Inject(GET_PARAMETERS)` |
| 43 | + |
| 44 | +### Static configuration |
| 45 | + |
| 46 | +```typescript |
| 47 | +import {Module} from "@nestjs/common"; |
| 48 | +import {NestjsParameterStoreModule} from "nestjs-parameter-store"; |
| 49 | + |
| 50 | +@Module({ |
| 51 | + imports: [ |
| 52 | + NestjsParameterStoreModule.forRoot({region: "region"}), |
| 53 | + NestjsParameterStoreModule.registerParamStore({ |
| 54 | + Path: "/test", |
| 55 | + Recursive: true, |
| 56 | + WithDecryption: true, |
| 57 | + }), |
| 58 | + ], |
| 59 | +}) |
| 60 | +export class AppModule { |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Async configuration |
| 65 | + |
| 66 | +```typescript |
| 67 | +import {Module} from "@nestjs/common"; |
| 68 | +import {NestjsParameterStoreModule} from "nestjs-parameter-store"; |
| 69 | + |
| 70 | +@Module({ |
| 71 | + imports: [ |
| 72 | + NestjsParameterStoreModule.forRootAsync({ |
| 73 | + imports: [ConfigModule], |
| 74 | + inject: [ConfigService], |
| 75 | + useFactory: async (config: ConfigService) => { |
| 76 | + const {region, accessKeyId, secretAccessKey} = config.get("aws"); |
| 77 | + return {region, accessKeyId, secretAccessKey}; |
| 78 | + }, |
| 79 | + }), |
| 80 | + NestjsParameterStoreModule.registerParamStoreAsync({ |
| 81 | + imports: [ConfigModule], |
| 82 | + inject: [ConfigService], |
| 83 | + useFactory: async (config: ConfigService) => { |
| 84 | + const {path} = config.get("aws-param-store"); |
| 85 | + return {Path: path, Recursive: true, WithDecryption: true}; |
| 86 | + }, |
| 87 | + }), |
| 88 | + ], |
| 89 | +}) |
| 90 | +export class AppModule { |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Service |
| 95 | + |
| 96 | +This module exposes the following services. |
| 97 | + |
| 98 | +### AwsParamStoreService |
| 99 | + |
| 100 | +The `AwsParamStoreService` allows you to access the configuration loaded from AWS Parameter Store. Use its own class |
| 101 | +name as the injection token. |
| 102 | + |
| 103 | +The `AwsParamStoreService` exposes the following methods: |
| 104 | + |
| 105 | +- getParameter({ Name: String, WithDecryption: Boolean }) |
| 106 | +- getParameters({ Name: String[], WithDecryption: Boolean }) |
| 107 | +- getParametersByPath({ Path: String, Recursive: true, WithDecryption: true }) |
| 108 | + |
| 109 | +```typescript |
| 110 | +import {Injectable} from "@nestjs/common"; |
| 111 | +import {AwsParamStoreService} from "nestjs-parameter-store"; |
| 112 | + |
| 113 | +@Injectable() |
| 114 | +export class TestService { |
| 115 | + constructor(private readonly awsParameterStore: AwsParameterStore) { |
| 116 | + console.log(awsParamStoreService.getParameter({Name: "/test/parameter"})); |
| 117 | + console.log( |
| 118 | + awsParamStoreService.getParameters({ |
| 119 | + Names: ["/test/parameter", "/test/secure"], |
| 120 | + WithDecryption: true, |
| 121 | + }), |
| 122 | + ); |
| 123 | + console.log( |
| 124 | + awsParamStoreService.getParametersByPath({ |
| 125 | + Path: "/test", |
| 126 | + Recursive: true, |
| 127 | + WithDecryption: true, |
| 128 | + OnlyValue: true, |
| 129 | + }), |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### GET_PARAMETERS |
| 136 | + |
| 137 | +You can access the parameters loaded from the Parameter Store by configuration `registerParamStore()` |
| 138 | +or `registerParamStoreAsync()` |
| 139 | + |
| 140 | +`@Inject(GET_PARAMETERS)` is functionally the same as `getParametersByPath()`. |
| 141 | + |
| 142 | +```typescript |
| 143 | +import {Inject, Injectable} from "@nestjs/common"; |
| 144 | +import {GET_PARAMETERS} from "nestjs-parameter-store"; |
| 145 | +import {Parameter} from "@aws-sdk/client-ssm"; |
| 146 | + |
| 147 | +@Injectable() |
| 148 | +export class ParameterStoreService { |
| 149 | + constructor(@Inject(GET_PARAMETERS) private readonly parameters: Parameter[]) { |
| 150 | + console.log(parameters) |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
0 commit comments