You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add examples and note for plugins written in ESM
By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.
---------
Co-authored-by: Evan Kuranishi <evzz@amazon.com>
Co-authored-by: Evan K <113387406+evzzk@users.noreply.github.com>
Copy file name to clipboardExpand all lines: v2/guide/plugins.adoc
+176Lines changed: 176 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,8 @@ To create a CDK Toolkit plugin, you first create a Node.js module, authored in T
35
35
[role="tablist"]
36
36
TypeScript::
37
37
+
38
+
*CommonJS*:::
39
+
+
38
40
[source,typescript,subs="verbatim,attributes"]
39
41
----
40
42
// Example plugin structure
@@ -53,9 +55,32 @@ const plugin: Plugin = {
53
55
54
56
export = plugin;
55
57
----
58
+
+
59
+
*ESM*:::
60
+
+
61
+
[source,typescript,subs="verbatim,attributes"]
62
+
----
63
+
// Example plugin structure
64
+
import type { IPluginHost, Plugin } from '@aws-cdk/cli-plugin-contract';
65
+
66
+
const plugin: Plugin = {
67
+
// Version of the plugin infrastructure (currently always '1')
68
+
version: '1',
69
+
70
+
// Initialization function called when the plugin is loaded
71
+
init(host: IPluginHost): void {
72
+
// Register your plugin functionality with the host
73
+
// For example, register a custom credential provider
74
+
}
75
+
};
76
+
77
+
export { plugin as 'module.exports' };
78
+
----
56
79
57
80
JavaScript::
58
81
+
82
+
*CommonJS*:::
83
+
+
59
84
[source,javascript,subs="verbatim,attributes"]
60
85
----
61
86
const plugin = {
@@ -71,6 +96,34 @@ const plugin = {
71
96
72
97
module.exports = plugin;
73
98
----
99
+
+
100
+
*ESM*:::
101
+
+
102
+
[source,javascript,subs="verbatim,attributes"]
103
+
----
104
+
const plugin = {
105
+
// Version of the plugin infrastructure (currently always '1')
106
+
version: '1',
107
+
108
+
// Initialization function called when the plugin is loaded
109
+
init(host) {
110
+
// Register your plugin functionality with the host
111
+
// For example, register a custom credential provider
112
+
}
113
+
};
114
+
115
+
export { plugin as 'module.exports' };
116
+
----
117
+
====
118
+
119
+
[NOTE]
120
+
====
121
+
CDK Toolkit plugins are loaded as CommonJS modules. You can build your plugin as an ECMAScript module (ESM), but have to link:https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require[adhere to some constraints]:
122
+
123
+
--
124
+
* The module cannot contain top-level `await`, or import any modules that contain top-level `await`.
125
+
* The module must ensure compatibility by exporting the plugin object under the `module.exports` export name: `export { plugin as 'module.exports' }`.
126
+
--
74
127
====
75
128
76
129
[#plugins-load-cli]
@@ -177,6 +230,8 @@ To implement a custom credential provider, create a class that implements the re
177
230
[role="tablist"]
178
231
TypeScript::
179
232
+
233
+
*CommonJS*:::
234
+
+
180
235
[source,typescript,subs="verbatim,attributes"]
181
236
----
182
237
import type { CredentialProviderSource, ForReading, ForWriting, IPluginHost, Plugin, PluginProviderResult, SDKv3CompatibleCredentials } from '@aws-cdk/cli-plugin-contract';
@@ -233,9 +288,70 @@ const plugin: Plugin = {
233
288
234
289
export = plugin;
235
290
----
291
+
+
292
+
*ESM*:::
293
+
+
294
+
[source,typescript,subs="verbatim,attributes"]
295
+
----
296
+
import type { CredentialProviderSource, ForReading, ForWriting, IPluginHost, Plugin, PluginProviderResult, SDKv3CompatibleCredentials } from '@aws-cdk/cli-plugin-contract';
297
+
298
+
class CustomCredentialProviderSource implements CredentialProviderSource {
299
+
// Friendly name for the provider, used in error messages
300
+
public readonly name: string = 'custom-credential-provider';
301
+
302
+
// Check if this provider is available on the current system
303
+
public async isAvailable(): Promise<boolean> {
304
+
// Return false if the plugin cannot be used
305
+
// For example, if it depends on files not present on the host
306
+
return true;
307
+
}
308
+
309
+
// Check if this provider can provide credentials for a specific account
310
+
public async canProvideCredentials(accountId: string): Promise<boolean> {
311
+
// Return false if the plugin cannot provide credentials for this account
312
+
// For example, if the account is not managed by this credential system
313
+
return true;
314
+
// You can use patterns to filter specific accounts
315
+
// return accountId.startsWith('123456');
316
+
}
317
+
318
+
// Get credentials for the specified account and access mode
319
+
// Returns PluginProviderResult which can be one of:
320
+
// - SDKv2CompatibleCredentials (AWS SDK v2 entered maintenance on Sept 8, 2024 and will reach end-of-life on Sept 8, 2025)
321
+
// - SDKv3CompatibleCredentialProvider
322
+
// - SDKv3CompatibleCredentials
323
+
public async getProvider(accountId: string, mode: ForReading | ForWriting): Promise<PluginProviderResult> {
324
+
// The access mode can be used to provide different credential sets
325
+
const readOnly = mode === 0 satisfies ForReading;
326
+
327
+
// Create appropriate credentials based on your authentication mechanism
0 commit comments