Skip to content

Commit

Permalink
chore(migrate): use import to deploy
Browse files Browse the repository at this point in the history
When the migrate.json file exists and the env is the same as the stack being deployed, use import changeset.
  • Loading branch information
TheRealAmazonKendra committed Dec 15, 2023
1 parent 15c7bb2 commit 13ab598
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Deployments } from './api/deployments';
import { HotswapMode } from './api/hotswap/common';
import { findCloudWatchLogGroups } from './api/logs/find-cloudwatch-logs';
import { CloudWatchLogEventMonitor } from './api/logs/logs-monitor';
import { ResourcesToImport } from './api/util/cloudformation';
import { StackActivityProgress } from './api/util/cloudformation/stack-activity-monitor';
import { generateCdkApp, generateStack, readFromPath, readFromStack, setEnvironment, validateSourceOptions } from './commands/migrate';
import { printSecurityDiff, printStackDiff, RequireApproval } from './diff';
Expand Down Expand Up @@ -283,6 +284,8 @@ export class CdkToolkit {
tags = tagsForStack(stack);
}

const resourcesToImport = this.tryGetMigrateResourcesToImport(stack.environment);

let elapsedDeployTime = 0;
try {
const result = await this.props.deployments.deployStack({
Expand All @@ -305,6 +308,7 @@ export class CdkToolkit {
hotswap: options.hotswap,
extraUserAgent: options.extraUserAgent,
assetParallelism: options.assetParallelism,
resourcesToImport,
});

const message = result.noOp
Expand All @@ -329,6 +333,11 @@ export class CdkToolkit {
print('Stack ARN:');

data(result.stackArn);

if (resourcesToImport) {
fs.rmSync('migrate.json');
}

} catch (e) {
error('\n ❌ %s failed: %s', chalk.bold(stack.displayName), e);
throw e;
Expand Down Expand Up @@ -858,6 +867,30 @@ export class CdkToolkit {
stackName: assetNode.parentStack.stackName,
}));
}

/**
* Checks to see if a migrate.json file exists. If it does and the source is either `filepath` or
* is in the same environment as the stack deployment and returns the cdk migrate resources to be
* imported into the new stack.
* @param env The environment to which the stack is being deployed
* @returns The resources to import into the stack
*/
private tryGetMigrateResourcesToImport(env: cxapi.Environment): ResourcesToImport | undefined {
try {
const migrateFile = fs.readJsonSync('migrate.json', { encoding: 'utf-8' });
if (migrateFile.Source === 'localfile') {
return migrateFile.Resources;
}
const sourceEnv = (migrateFile.Source as string).split(':');
if (sourceEnv[4] === env.account && sourceEnv[3] === env.region) {
return migrateFile.Resources;
}
return undefined;
} catch (e) {
// No migrate file is present, resume deployment as usual
return undefined;
}
}
}

export interface DiffOptions {
Expand Down

0 comments on commit 13ab598

Please sign in to comment.