Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/aws-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,8 @@ The table remains available throughout the entire process with zero downtime.

⚠️**CAUTION**⚠️: CDK Migrate is currently experimental and may have breaking changes in the future.

The generated CDK code is based on the CloudFormation template or AWS resources you migrate. This code runs on your machine when you run `cdk synth` or `cdk deploy`. Review it before you run either command.

CDK Migrate generates a CDK app from deployed AWS resources using `--from-scan`, deployed AWS CloudFormation stacks using `--from-stack`, and local AWS CloudFormation templates using `--from-path`.

To learn more about the CDK Migrate feature, see [Migrate to AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/migrate.html). For more information on `cdk migrate` command options, see [cdk migrate command reference](https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cdk-migrate.html).
Expand Down
13 changes: 12 additions & 1 deletion packages/aws-cdk/lib/cli/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,14 @@ export class CdkToolkit {
}
const stack = generateStack(generateTemplateOutput.migrateJson.templateBody, options.stackName, language);
await this.ioHost.asIoHelper().defaults.info(chalk.green(' ⏳ Generating CDK app for %s...'), chalk.blue(options.stackName));
await generateCdkApp(this.ioHost.asIoHelper(), options.stackName, stack!, language, options.outputPath, options.compress);
const generatedCodePath = await generateCdkApp(
this.ioHost.asIoHelper(),
options.stackName,
stack!,
language,
options.outputPath,
options.compress,
);
if (generateTemplateOutput) {
writeMigrateJsonFile(options.outputPath, options.stackName, generateTemplateOutput.migrateJson);
}
Expand All @@ -1397,6 +1404,10 @@ export class CdkToolkit {
generateTemplateOutput.resources!,
);
}
await this.ioHost.asIoHelper().defaults.warn(
` ⚠️ The generated CDK code is based on the CloudFormation template or AWS resources you migrated. Review %s before running ${chalk.blueBright('cdk synth')} or ${chalk.blueBright('cdk deploy')}.`,
generatedCodePath,
);
} catch (e) {
await this.ioHost.asIoHelper().defaults.error(' ❌ Migrate failed for `%s`: %s', options.stackName, (e as Error).message);
throw e;
Expand Down
5 changes: 4 additions & 1 deletion packages/aws-cdk/lib/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const MIGRATE_SUPPORTED_LANGUAGES: readonly string[] = cdk_from_cfn.supported_la
* @param stack - The yaml or json template for the stack
* @param language - The language to generate the CDK app in
* @param outputPath - The path at which to generate the CDK app
* @returns The path to the generated stack file, or to the zip file when `compress` is set
*/
export async function generateCdkApp(
ioHelper: IoHelper,
Expand All @@ -45,7 +46,7 @@ export async function generateCdkApp(
language: string,
outputPath?: string,
compress?: boolean,
): Promise<void> {
): Promise<string> {
const resolvedOutputPath = path.join(outputPath ?? process.cwd(), stackName);
const formattedStackName = decamelize(stackName);

Expand Down Expand Up @@ -91,7 +92,9 @@ export async function generateCdkApp(
if (compress) {
await zipDirectory(resolvedOutputPath, `${resolvedOutputPath}.zip`);
fs.rmSync(resolvedOutputPath, { recursive: true, force: true });
return `${resolvedOutputPath}.zip`;
}
return stackFileName;
} catch (error) {
fs.rmSync(resolvedOutputPath, { recursive: true, force: true });
throw error;
Expand Down
5 changes: 5 additions & 0 deletions packages/aws-cdk/test/cli/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import type { CloudFormationClientResolvedConfig, CreateChangeSetInput, CreateCh
import { CreateChangeSetCommand, DeleteChangeSetCommand, DescribeChangeSetCommand, DescribeStacksCommand, ExecuteChangeSetCommand, GetTemplateCommand, StackStatus } from '@aws-sdk/client-cloudformation';
import { GetParameterCommand } from '@aws-sdk/client-ssm';
import type { AwsStub } from 'aws-sdk-client-mock';
import chalk from 'chalk';
import * as fs from 'fs-extra';
import { type Template, type SdkProvider, ResourceImporter, StackAssembly, WorkGraphBuilder } from '../../lib/api';
import { mustMatch, selectExact, selectOnlySingle, selectWithUpstream } from '../../lib/api';
Expand Down Expand Up @@ -2865,6 +2866,10 @@ describe('migrate', () => {
expect(fs.pathExistsSync(path.join(workDir, 'SQSTypeScript', 'package.json'))).toBeTruthy();
expect(fs.pathExistsSync(path.join(workDir, 'SQSTypeScript', 'bin', 'sqs_type_script.ts'))).toBeTruthy();
expect(fs.pathExistsSync(path.join(workDir, 'SQSTypeScript', 'lib', 'sqs_type_script-stack.ts'))).toBeTruthy();
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
level: 'warn',
message: expect.stringContaining(`Review ${path.join(workDir, 'SQSTypeScript', 'lib', 'sqs_type_script-stack.ts')} before running ${chalk.blueBright('cdk synth')} or ${chalk.blueBright('cdk deploy')}`),
}));
Comment thread
sai-ray marked this conversation as resolved.
Resolved
});

cliTest('migrate succeeds for valid template from local path when language is provided', async (workDir) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/aws-cdk/test/commands/migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ describe('Migrate Function Tests', () => {

cliTest('generateCdkApp generates the expected cdk app when called for typescript', async (workDir) => {
const stack = generateStack(validTemplate, 'GoodTypeScript', 'typescript');
await generateCdkApp(ioHelper, 'GoodTypeScript', stack, 'typescript', workDir);
const generatedCodePath = await generateCdkApp(ioHelper, 'GoodTypeScript', stack, 'typescript', workDir);
expect(generatedCodePath).toEqual(path.join(workDir, 'GoodTypeScript', 'lib', 'good_type_script-stack.ts'));

// Packages exist in the correct spot
expect(fs.pathExistsSync(path.join(workDir, 'GoodTypeScript', 'package.json'))).toBeTruthy();
Expand Down Expand Up @@ -402,7 +403,8 @@ describe('Migrate Function Tests', () => {

cliTest('generatedCdkApp generates a zip file when --compress is used', async (workDir) => {
const stack = generateStack(validTemplate, 'GoodTypeScript', 'typescript');
await generateCdkApp(ioHelper, 'GoodTypeScript', stack, 'typescript', workDir, true);
const generatedCodePath = await generateCdkApp(ioHelper, 'GoodTypeScript', stack, 'typescript', workDir, true);
expect(generatedCodePath).toEqual(path.join(workDir, 'GoodTypeScript.zip'));

// Packages not in outDir
expect(fs.pathExistsSync(path.join(workDir, 'GoodTypeScript', 'package.json'))).toBeFalsy();
Expand Down
Loading