Skip to content

Commit

Permalink
feat(cli-lib): support bootstrap command (#26205)
Browse files Browse the repository at this point in the history
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: #15851 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
AntonioAngelino authored Jul 3, 2023
1 parent db923dd commit 9364e94
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/@aws-cdk/cli-lib-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Currently the package includes implementations for:

- `cdk deploy`
- `cdk synth`
- `cdk bootstrap`
- `cdk destroy`
- `cdk list`

Expand Down Expand Up @@ -95,6 +96,13 @@ cli.synth({
});
```

### bootstrap

```ts
// await this asynchronous method call using a language feature
cli.bootstrap();
```

### deploy

```ts
Expand Down
35 changes: 34 additions & 1 deletion packages/@aws-cdk/cli-lib-alpha/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +18,11 @@ export interface IAwsCdkCli {
*/
synth(options?: SynthOptions): Promise<void>;

/**
* cdk bootstrap
*/
bootstrap(options?: BootstrapOptions): Promise<void>;

/**
* cdk deploy
*/
Expand Down Expand Up @@ -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
*/
Expand Down
123 changes: 123 additions & 0 deletions packages/@aws-cdk/cli-lib-alpha/lib/commands/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/cli-lib-alpha/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './deploy';
export * from './destroy';
export * from './list';
export * from './synth';
export * from './bootstrap';
12 changes: 12 additions & 0 deletions packages/@aws-cdk/cli-lib-alpha/test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
});

});

0 comments on commit 9364e94

Please sign in to comment.