Skip to content

Commit dda5e82

Browse files
authored
feat(toolkit-lib): General Availability of the AWS CDK Toolkit Library (#553)
**📣 Today we are very happy to announce the General Availability of the AWS CDK Toolkit Library 📣** The AWS Cloud Development Kit (AWS CDK) Toolkit Library enables you to perform CDK actions requiring programmatic access on AWS. You can use the AWS CDK Toolkit Library to implement actions such as bootstrapping, synthesizing, and deploying through code rather than command-line interface (CLI) commands. With this library, you can create custom tools, build specialized CLI applications, and integrate CDK programmatic access capabilities into your development workflows. 📜 Reference documentation: https://docs.aws.amazon.com/cdk/api/toolkit-lib/ 📦 npm package: https://www.npmjs.com/package/@aws-cdk/toolkit-lib 👩🏻‍🏫 Guide: https://docs.aws.amazon.com/cdk/v2/guide/toolkit-library.html --- Even though the CDK Toolkit Library is now *generally available*, we still want to hear from you and appreciate all feedback! 💬 [Provide feedback, ideas and request new features](#154) 🐛 [Report a bug](https://github.com/aws/aws-cdk-cli/issues/new/choose) --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent c404f17 commit dda5e82

File tree

6 files changed

+86
-71
lines changed

6 files changed

+86
-71
lines changed

.projenrc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ const toolkitLib = configureProject(
811811
isolatedModules: false, // we use the respective tsc setting
812812
},
813813
},
814+
majorVersion: 1,
814815
nextVersionCommand: 'tsx ../../../projenrc/next-version.ts maybeRc',
815816
}),
816817
);

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ If you have a support plan with AWS Support, you can also create a new [support
2929

3030
You may also find help on these community resources:
3131

32-
* Look through the [API Reference](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html) or [Developer Guide](https://docs.aws.amazon.com/cdk/latest/guide)
33-
* The #aws-cdk Slack channel in [cdk.dev](https://cdk.dev)
34-
* Ask a question on [Stack Overflow](https://stackoverflow.com/questions/tagged/aws-cdk)
32+
- Look through the [API Reference](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html) or [Developer Guide](https://docs.aws.amazon.com/cdk/latest/guide)
33+
- The #aws-cdk Slack channel in [cdk.dev](https://cdk.dev)
34+
- Ask a question on [Stack Overflow](https://stackoverflow.com/questions/tagged/aws-cdk)
3535
and tag it with `aws-cdk`
3636

3737
## Contributing
@@ -44,20 +44,20 @@ environment and submit code.
4444

4545
Here are the packages in this repository. See the README of each package for more information about it:
4646

47-
| Package | Description | Published? | Maintained? |
48-
|---------|-------------|------------|-------------|
49-
| [aws-cdk](./packages/aws-cdk/) | The CDK Toolkit CLI, main CLI interface to CDK projects. | Yes | Yes |
50-
| [cdk](./packages/cdk/) | An alias for `aws-cdk` so you can run `npx cdk` even if it's not installed. | Yes | Yes |
51-
| [cdk-assets v3](./packages/cdk-assets/) | CLI component handling asset uploads, also used as a CLI in [CDK Pipelines](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines-readme.html) | Yes | Yes |
52-
| [@aws-cdk/cloud-assembly-schema](./packages/@aws-cdk//cloud-assembly-schema/) | The contract between the CDK construct library and the CDK toolkit | Yes | Yes |
53-
| [@aws-cdk/cloudformation-diff](./packages/@aws-cdk/cloudformation-diff/) | CLI component for diffing CloudFormation templates | Yes | Yes |
54-
| [@aws-cdk/cli-lib-alpha](./packages/@aws-cdk/cli-lib-alpha/) | A deprecated attempt at building a programmatic interface for the CLI | Yes | No |
55-
| [@aws-cdk/toolkit-lib](./packages/@aws-cdk/toolkit-lib/) | A programmatic interface for the CDK Toolkit | Yes | Yes |
56-
| [@aws-cdk/cli-plugin-contract](./packages/@aws-cdk/cli-plugin-contract/) | TypeScript types for CLI plugins. | No | Yes |
57-
| [@aws-cdk/cdk-cli-wrapper](./packages/@aws-cdk/cdk-cli-wrapper/) | A deprecated attempt at building a programmatic interface for the CLI | No | No |
58-
| [@aws-cdk/node-bundle](./packages/@aws-cdk/node-bundle/) | A tool to build CLI bundles that include license attributions. | No | Yes |
59-
| [@aws-cdk/user-input-gen](./packages/@aws-cdk/user-input-gen/) | A build tool for the CLI and toolkit-lib. | No | Yes |
60-
| [@aws-cdk/yarn-cling](./packages/@aws-cdk/yarn-cling/) | A deprecated build tool for the CLI. | No | No |
47+
| Package | Description | Published? | Maintained? |
48+
| ----------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ----------- |
49+
| [aws-cdk](./packages/aws-cdk/) | The CDK Toolkit CLI, main CLI interface to CDK projects. | Yes | Yes |
50+
| [@aws-cdk/toolkit-lib](./packages/@aws-cdk/toolkit-lib/) | A programmatic interface for the CDK Toolkit. | Yes | Yes |
51+
| [cdk](./packages/cdk/) | An alias for `aws-cdk` so you can run `npx cdk` even if it's not installed. | Yes | Yes |
52+
| [cdk-assets v3](./packages/cdk-assets/) | CLI component handling asset uploads, also used as a CLI in [CDK Pipelines](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines-readme.html) | Yes | Yes |
53+
| [@aws-cdk/cloud-assembly-schema](./packages/@aws-cdk//cloud-assembly-schema/) | The contract between the CDK construct library and the CDK toolkit | Yes | Yes |
54+
| [@aws-cdk/cloudformation-diff](./packages/@aws-cdk/cloudformation-diff/) | CLI component for diffing CloudFormation templates | Yes | Yes |
55+
| [@aws-cdk/cli-lib-alpha](./packages/@aws-cdk/cli-lib-alpha/) | A deprecated attempt at building a programmatic interface for the CLI | Yes | No |
56+
| [@aws-cdk/cli-plugin-contract](./packages/@aws-cdk/cli-plugin-contract/) | TypeScript types for CLI plugins. | Yes | Yes |
57+
| [@aws-cdk/cdk-cli-wrapper](./packages/@aws-cdk/cdk-cli-wrapper/) | A deprecated attempt at building a programmatic interface for the CLI | No | No |
58+
| [@aws-cdk/node-bundle](./packages/@aws-cdk/node-bundle/) | A tool to build CLI bundles that include license attributions. | No | Yes |
59+
| [@aws-cdk/user-input-gen](./packages/@aws-cdk/user-input-gen/) | A build tool for the CLI and toolkit-lib. | No | Yes |
60+
| [@aws-cdk/yarn-cling](./packages/@aws-cdk/yarn-cling/) | A deprecated build tool for the CLI. | No | No |
6161

6262
Every package comes with its own unit tests. There is a companion repository to this one containing the integration tests. You can find it here: <https://github.com/aws/aws-cdk-cli-testing>
6363

@@ -70,4 +70,4 @@ released before it. We recommend you always keep the CLI version up-to-date to g
7070
latest features.
7171

7272
For more details on the compatibility model and specific versions that work together,
73-
see [COMPATIBILITY.md](COMPATIBILITY.md).
73+
see [COMPATIBILITY.md](COMPATIBILITY.md).

packages/@aws-cdk/toolkit-lib/.projen/tasks.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit-lib/README.md

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
11
# AWS CDK Toolkit Library
2-
<!--BEGIN STABILITY BANNER-->
3-
4-
---
5-
6-
![@aws-cdk/toolkit-lib: Developer Preview](./assets/toolkit--lib-developer_preview-important.svg)
7-
8-
> The APIs in this module are experimental and under active development.
9-
> They are subject to non-backward compatible changes or removal in future versions.
10-
> The package follows the [Semantic Versioning](https://semver.org/) model for [major version zero](https://semver.org/#spec-item-4).
11-
> Accordingly, breaking changes will be introduced in minor versions and announced in the release notes.
12-
> This means that while you may use them, you may need to update
13-
> your source code when upgrading to a newer version of this package.
14-
15-
---
16-
17-
<!--END STABILITY BANNER-->
182

193
The AWS Cloud Development Kit (AWS CDK) Toolkit Library enables you to perform CDK actions requiring programmatic access on AWS. You can use the AWS CDK Toolkit Library to implement actions such as bootstrapping, synthesizing, and deploying through code rather than command-line interface (CLI) commands. With this library, you can create custom tools, build specialized CLI applications, and integrate CDK programmatic access capabilities into your development workflows.
204

@@ -54,7 +38,7 @@ The _Cloud Assembly Source_ is a fundamental CDK Toolkit component that defines
5438
For example, CDK apps may need to be synthesized multiple times with additional context values before they are ready.
5539
Once created, you can use your _Cloud Assembly Source_ to perform actions with the CDK Toolkit.
5640

57-
The following is an example of creating a _Cloud Assembly Source_ using an inline _assembly builder function_:
41+
The following is an example of creating a _Cloud Assembly Source_ using the _assembly builder function_:
5842

5943
```ts
6044
import * as core from 'aws-cdk-lib/core';
@@ -112,24 +96,25 @@ because the _Cloud Assembly_ does not have to be produced multiple times.
11296

11397
```ts
11498
declare const cdk: Toolkit;
115-
declare const cx: ICloudAssemblySource;
99+
declare const source: ICloudAssemblySource;
116100

117101
// Will run the CDK app defined in the Cloud Assembly Source
118102
// This is an expensive and slow operation
119-
const cxSnap = await cdk.synth(cx, {
103+
// But its result can be stored and re-used
104+
const cx = await cdk.synth(source, {
120105
validateStacks: true, // set to `false` to not throw an error if stacks in the assembly contain error annotations
121106
})
122107

123108
// Will use the previously synthesized Cloud Assembly
124109
// This is now a cheap and fast operation
125-
const cxSnap = await cdk.list(cxSnap);
110+
const appDetails = await cdk.list(cx);
126111
```
127112

128-
You can also use the snapshot to query information about the synthesized _Cloud Assembly_:
113+
You can also query information about the synthesized _Cloud Assembly_:
129114

130115
```ts
131-
declare const cloudAssembly = await cxSnap.produce();
132-
declare const template = cloudAssembly.getStack("my-stack").template;
116+
const cloudAssembly = await cx.produce();
117+
const template = cloudAssembly.getStack("my-stack").template;
133118
```
134119

135120
### list
@@ -149,6 +134,23 @@ const details = await cdk.list(cx, {
149134
});
150135
```
151136

137+
### diff
138+
139+
You can create a diff of your app to gain detailed insights into what will be changing in the next deployment.
140+
141+
```ts
142+
declare const cdk: Toolkit;
143+
declare const cx: ICloudAssemblySource;
144+
145+
const stackDiffs = await cdk.diff(cx, {
146+
// optionally provide a stack selector to control which stacks you want to create a diff for
147+
stacks: {
148+
strategy: StackSelectionStrategy.PATTERN_MUST_MATCH,
149+
patterns: ["my-stack"],
150+
}
151+
});
152+
```
153+
152154
### deploy
153155

154156
Deployment is the process of provisioning or updating your infrastructure in AWS using the CloudFormation templates and assets produced during synthesis.
@@ -194,6 +196,28 @@ await cdk.watch(cx, {
194196
})
195197
```
196198

199+
### drift
200+
201+
Drift detection is crucial for maintaining infrastructure reliability.
202+
It identifies when your deployed resources have been modified outside of your CDK code.
203+
This allows you to quickly address unauthorized changes.
204+
Such changes could lead to security vulnerabilities, compliance issues, or unexpected behavior in your cloud environment.
205+
206+
Use the drift operation to detect such drift in your CDK applications:
207+
208+
```ts
209+
declare const cdk: Toolkit;
210+
declare const cx: ICloudAssemblySource;
211+
212+
const stackDrifts = await cdk.drift(cx, {
213+
// optionally provide a stack selector to control which stacks are checked for drift
214+
stacks: {
215+
strategy: StackSelectionStrategy.PATTERN_MUST_MATCH,
216+
patterns: ["my-stack"],
217+
}
218+
});
219+
```
220+
197221
### destroy
198222

199223
Use the destroy feature to remove CDK stacks and their associated resources from AWS:
@@ -247,36 +271,40 @@ The CDK Toolkit awaits the completion of each call, allowing clients to perform
247271
When you implement an `IoHost` interface, you can either process these communications (for example, logging to CloudWatch or prompting users for input) or return immediately without taking action.
248272
If your implementation doesn’t provide a response to a request, the CDK Toolkit proceeds with a default value.
249273

250-
#### Default IoHost
274+
#### Default `NonInteractiveIoHost`
251275

252-
By default the CDK Toolkit Library will use a `IoHost` implantation that mimics the behavior of the AWS CDK Toolkit CLI.
276+
By default the CDK Toolkit Library will use a `NonInteractiveIoHost ` implementation that mimics the behavior of the AWS CDK Toolkit CLI.
277+
This `NonInteractiveIoHost` is available for you as a base implementation to extend on.
253278

254279
### Configure your AWS profile
255280

256281
The Toolkit internally uses AWS SDK Clients to make necessary API calls to AWS.
257-
Authentication configuration is loaded automatically from the environment, but you can explicitly specify the profile to be used:
282+
Authentication configuration is loaded automatically from the environment, but you can explicitly specify the base credentials to be used:
258283

259284
```ts
260-
import { Toolkit } from '@aws-cdk/toolkit-lib';
285+
import { Toolkit, BaseCredentials } from '@aws-cdk/toolkit-lib';
261286

262287
const cdk = new Toolkit({
263-
sdkConfig: { profile: "my-profile" },
288+
sdkConfig: {
289+
baseCredentials: BaseCredentials.awsCliCompatible({ profile: "my-profile" })
290+
},
264291
});
265292
```
266293

267294
### Configure a proxy for SDK calls
268295

269296
The Toolkit internally uses AWS SDK Clients to make necessary API calls to AWS.
270-
You can specify a proxy configuration for all SDK calls:
297+
You can specify a Node.js Agent to configure SDK calls.
298+
This allows you to configure third-party packages like [`proxy-agent`](https://www.npmjs.com/package/proxy-agent) to proxy all SDK calls:
271299

272300
```ts
301+
import { ProxyAgent } from 'proxy-agent';
273302
import { Toolkit } from '@aws-cdk/toolkit-lib';
274303

275304
const cdk = new Toolkit({
276305
sdkConfig: {
277306
httpOptions: {
278-
proxyAddress: "https://example.com",
279-
caBundlePath: "path/to/ca/bundle",
307+
agent: new ProxyAgent(),
280308
},
281309
},
282310
});
@@ -316,7 +344,8 @@ await cdk.fromCdkApp("ts-node app.ts");
316344
await cdk.fromCdkApp("python app.py");
317345
```
318346

319-
Alternatively a inline `AssemblyBuilder` function can be used to build a CDK app on-the-fly.
347+
The `AssemblyBuilder` function provides basic Toolkit features like Lookups, but otherwise allows you to implement any custom source.
348+
For example you build an inline CDK app on-the-fly:
320349

321350
```ts
322351
declare const cdk: Toolkit;
@@ -412,26 +441,11 @@ try {
412441
});
413442

414443
} catch (error) {
415-
416-
if (ToolkitError.isAuthenticationError(error)) {
417-
// Handle credential issues
418-
console.error('AWS credentials error:', error.message);
419-
420-
} else if (ToolkitError.isAssemblyError(error)) {
421-
// Handle errors from your CDK app
422-
console.error('CDK app error:', error.message);
423-
424-
} else if (ToolkitError.isContextProviderError(error)) {
425-
// Handle errors from context providers
426-
console.error('Context provider error:', error.message);
427-
428-
} else if (ToolkitError.isToolkitError(error)) {
429-
// Handle all other Toolkit errors
430-
console.error('Generic Toolkit error:', error.message);
431-
444+
// Handle user error
445+
if (ToolkitError.isToolkitError(error) && error.source === 'user') {
446+
console.error('Hey, something is wrong with your app!', error.message);
432447
} else {
433-
// Handle unexpected errors
434-
console.error('Unexpected error:', error);
448+
throw error;
435449
}
436450
}
437451
```

packages/@aws-cdk/toolkit-lib/assets/.gitkeep

Whitespace-only changes.

packages/@aws-cdk/toolkit-lib/assets/toolkit--lib-developer_preview-important.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)