From fec4bb1c26db54bbb151bd05239e1fc1be5de657 Mon Sep 17 00:00:00 2001 From: Momo Kornher Date: Thu, 10 Oct 2024 11:28:36 +0100 Subject: [PATCH] fix(cli-lib): cannot bootstrap specific environment (#31713) ### Issue Closes https://github.com/aws/aws-cdk/issues/31656 ### Reason for this change When boostrapping, users may need to explicitly provide the environment if it can be determined from the app or shell environment. Previously this was not possible using the cli-lib-alpha package. ### Description of changes Add a new argument to support environments. ### Description of how you validated changes Unit test case was added. ### 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* --- packages/@aws-cdk/cli-lib-alpha/lib/cli.ts | 2 ++ .../@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts | 8 ++++++++ .../@aws-cdk/cli-lib-alpha/test/commands.test.ts | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/packages/@aws-cdk/cli-lib-alpha/lib/cli.ts b/packages/@aws-cdk/cli-lib-alpha/lib/cli.ts index aac53864eb983..1b96fa146ec4c 100644 --- a/packages/@aws-cdk/cli-lib-alpha/lib/cli.ts +++ b/packages/@aws-cdk/cli-lib-alpha/lib/cli.ts @@ -172,7 +172,9 @@ export class AwsCdkCli implements IAwsCdkCli { * cdk bootstrap */ public async bootstrap(options: BootstrapOptions = {}) { + const envs = options.environments ?? []; const bootstrapCommandArgs: string[] = [ + ...envs, ...renderBooleanArg('force', options.force), ...renderBooleanArg('show-template', options.showTemplate), ...renderBooleanArg('terminationProtection', options.terminationProtection), diff --git a/packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts b/packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts index 1dffabfdfb945..824a823be7808 100644 --- a/packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts +++ b/packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts @@ -4,6 +4,14 @@ import { SharedOptions } from './common'; * Options to use with cdk bootstrap */ export interface BootstrapOptions extends SharedOptions { + /** + * The target AWS environments to deploy the bootstrap stack to. + * Uses the following format: `aws:///` + * + * @example "aws://123456789012/us-east-1" + * @default - Bootstrap all environments referenced in the CDK app or determine an environment from local configuration. + */ + readonly environments?: string[]; /** * The name of the CDK toolkit stack to create 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 710bc278b2ff4..59f7080f12811 100644 --- a/packages/@aws-cdk/cli-lib-alpha/test/commands.test.ts +++ b/packages/@aws-cdk/cli-lib-alpha/test/commands.test.ts @@ -369,4 +369,16 @@ describe('list', () => { ); }); + test('bootstrap specific environment', async () => { + // WHEN + await cdk.bootstrap({ + environments: ['aws://123456789012/us-east-1'], + }); + + // THEN + expect(jest.mocked(cli.exec)).toHaveBeenCalledWith( + ['bootstrap', 'aws://123456789012/us-east-1', '--all'], + expect.anything(), + ); + }); });