From 9364e94d1b343d18d1ceceee2881f2cc59d67980 Mon Sep 17 00:00:00 2001 From: Antonio Angelino Date: Mon, 3 Jul 2023 17:36:43 +0100 Subject: [PATCH] feat(cli-lib): support bootstrap command (#26205) The first iteration of [@aws-cdk/cli-lib-alpha](https://docs.aws.amazon.com/cdk/api/v2/docs/cli-lib-alpha-readme.html) doesn't support the bootstrap command that is mandatory to deploy a new app via CDK. This PR introduces the bootstrap command for the CLI. Related: https://github.com/aws/aws-cdk/issues/15851 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/cli-lib-alpha/README.md | 8 ++ packages/@aws-cdk/cli-lib-alpha/lib/cli.ts | 35 ++++- .../cli-lib-alpha/lib/commands/bootstrap.ts | 123 ++++++++++++++++++ .../cli-lib-alpha/lib/commands/index.ts | 1 + .../cli-lib-alpha/test/commands.test.ts | 12 ++ 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts diff --git a/packages/@aws-cdk/cli-lib-alpha/README.md b/packages/@aws-cdk/cli-lib-alpha/README.md index 186637d6e0b09..d58bc2aeeddc0 100644 --- a/packages/@aws-cdk/cli-lib-alpha/README.md +++ b/packages/@aws-cdk/cli-lib-alpha/README.md @@ -34,6 +34,7 @@ Currently the package includes implementations for: - `cdk deploy` - `cdk synth` +- `cdk bootstrap` - `cdk destroy` - `cdk list` @@ -95,6 +96,13 @@ cli.synth({ }); ``` +### bootstrap + +```ts +// await this asynchronous method call using a language feature +cli.bootstrap(); +``` + ### deploy ```ts diff --git a/packages/@aws-cdk/cli-lib-alpha/lib/cli.ts b/packages/@aws-cdk/cli-lib-alpha/lib/cli.ts index f48fe351a0c4d..cde003aa42eed 100644 --- a/packages/@aws-cdk/cli-lib-alpha/lib/cli.ts +++ b/packages/@aws-cdk/cli-lib-alpha/lib/cli.ts @@ -2,7 +2,7 @@ import { exec as runCli } from 'aws-cdk/lib'; // eslint-disable-next-line import/no-extraneous-dependencies import { createAssembly, prepareContext, prepareDefaultEnvironment } from 'aws-cdk/lib/api/cxapp/exec'; -import { SharedOptions, DeployOptions, DestroyOptions, SynthOptions, ListOptions, StackActivityProgress } from './commands'; +import { SharedOptions, DeployOptions, DestroyOptions, BootstrapOptions, SynthOptions, ListOptions, StackActivityProgress } from './commands'; /** * AWS CDK CLI operations @@ -18,6 +18,11 @@ export interface IAwsCdkCli { */ synth(options?: SynthOptions): Promise; + /** + * cdk bootstrap + */ + bootstrap(options?: BootstrapOptions): Promise; + /** * cdk deploy */ @@ -163,6 +168,34 @@ export class AwsCdkCli implements IAwsCdkCli { await this.exec(['synth', ...synthCommandArgs]); } + /** + * cdk bootstrap + */ + public async bootstrap(options: BootstrapOptions = {}) { + const bootstrapCommandArgs: string[] = [ + ...renderBooleanArg('force', options.force), + ...renderBooleanArg('show-template', options.showTemplate), + ...renderBooleanArg('terminationProtection', options.terminationProtection), + ...renderBooleanArg('example-permissions-boundary', options.examplePermissionsBoundary), + ...renderBooleanArg('terminationProtection', options.usePreviousParameters), + ...renderBooleanArg('execute', options.execute), + ...options.toolkitStackName ? ['--toolkit-stack-name', options.toolkitStackName] : [], + ...options.bootstrapBucketName ? ['--bootstrap-bucket-name', options.bootstrapBucketName] : [], + ...options.cfnExecutionPolicy ? ['--cloudformation-execution-policies', options.cfnExecutionPolicy] : [], + ...options.template ? ['--template', options.template] : [], + ...options.customPermissionsBoundary ? ['--custom-permissions-boundary', options.customPermissionsBoundary] : [], + ...options.qualifier ? ['--qualifier', options.qualifier] : [], + ...options.trust ? ['--qualifier', options.trust] : [], + ...options.trustForLookup ? ['--qualifier', options.trustForLookup] : [], + ...options.bootstrapKmsKeyId ? ['--bootstrap-kms-key-id', options.bootstrapKmsKeyId] : [], + ...options.bootstrapCustomerKey ? ['--bootstrap-customer-key', options.bootstrapCustomerKey] : [], + ...options.publicAccessBlockConfiguration ? ['--public-access-block-configuration', options.publicAccessBlockConfiguration] : [], + ...this.createDefaultArguments(options), + ]; + + await this.exec(['bootstrap', ...bootstrapCommandArgs]); + } + /** * cdk deploy */ diff --git a/packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts b/packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts new file mode 100644 index 0000000000000..1dffabfdfb945 --- /dev/null +++ b/packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts @@ -0,0 +1,123 @@ +import { SharedOptions } from './common'; + +/** + * Options to use with cdk bootstrap + */ +export interface BootstrapOptions extends SharedOptions { + + /** + * The name of the CDK toolkit stack to create + */ + readonly toolkitStackName?: string; + + /** + * The name of the CDK toolkit bucket; bucket will be created and + * must not exist + * @default - auto-generated CloudFormation name + */ + readonly bootstrapBucketName?: string; + + /** + * Always bootstrap even if it would downgrade template version + * @default false + */ + readonly force?: boolean; + + /** + * The Managed Policy ARNs that should be attached to the + * role performing deployments into this environment (may be repeated, modern bootstrapping only) + * @default - none + */ + readonly cfnExecutionPolicy?: string; + + /** + * Instead of actual bootstrapping, print the current + * CLI\'s bootstrapping template to stdout for customization + * @default false + */ + readonly showTemplate?: boolean; + + /** + * Use the template from the given file instead of the + * built-in one (use --show-template to obtain an example) + */ + readonly template?: string; + + /** + * Toggle CloudFormation termination protection on the + * bootstrap stacks + * @default false + */ + readonly terminationProtection?: boolean; + + /** + * Use the example permissions boundary. + * @default undefined + */ + readonly examplePermissionsBoundary?: boolean; + + /** + * Use the permissions boundary specified by name. + * @default undefined + */ + readonly customPermissionsBoundary?: string; + + /** + * Use previous values for existing parameters (you must specify + * all parameters on every deployment if this is disabled) + * @default true + */ + readonly usePreviousParameters?: boolean; + + /** + * Whether to execute ChangeSet (--no-execute will NOT execute + * the ChangeSet) + * @default true + */ + readonly execute?: boolean; + + /** + * String which must be unique for each bootstrap stack. You + * must configure it on your CDK app if you change this + * from the default. + * @default undefined + */ + readonly qualifier?: string; + + /** + * The AWS account IDs that should be trusted to perform + * deployments into this environment (may be repeated, + * modern bootstrapping only) + * @default undefined + */ + readonly trust?: string; + + /** + * The AWS account IDs that should be trusted to look + * up values in this environment (may be repeated, + * modern bootstrapping only) + * @default undefined + */ + readonly trustForLookup?: string; + + /** + * AWS KMS master key ID used for the SSE-KMS encryption + * @default undefined + */ + readonly bootstrapKmsKeyId?: string; + + /** + * Create a Customer Master Key (CMK) for the bootstrap + * bucket (you will be charged but can customize + * permissions, modern bootstrapping only) + * @default undefined + */ + readonly bootstrapCustomerKey?: string; + + /** + * Block public access configuration on CDK toolkit + * bucket (enabled by default) + * @default undefined + */ + readonly publicAccessBlockConfiguration?: string; +} \ No newline at end of file diff --git a/packages/@aws-cdk/cli-lib-alpha/lib/commands/index.ts b/packages/@aws-cdk/cli-lib-alpha/lib/commands/index.ts index 67262acc1d480..8c4e57e7d29ff 100644 --- a/packages/@aws-cdk/cli-lib-alpha/lib/commands/index.ts +++ b/packages/@aws-cdk/cli-lib-alpha/lib/commands/index.ts @@ -3,3 +3,4 @@ export * from './deploy'; export * from './destroy'; export * from './list'; export * from './synth'; +export * from './bootstrap'; diff --git a/packages/@aws-cdk/cli-lib-alpha/test/commands.test.ts b/packages/@aws-cdk/cli-lib-alpha/test/commands.test.ts index d2433139d68a1..bc2474dc9a24e 100644 --- a/packages/@aws-cdk/cli-lib-alpha/test/commands.test.ts +++ b/packages/@aws-cdk/cli-lib-alpha/test/commands.test.ts @@ -318,4 +318,16 @@ describe('list', () => { expect.anything(), ); }); + + test('bootstrap without options', async () => { + // WHEN + await cdk.bootstrap(); + + // THEN + expect(jest.mocked(cli.exec)).toHaveBeenCalledWith( + ['bootstrap', '--all'], + expect.anything(), + ); + }); + });