@aws-blocks/bb-app-setting 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +174 -0
- package/README.md +156 -0
- package/dist/errors.d.ts +25 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +26 -0
- package/dist/index.aws.d.ts +76 -0
- package/dist/index.aws.d.ts.map +1 -0
- package/dist/index.aws.js +135 -0
- package/dist/index.browser.d.ts +5 -0
- package/dist/index.browser.d.ts.map +1 -0
- package/dist/index.browser.js +9 -0
- package/dist/index.cdk.d.ts +35 -0
- package/dist/index.cdk.d.ts.map +1 -0
- package/dist/index.cdk.js +242 -0
- package/dist/index.cdk.test.d.ts +2 -0
- package/dist/index.cdk.test.d.ts.map +1 -0
- package/dist/index.cdk.test.js +187 -0
- package/dist/index.hooks.d.ts +2 -0
- package/dist/index.hooks.d.ts.map +1 -0
- package/dist/index.hooks.js +3 -0
- package/dist/index.mock.d.ts +77 -0
- package/dist/index.mock.d.ts.map +1 -0
- package/dist/index.mock.js +153 -0
- package/dist/index.test.d.ts +2 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/index.test.js +542 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/package.json +46 -0
- package/src/errors.ts +27 -0
- package/src/index.aws.ts +165 -0
- package/src/index.browser.ts +10 -0
- package/src/index.cdk.test.ts +227 -0
- package/src/index.cdk.ts +291 -0
- package/src/index.hooks.ts +5 -0
- package/src/index.mock.ts +181 -0
- package/src/index.test.ts +627 -0
- package/src/types.ts +56 -0
- package/src/version.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# AppSetting
|
|
2
|
+
|
|
3
|
+
A single application configuration value backed by SSM Parameter Store.
|
|
4
|
+
|
|
5
|
+
**When to use:** You need a configuration value (feature flag, API URL, threshold) or a secret (API key, token) stored as a single SSM parameter. Each `AppSetting` instance maps to exactly one parameter.
|
|
6
|
+
|
|
7
|
+
**When NOT to use:** If you need structured key-value data with conditional writes and queries, use `KVStore` or `DistributedTable`.
|
|
8
|
+
|
|
9
|
+
## API
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
const setting = new AppSetting(scope, id, options)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
| Method | Returns | Description |
|
|
16
|
+
|--------|---------|-------------|
|
|
17
|
+
| `get()` | `Promise<T>` | Read the current value. |
|
|
18
|
+
| `put(value)` | `Promise<void>` | Update the value at runtime. |
|
|
19
|
+
|
|
20
|
+
### Options
|
|
21
|
+
|
|
22
|
+
| Option | Type | Required | Description |
|
|
23
|
+
|--------|------|----------|-------------|
|
|
24
|
+
| `name` | `string` | No | SSM parameter path. When omitted, derived from the scope tree as `/${fullId}`, guaranteeing uniqueness within the stack. |
|
|
25
|
+
| `value` | `T` | No | Initial value. Required for non-secret parameters. Must not be provided when `secret` is set. |
|
|
26
|
+
| `schema` | `StandardSchemaV1<T>` | No | Runtime validation schema (Zod, Valibot, ArkType). Infers `T` from the schema. Cannot be used with `secret`. |
|
|
27
|
+
| `secret` | `boolean` | No | When `true`, creates an SSM SecureString encrypted with the `aws/ssm` KMS key. Cannot be used with `schema` or `value`. |
|
|
28
|
+
| `logger` | `ChildLogger` | No | Optional logger for internal operations. When omitted, a default Logger at error level is created. |
|
|
29
|
+
|
|
30
|
+
> **Naming:** When you omit `name`, the framework derives a unique SSM path from
|
|
31
|
+
> the construct scope tree (`/${fullId}`). This is the recommended approach — it
|
|
32
|
+
> prevents collisions across stacks automatically. If you provide an explicit
|
|
33
|
+
> `name`, **you** are responsible for ensuring it is unique across all stacks
|
|
34
|
+
> deployed to the same AWS account and region.
|
|
35
|
+
|
|
36
|
+
### Error Handling
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { isBlocksError } from '@aws-blocks/core';
|
|
40
|
+
import { AppSettingErrors } from '@aws-blocks/bb-app-setting';
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
await setting.put(value);
|
|
44
|
+
} catch (e: unknown) {
|
|
45
|
+
if (isBlocksError(e, AppSettingErrors.ValidationFailed)) {
|
|
46
|
+
// schema validation failed or value exceeds 4 KB
|
|
47
|
+
}
|
|
48
|
+
throw e;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const value = await setting.get();
|
|
53
|
+
} catch (e: unknown) {
|
|
54
|
+
if (isBlocksError(e, AppSettingErrors.ParameterNotFound)) {
|
|
55
|
+
// the parameter is missing (e.g., deleted out-of-band) or a secret has an empty value
|
|
56
|
+
}
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`get()` can throw `AppSettingErrors.ParameterNotFound` when the parameter does not exist or a secret parameter has an empty value (in both the AWS and mock runtimes).
|
|
62
|
+
|
|
63
|
+
## Examples
|
|
64
|
+
|
|
65
|
+
### Plain String Setting
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// Recommended: let the framework manage the SSM name (unique by default)
|
|
69
|
+
const apiUrl = new AppSetting(scope, 'apiUrl', {
|
|
70
|
+
value: 'https://api.example.com',
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export const api = new ApiNamespace(scope, 'api', (context) => ({
|
|
74
|
+
async getApiUrl() {
|
|
75
|
+
return { url: await apiUrl.get() };
|
|
76
|
+
},
|
|
77
|
+
async setApiUrl(url: string) {
|
|
78
|
+
await apiUrl.put(url);
|
|
79
|
+
},
|
|
80
|
+
}));
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Typed Object with Schema
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { z } from 'zod';
|
|
87
|
+
|
|
88
|
+
const config = new AppSetting(scope, 'config', {
|
|
89
|
+
value: { maxRetries: 3, timeout: 5000 },
|
|
90
|
+
schema: z.object({ maxRetries: z.number(), timeout: z.number() }),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
export const api = new ApiNamespace(scope, 'api', (context) => ({
|
|
94
|
+
async getConfig() {
|
|
95
|
+
return await config.get(); // { maxRetries: number; timeout: number }
|
|
96
|
+
},
|
|
97
|
+
async updateConfig(cfg: { maxRetries: number; timeout: number }) {
|
|
98
|
+
await config.put(cfg); // validated against schema
|
|
99
|
+
},
|
|
100
|
+
}));
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Secret
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
const stripeKey = new AppSetting(scope, 'stripeKey', {
|
|
107
|
+
secret: true,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
export const api = new ApiNamespace(scope, 'api', (context) => ({
|
|
111
|
+
async charge(amount: number) {
|
|
112
|
+
const key = await stripeKey.get(); // decrypted from SSM SecureString
|
|
113
|
+
// use key...
|
|
114
|
+
},
|
|
115
|
+
}));
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Explicit Name (Cross-Service Reads)
|
|
119
|
+
|
|
120
|
+
Use an explicit `name` when the SSM parameter must be read by other services or follow a well-known path convention.
|
|
121
|
+
|
|
122
|
+
> ⚠️ You must ensure this name is unique across all stacks deployed to the
|
|
123
|
+
> same AWS account and region to avoid collisions.
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
const apiUrl = new AppSetting(scope, 'apiUrl', {
|
|
127
|
+
name: '/my-app/production/api-url',
|
|
128
|
+
value: 'https://api.example.com',
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Validation Rules
|
|
133
|
+
|
|
134
|
+
These are enforced at CDK synth time:
|
|
135
|
+
|
|
136
|
+
- `secret` + `schema` → error (secrets are plain strings)
|
|
137
|
+
- `schema` without `value` → error (parameter needs a valid initial value)
|
|
138
|
+
- `secret` with `value` → error (don't put secrets in source code)
|
|
139
|
+
- Non-secret without `value` → error (settings require an initial value)
|
|
140
|
+
|
|
141
|
+
## Scaling & Cost (AWS)
|
|
142
|
+
|
|
143
|
+
- **String parameters:** Standard tier, free, 4 KB limit, 10,000 per account/region
|
|
144
|
+
- **SecureString parameters:** Standard tier, free, encrypted with `aws/ssm` KMS key
|
|
145
|
+
- **Throughput:** 40 TPS default for GetParameter (can be increased)
|
|
146
|
+
- **Latency:** Single-digit ms reads and writes
|
|
147
|
+
|
|
148
|
+
## Local Development
|
|
149
|
+
|
|
150
|
+
Mock data persists to disk at `.bb-data/settings/{fullId}/value.json` across dev server restarts. Wipe with `rm -rf .bb-data`. Secrets generate a random value locally (no KMS encryption in mock mode).
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
## See Also
|
|
155
|
+
|
|
156
|
+
- [DESIGN.md](./DESIGN.md) — Infrastructure details, mock parity gaps, and interface decisions
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed error constants for AppSetting. Use with `isBlocksError()` in catch blocks.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { isBlocksError } from '@aws-blocks/core';
|
|
7
|
+
* import { AppSettingErrors } from '@aws-blocks/bb-app-setting';
|
|
8
|
+
*
|
|
9
|
+
* try {
|
|
10
|
+
* await setting.put(value);
|
|
11
|
+
* } catch (e: unknown) {
|
|
12
|
+
* if (isBlocksError(e, AppSettingErrors.ValidationFailed)) {
|
|
13
|
+
* // schema validation failed or value exceeds 4 KB
|
|
14
|
+
* }
|
|
15
|
+
* throw e;
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare const AppSettingErrors: {
|
|
20
|
+
/** Thrown when the SSM parameter does not exist. */
|
|
21
|
+
readonly ParameterNotFound: "ParameterNotFoundException";
|
|
22
|
+
/** Thrown when schema validation fails, value exceeds 4 KB, or options are invalid. */
|
|
23
|
+
readonly ValidationFailed: "ValidationFailedException";
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB;IAC5B,oDAAoD;;IAEpD,uFAAuF;;CAE9E,CAAC"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* Typed error constants for AppSetting. Use with `isBlocksError()` in catch blocks.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { isBlocksError } from '@aws-blocks/core';
|
|
9
|
+
* import { AppSettingErrors } from '@aws-blocks/bb-app-setting';
|
|
10
|
+
*
|
|
11
|
+
* try {
|
|
12
|
+
* await setting.put(value);
|
|
13
|
+
* } catch (e: unknown) {
|
|
14
|
+
* if (isBlocksError(e, AppSettingErrors.ValidationFailed)) {
|
|
15
|
+
* // schema validation failed or value exceeds 4 KB
|
|
16
|
+
* }
|
|
17
|
+
* throw e;
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export const AppSettingErrors = {
|
|
22
|
+
/** Thrown when the SSM parameter does not exist. */
|
|
23
|
+
ParameterNotFound: 'ParameterNotFoundException',
|
|
24
|
+
/** Thrown when schema validation fails, value exceeds 4 KB, or options are invalid. */
|
|
25
|
+
ValidationFailed: 'ValidationFailedException',
|
|
26
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Scope } from '@aws-blocks/core';
|
|
2
|
+
import type { ScopeParent } from '@aws-blocks/core';
|
|
3
|
+
import type { AppSettingOptions } from './types.js';
|
|
4
|
+
import type { ChildLogger } from '@aws-blocks/bb-logger';
|
|
5
|
+
export { AppSettingErrors } from './errors.js';
|
|
6
|
+
export type { AppSettingOptions } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* A single application configuration value backed by SSM Parameter Store.
|
|
9
|
+
*
|
|
10
|
+
* **When to use:** You need to store and retrieve a non-secret configuration
|
|
11
|
+
* value at runtime — a feature flag, API URL, threshold, or structured config
|
|
12
|
+
* object. For sensitive values, set `secret: true` to use SSM SecureString.
|
|
13
|
+
*
|
|
14
|
+
* **When NOT to use:** If you need structured key-value data with conditional
|
|
15
|
+
* writes and queries, use `KVStore` or `DistributedTable`.
|
|
16
|
+
*
|
|
17
|
+
* **Best practices:**
|
|
18
|
+
* - One AppSetting per logical configuration value
|
|
19
|
+
* - Use a schema for structured objects to get type safety and runtime validation
|
|
20
|
+
* - Use `secret: true` for API keys, tokens, and passwords
|
|
21
|
+
*
|
|
22
|
+
* **Scaling:** Standard-tier SSM parameters. 40 TPS default for GetParameter
|
|
23
|
+
* (can be increased). No cost for standard parameters.
|
|
24
|
+
*/
|
|
25
|
+
export declare class AppSetting<T = string> extends Scope {
|
|
26
|
+
/**
|
|
27
|
+
* Reference an SSM parameter created and owned **outside this stack** (e.g. a
|
|
28
|
+
* connection string seeded by `ensureSecrets` before deploy). At runtime this
|
|
29
|
+
* reads the value like any other setting; the construct-time behavior (no
|
|
30
|
+
* create/seed/tag/delete, read-only grant) is applied by the CDK variant. The
|
|
31
|
+
* factory exists on every variant so app code uses one API across dev/deploy.
|
|
32
|
+
*/
|
|
33
|
+
static fromExisting<T = string>(scope: ScopeParent, id: string, options: {
|
|
34
|
+
name: string;
|
|
35
|
+
secret?: boolean;
|
|
36
|
+
}): AppSetting<T>;
|
|
37
|
+
readonly bbName = "AppSetting";
|
|
38
|
+
private schema?;
|
|
39
|
+
private isSecret;
|
|
40
|
+
private client;
|
|
41
|
+
/** @internal Logger for internal operations. Defaults to error-level when not provided. */
|
|
42
|
+
protected log: ChildLogger;
|
|
43
|
+
constructor(scope: ScopeParent, id: string, options: AppSettingOptions<T>);
|
|
44
|
+
/**
|
|
45
|
+
* Retrieve the current value.
|
|
46
|
+
*
|
|
47
|
+
* Returns the stored SSM parameter value. The parameter is guaranteed
|
|
48
|
+
* to exist because the CDK layer creates it with the initial value.
|
|
49
|
+
*
|
|
50
|
+
* @returns The current value.
|
|
51
|
+
* @throws {AppSettingErrors.ParameterNotFound} If the parameter does not exist in SSM (e.g., deleted out-of-band).
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const retries = await maxRetries.get();
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
get(): Promise<T>;
|
|
59
|
+
/**
|
|
60
|
+
* Update the value at runtime.
|
|
61
|
+
*
|
|
62
|
+
* Overwrites the current SSM parameter value. When a schema is configured,
|
|
63
|
+
* the value is validated before writing.
|
|
64
|
+
*
|
|
65
|
+
* @param value - The new value to store.
|
|
66
|
+
* @throws {AppSettingErrors.ValidationFailed} If schema validation fails or value exceeds 4 KB.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* await maxRetries.put('5');
|
|
71
|
+
* await config.put({ maxRetries: 5, timeout: 10000 });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
put(value: T): Promise<void>;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=index.aws.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.aws.d.ts","sourceRoot":"","sources":["../src/index.aws.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,EAA6C,MAAM,kBAAkB,CAAC;AACpF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGpD,OAAO,KAAK,EAAE,iBAAiB,EAA6B,MAAM,YAAY,CAAC;AAG/E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAqBpD;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,UAAU,CAAC,CAAC,GAAG,MAAM,CAAE,SAAQ,KAAK;IAChD;;;;;;OAMG;IACH,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,MAAM,EAC7B,KAAK,EAAE,WAAW,EAClB,EAAE,EAAE,MAAM,EACV,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,GACzC,UAAU,CAAC,CAAC,CAAC;IAKhB,QAAQ,CAAC,MAAM,gBAAW;IAC1B,OAAO,CAAC,MAAM,CAAC,CAAsB;IACrC,OAAO,CAAC,QAAQ,CAAU;IAC1B,OAAO,CAAC,MAAM,CAAY;IAE1B,2FAA2F;IAC3F,SAAS,CAAC,GAAG,EAAE,WAAW,CAAC;gBAEf,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC,CAAC;IAazE;;;;;;;;;;;;;OAaG;IACG,GAAG,IAAI,OAAO,CAAC,CAAC,CAAC;IA2BvB;;;;;;;;;;;;;;OAcG;IACG,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAYlC"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { SSMClient, GetParameterCommand, PutParameterCommand, } from '@aws-sdk/client-ssm';
|
|
4
|
+
import { Scope, registerSdkIdentifiers, getSdkIdentifiers } from '@aws-blocks/core';
|
|
5
|
+
import { AppSettingErrors } from './errors.js';
|
|
6
|
+
import { BB_NAME, BB_VERSION } from './version.js';
|
|
7
|
+
import { Logger } from '@aws-blocks/bb-logger';
|
|
8
|
+
// Re-export public types from types module (canonical source)
|
|
9
|
+
export { AppSettingErrors } from './errors.js';
|
|
10
|
+
// ── Helpers ─────────────────────────────────────────────────────────────────
|
|
11
|
+
function blocksError(name, message) {
|
|
12
|
+
const err = new Error(`${name}: ${message}`);
|
|
13
|
+
err.name = name;
|
|
14
|
+
return err;
|
|
15
|
+
}
|
|
16
|
+
async function validateSchema(schema, value) {
|
|
17
|
+
if (!schema)
|
|
18
|
+
return;
|
|
19
|
+
const result = schema['~standard'].validate(value);
|
|
20
|
+
const resolved = result instanceof Promise ? await result : result;
|
|
21
|
+
if (resolved.issues) {
|
|
22
|
+
throw blocksError(AppSettingErrors.ValidationFailed, resolved.issues[0].message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// ── AppSetting (AWS runtime) ────────────────────────────────────────────────
|
|
26
|
+
/**
|
|
27
|
+
* A single application configuration value backed by SSM Parameter Store.
|
|
28
|
+
*
|
|
29
|
+
* **When to use:** You need to store and retrieve a non-secret configuration
|
|
30
|
+
* value at runtime — a feature flag, API URL, threshold, or structured config
|
|
31
|
+
* object. For sensitive values, set `secret: true` to use SSM SecureString.
|
|
32
|
+
*
|
|
33
|
+
* **When NOT to use:** If you need structured key-value data with conditional
|
|
34
|
+
* writes and queries, use `KVStore` or `DistributedTable`.
|
|
35
|
+
*
|
|
36
|
+
* **Best practices:**
|
|
37
|
+
* - One AppSetting per logical configuration value
|
|
38
|
+
* - Use a schema for structured objects to get type safety and runtime validation
|
|
39
|
+
* - Use `secret: true` for API keys, tokens, and passwords
|
|
40
|
+
*
|
|
41
|
+
* **Scaling:** Standard-tier SSM parameters. 40 TPS default for GetParameter
|
|
42
|
+
* (can be increased). No cost for standard parameters.
|
|
43
|
+
*/
|
|
44
|
+
export class AppSetting extends Scope {
|
|
45
|
+
/**
|
|
46
|
+
* Reference an SSM parameter created and owned **outside this stack** (e.g. a
|
|
47
|
+
* connection string seeded by `ensureSecrets` before deploy). At runtime this
|
|
48
|
+
* reads the value like any other setting; the construct-time behavior (no
|
|
49
|
+
* create/seed/tag/delete, read-only grant) is applied by the CDK variant. The
|
|
50
|
+
* factory exists on every variant so app code uses one API across dev/deploy.
|
|
51
|
+
*/
|
|
52
|
+
static fromExisting(scope, id, options) {
|
|
53
|
+
const opts = { ...options, external: true };
|
|
54
|
+
return new AppSetting(scope, id, opts);
|
|
55
|
+
}
|
|
56
|
+
bbName = BB_NAME;
|
|
57
|
+
schema;
|
|
58
|
+
isSecret;
|
|
59
|
+
client;
|
|
60
|
+
/** @internal Logger for internal operations. Defaults to error-level when not provided. */
|
|
61
|
+
log;
|
|
62
|
+
constructor(scope, id, options) {
|
|
63
|
+
super(id, { parent: scope, bbName: BB_NAME, bbVersion: BB_VERSION });
|
|
64
|
+
this.log = options?.logger ?? new Logger(this, 'logger', { level: 'error' });
|
|
65
|
+
const parameterName = options.name ?? `/${this.fullId}`;
|
|
66
|
+
this.schema = options.schema;
|
|
67
|
+
this.isSecret = options.secret ?? false;
|
|
68
|
+
this.client = new SSMClient({
|
|
69
|
+
customUserAgent: this.buildUserAgentChain(),
|
|
70
|
+
});
|
|
71
|
+
registerSdkIdentifiers(this.fullId, { parameterName });
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Retrieve the current value.
|
|
75
|
+
*
|
|
76
|
+
* Returns the stored SSM parameter value. The parameter is guaranteed
|
|
77
|
+
* to exist because the CDK layer creates it with the initial value.
|
|
78
|
+
*
|
|
79
|
+
* @returns The current value.
|
|
80
|
+
* @throws {AppSettingErrors.ParameterNotFound} If the parameter does not exist in SSM (e.g., deleted out-of-band).
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const retries = await maxRetries.get();
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
async get() {
|
|
88
|
+
const result = await this.client.send(new GetParameterCommand({
|
|
89
|
+
Name: getSdkIdentifiers(this).parameterName,
|
|
90
|
+
WithDecryption: this.isSecret,
|
|
91
|
+
}));
|
|
92
|
+
this.client.config.customUserAgent = this.buildUserAgentChain();
|
|
93
|
+
const raw = result.Parameter?.Value;
|
|
94
|
+
if (raw === undefined || raw === null) {
|
|
95
|
+
throw blocksError(AppSettingErrors.ParameterNotFound, `Parameter "${getSdkIdentifiers(this).parameterName}" has no value`);
|
|
96
|
+
}
|
|
97
|
+
let value;
|
|
98
|
+
try {
|
|
99
|
+
value = JSON.parse(raw);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
// fallback for old-format values
|
|
103
|
+
value = raw;
|
|
104
|
+
}
|
|
105
|
+
if (this.isSecret && value === '') {
|
|
106
|
+
throw blocksError(AppSettingErrors.ParameterNotFound, `Secret parameter "${getSdkIdentifiers(this).parameterName}" has an empty value — secrets must not be empty`);
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Update the value at runtime.
|
|
112
|
+
*
|
|
113
|
+
* Overwrites the current SSM parameter value. When a schema is configured,
|
|
114
|
+
* the value is validated before writing.
|
|
115
|
+
*
|
|
116
|
+
* @param value - The new value to store.
|
|
117
|
+
* @throws {AppSettingErrors.ValidationFailed} If schema validation fails or value exceeds 4 KB.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* await maxRetries.put('5');
|
|
122
|
+
* await config.put({ maxRetries: 5, timeout: 10000 });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
async put(value) {
|
|
126
|
+
await validateSchema(this.schema, value);
|
|
127
|
+
const serialized = JSON.stringify(value);
|
|
128
|
+
await this.client.send(new PutParameterCommand({
|
|
129
|
+
Name: getSdkIdentifiers(this).parameterName,
|
|
130
|
+
Value: serialized,
|
|
131
|
+
Type: this.isSecret ? 'SecureString' : 'String',
|
|
132
|
+
Overwrite: true,
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAIA,qBAAa,UAAU;IACtB,MAAM,CAAC,YAAY,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG;gBAG5B,GAAG,IAAI,EAAE,GAAG,EAAE;CAC1B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// Browser stub - AppSetting runs server-side only
|
|
4
|
+
export class AppSetting {
|
|
5
|
+
static fromExisting(...args) {
|
|
6
|
+
return new AppSetting();
|
|
7
|
+
}
|
|
8
|
+
constructor(...args) { }
|
|
9
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Scope } from '@aws-blocks/core/cdk';
|
|
2
|
+
import type { ScopeParent } from '@aws-blocks/core';
|
|
3
|
+
import type { AppSettingOptions } from './types.js';
|
|
4
|
+
export { AppSettingErrors } from './errors.js';
|
|
5
|
+
export type { AppSettingOptions } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* CDK construct for AppSetting. Creates a single SSM parameter (String or
|
|
8
|
+
* SecureString) and grants the shared Lambda handler read/write permissions.
|
|
9
|
+
*
|
|
10
|
+
* - String parameters use `aws-cdk-lib/aws-ssm.StringParameter` directly.
|
|
11
|
+
* - SecureString parameters use a Custom Resource Lambda because
|
|
12
|
+
* CloudFormation cannot natively create SecureString parameters.
|
|
13
|
+
* - SecureString parameters are encrypted with the default `aws/ssm` KMS key.
|
|
14
|
+
*/
|
|
15
|
+
export declare class AppSetting<T = string> extends Scope {
|
|
16
|
+
/**
|
|
17
|
+
* Reference an SSM parameter that is created and owned **outside this stack**
|
|
18
|
+
* (e.g. a connection string seeded by `ensureSecrets` before deploy). The
|
|
19
|
+
* construct does not create, seed, tag, or delete it — it only grants the app
|
|
20
|
+
* **read-only** access (`ssm:GetParameter`, plus `kms:Decrypt` when `secret`)
|
|
21
|
+
* and registers the name for config resolution.
|
|
22
|
+
*
|
|
23
|
+
* The parameter MUST already exist at deploy time, otherwise the app fails at
|
|
24
|
+
* runtime with `ParameterNotFound`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const dbUrl = AppSetting.fromExisting(scope, 'db-url', { name: dbParameterName, secret: true });
|
|
28
|
+
*/
|
|
29
|
+
static fromExisting<T = string>(scope: ScopeParent, id: string, options: {
|
|
30
|
+
name: string;
|
|
31
|
+
secret?: boolean;
|
|
32
|
+
}): AppSetting<T>;
|
|
33
|
+
constructor(scope: ScopeParent, id: string, options: AppSettingOptions<T>);
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=index.cdk.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cdk.d.ts","sourceRoot":"","sources":["../src/index.cdk.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,EAAwC,MAAM,sBAAsB,CAAC;AACnF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,KAAK,EAAE,iBAAiB,EAA6B,MAAM,YAAY,CAAC;AAE/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpD;;;;;;;;GAQG;AACH,qBAAa,UAAU,CAAC,CAAC,GAAG,MAAM,CAAE,SAAQ,KAAK;IAChD;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,MAAM,EAC7B,KAAK,EAAE,WAAW,EAClB,EAAE,EAAE,MAAM,EACV,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,GACzC,UAAU,CAAC,CAAC,CAAC;gBAKJ,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC,CAAC;CA0HzE"}
|