Skip to content

Commit 35275c3

Browse files
authored
chore(toolkit): programmatic toolkit for the AWS CDK initial code (#32919)
### Description of changes Initial code for the Programmatic Toolkit. This won't be released just yet. Contains a mix of extensions and hard copies to the current CLI code. After this PR we are moving the appropriate tests over from the CLI. ### Describe any new or updated permissions being added n/a ### Description of how you validated changes For the changes to `aws-cdk` we run the existing tests and the integration tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d68020b commit 35275c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2587
-187
lines changed

packages/@aws-cdk/aws-custom-resource-sdk-adapter/package.json

+21-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,27 @@
2727
"devDependencies": {
2828
"@aws-cdk/cdk-build-tools": "0.0.0",
2929
"@aws-cdk/pkglint": "0.0.0",
30+
"@aws-sdk/client-account": "3.632.0",
31+
"@aws-sdk/client-acm": "3.632.0",
32+
"@aws-sdk/client-amplify": "3.632.0",
33+
"@aws-sdk/client-cloudwatch": "3.632.0",
34+
"@aws-sdk/client-cloudwatch-logs": "3.632.0",
35+
"@aws-sdk/client-codepipeline": "3.632.0",
36+
"@aws-sdk/client-dynamodb": "3.632.0",
37+
"@aws-sdk/client-ec2": "3.632.0",
38+
"@aws-sdk/client-ecr": "3.632.0",
39+
"@aws-sdk/client-ecs": "3.632.0",
40+
"@aws-sdk/client-eks": "3.632.0",
41+
"@aws-sdk/client-kinesis": "3.632.0",
42+
"@aws-sdk/client-kms": "3.632.0",
43+
"@aws-sdk/client-lambda": "3.632.0",
44+
"@aws-sdk/client-redshift": "3.632.0",
45+
"@aws-sdk/client-route-53": "3.632.0",
3046
"@aws-sdk/client-s3": "3.632.0",
47+
"@aws-sdk/client-ssm": "3.632.0",
48+
"@aws-sdk/client-sts": "3.632.0",
49+
"@aws-sdk/client-synthetics": "3.632.0",
50+
"@aws-sdk/s3-request-presigner": "3.632.0",
3151
"@smithy/types": "3.6.0",
3252
"@types/jest": "^29.5.14",
3353
"jest": "^29.7.0"
@@ -51,4 +71,4 @@
5171
"publishConfig": {
5272
"tag": "latest"
5373
}
54-
}
74+
}

packages/@aws-cdk/custom-resource-handlers/lib/aws-events-targets/aws-api-handler/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface AwsApiInput {
66
readonly service: string;
77
readonly action: string;
88
readonly parameters?: {
9-
[param: string]: any,
9+
[param: string]: any;
1010
};
1111
readonly apiVersion?: string;
1212
readonly catchErrorPattern?: string;

packages/@aws-cdk/toolkit/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.js
22
*.js.map
33
*.d.ts
4+
*.d.ts.map
45
*.gz
56
node_modules
67
dist

packages/@aws-cdk/toolkit/.npmignore

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1-
# Ignore artifacts
1+
# Ignore build artifacts
2+
**/cdk.out
3+
**/*.snapshot
24
dist
35
.LAST_PACKAGE
46
.LAST_BUILD
57
*.snk
68
*.ts
7-
!*.d.ts
8-
!*.js
99
coverage
1010
.nyc_output
1111
*.tgz
1212

13-
# Ignore configs and test files
13+
# Ignore config files
1414
.eslintrc.js
1515
tsconfig.json
1616
*.tsbuildinfo
1717
junit.xml
18+
jest.config.js
19+
bundle.mjs
1820

19-
# Include .jsii
20-
!.jsii
21-
22-
# exclude cdk artifacts
23-
**/cdk.out
24-
**/*.snapshot
21+
# Explicitly allow all required files
22+
!build-info.json
23+
!db.json.gz
24+
# !lib/main.js
25+
# !lib/bridge.js
26+
# !lib/setup-sandbox.js
27+
# !lib/api/bootstrap/bootstrap-template.yaml
28+
!*.d.ts
29+
!*.d.ts.map
30+
!*.js
31+
!LICENSE
32+
!NOTICE
33+
!THIRD_PARTY_LICENSES

packages/@aws-cdk/toolkit/bundle.mjs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createRequire } from 'node:module';
2+
import * as path from "node:path";
3+
import * as esbuild from "esbuild";
4+
import * as fs from "fs-extra";
5+
6+
const require = createRequire(import.meta.url);
7+
8+
const cliPackage = path.dirname(require.resolve("aws-cdk/package.json"));
9+
let copyFromCli = (from, to = undefined) => {
10+
return fs.copy(path.join(cliPackage, ...from), path.join(process.cwd(), ...(to ?? from)))
11+
}
12+
13+
await Promise.all([
14+
copyFromCli(["build-info.json"]),
15+
copyFromCli(["/db.json.gz"]),
16+
copyFromCli(["lib", "index_bg.wasm"]),
17+
])
18+
19+
// # Copy all resources that aws_cdk/generate.sh produced, and some othersCall the generator for the
20+
// cp -R $aws_cdk/lib/init-templates ./lib/
21+
// mkdir -p ./lib/api/bootstrap/ && cp $aws_cdk/lib/api/bootstrap/bootstrap-template.yaml ./lib/api/bootstrap/
22+
23+
24+
let bundleCli = {
25+
name: "bundle-aws-cdk",
26+
setup(build) {
27+
28+
// Mark all paths inside aws-cdk as internal
29+
build.onResolve({ filter: /^aws-cdk\/lib/ }, (args) => {
30+
return { path: require.resolve(args.path), external: false }
31+
});
32+
},
33+
};
34+
35+
await esbuild.build({
36+
entryPoints: ["lib/index.ts"],
37+
target: "node18",
38+
platform: "node",
39+
packages: "external",
40+
plugins: [bundleCli],
41+
sourcemap: true,
42+
bundle: true,
43+
outfile: "lib/main.js",
44+
});

packages/@aws-cdk/toolkit/lib/actions/deploy.ts

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { StackSelector } from '../types';
1+
import { Deployments, StackActivityProgress, WorkGraph } from '../api/aws-cdk';
2+
import { StackSelector } from '../api/cloud-assembly/stack-selector';
23

34
export type DeploymentMethod = DirectDeploymentMethod | ChangeSetDeploymentMethod;
45

@@ -113,6 +114,7 @@ export interface BaseDeployOptions {
113114
readonly stacks: StackSelector;
114115

115116
/**
117+
* @deprecated set on toolkit
116118
* Name of the toolkit stack to use/deploy
117119
*
118120
* @default CDKToolkit
@@ -130,6 +132,7 @@ export interface BaseDeployOptions {
130132
* Always deploy, even if templates are identical.
131133
*
132134
* @default false
135+
* @deprecated
133136
*/
134137
readonly force?: boolean;
135138

@@ -225,4 +228,51 @@ export interface DeployOptions extends BaseDeployOptions {
225228
* @default AssetBuildTime.ALL_BEFORE_DEPLOY
226229
*/
227230
readonly assetBuildTime?: AssetBuildTime;
231+
232+
/**
233+
* Change stack watcher output to CI mode.
234+
*
235+
* @deprecated Implement in IoHost instead
236+
*/
237+
readonly ci?: boolean;
238+
239+
/**
240+
* Display mode for stack deployment progress.
241+
*
242+
* @deprecated Implement in IoHost instead
243+
*/
244+
readonly progress?: StackActivityProgress;
245+
}
246+
247+
export function buildParameterMap(parameters?: Map<string, string | undefined>): { [name: string]: { [name: string]: string | undefined } } {
248+
const parameterMap: {
249+
[name: string]: { [name: string]: string | undefined };
250+
} = {};
251+
parameterMap['*'] = {};
252+
253+
const entries = parameters?.entries() ?? [];
254+
for (const [key, value] of entries) {
255+
const [stack, parameter] = key.split(':', 2) as [string, string | undefined];
256+
if (!parameter) {
257+
parameterMap['*'][stack] = value;
258+
} else {
259+
if (!parameterMap[stack]) {
260+
parameterMap[stack] = {};
261+
}
262+
parameterMap[stack][parameter] = value;
263+
}
264+
}
265+
266+
return parameterMap;
267+
}
268+
269+
/**
270+
* Remove the asset publishing and building from the work graph for assets that are already in place
271+
*/
272+
export async function removePublishedAssets(graph: WorkGraph, deployments: Deployments, options: DeployOptions) {
273+
await graph.removeUnnecessaryAssets(assetNode => deployments.isSingleAssetPublished(assetNode.assetManifest, assetNode.asset, {
274+
stack: assetNode.parentStack,
275+
roleArn: options.roleArn,
276+
stackName: assetNode.parentStack.stackName,
277+
}));
228278
}

packages/@aws-cdk/toolkit/lib/actions/destroy.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StackSelector } from '../types';
1+
import { StackSelector } from '../api/cloud-assembly/stack-selector';
22

33
export interface DestroyOptions {
44
/**
@@ -10,4 +10,11 @@ export interface DestroyOptions {
1010
* The arn of the IAM role to use
1111
*/
1212
readonly roleArn?: string;
13+
14+
/**
15+
* Change stack watcher output to CI mode.
16+
*
17+
* @deprecated Implement in IoHost instead
18+
*/
19+
readonly ci?: boolean;
1320
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { StackSelector } from '../api/cloud-assembly/stack-selector';
2+
3+
export interface CloudFormationDiffOptions {
4+
/**
5+
* Whether to run the diff against the template after the CloudFormation Transforms inside it have been executed
6+
* (as opposed to the original template, the default, which contains the unprocessed Transforms).
7+
*
8+
* @default false
9+
*/
10+
readonly compareAgainstProcessedTemplate?: boolean;
11+
}
12+
13+
export interface ChangeSetDiffOptions extends CloudFormationDiffOptions {
14+
/**
15+
* Enable falling back to template-based diff in case creating the changeset is not possible or results in an error.
16+
*
17+
* Should be used for stacks containing nested stacks or when change set permissions aren't available.
18+
*
19+
* @default true
20+
*/
21+
readonly fallbackToTemplate?: boolean;
22+
23+
/**
24+
* Additional parameters for CloudFormation when creating a diff change set
25+
*
26+
* @default {}
27+
*/
28+
readonly parameters?: { [name: string]: string | undefined };
29+
}
30+
31+
export class DiffMethod {
32+
/**
33+
* Use a changeset to compute the diff.
34+
*
35+
* This will create, analyze, and subsequently delete a changeset against the CloudFormation stack.
36+
*/
37+
public static ChangeSet(options: ChangeSetDiffOptions = {}) {
38+
return new class extends DiffMethod {
39+
public override readonly options: ChangeSetDiffOptions;
40+
public constructor(opts: ChangeSetDiffOptions) {
41+
super('change-set', opts);
42+
this.options = opts;
43+
}
44+
}(options);
45+
}
46+
47+
public static TemplateOnly(options: CloudFormationDiffOptions = {}) {
48+
return new class extends DiffMethod {
49+
public override readonly options: CloudFormationDiffOptions;
50+
public constructor(opts: CloudFormationDiffOptions) {
51+
super('template-only', opts);
52+
this.options = opts;
53+
}
54+
}(options);
55+
}
56+
57+
public static LocalFile(path: string) {
58+
return new class extends DiffMethod {
59+
public override readonly options: { path: string };
60+
public constructor(opts: { path: string }) {
61+
super('local-file', opts);
62+
this.options = opts;
63+
}
64+
}({ path });
65+
};
66+
67+
private constructor(
68+
public readonly method: 'change-set' | 'template-only' | 'local-file',
69+
public readonly options: ChangeSetDiffOptions | CloudFormationDiffOptions | { path: string },
70+
) {}
71+
}
72+
73+
export interface DiffOptions {
74+
/**
75+
* Select the stacks
76+
*/
77+
readonly stacks: StackSelector;
78+
79+
/**
80+
* The mode to create a stack diff.
81+
*
82+
* Use changeset diff for the highest fidelity, including analyze resource replacements.
83+
* In this mode, diff will use the deploy role instead of the lookup role.
84+
*
85+
* Use template-only diff for a faster, less accurate diff that doesn't require
86+
* permissions to create a change-set.
87+
*
88+
* Use local-template diff for a fast, local-only diff that doesn't require
89+
* any permissions or internet access.
90+
*
91+
* @default DiffMode.ChangeSet
92+
*/
93+
readonly method: DiffMethod;
94+
95+
/**
96+
* Strict diff mode
97+
* When enabled, this will not filter out AWS::CDK::Metadata resources, mangled non-ASCII characters, or the CheckBootstrapVersionRule.
98+
*
99+
* @default false
100+
*/
101+
readonly strict?: boolean;
102+
103+
/**
104+
* How many lines of context to show in the diff
105+
*
106+
* @default 3
107+
*/
108+
readonly contextLines?: number;
109+
110+
/**
111+
* Only include broadened security changes in the diff
112+
*
113+
* @default false
114+
*/
115+
readonly securityOnly?: boolean;
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export * from './deploy';
2+
export * from './destroy';
3+
export * from './diff';
4+
export * from './import';
5+
export * from './list';
6+
export * from './synth';
7+
export * from './watch';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { StackSelector } from '../api/cloud-assembly/stack-selector';
2+
3+
export interface ListOptions {
4+
/**
5+
* Select the stacks
6+
*/
7+
readonly stacks: StackSelector;
8+
}

0 commit comments

Comments
 (0)