-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(migrate): enable import of resources on apps created from cdk migrate #28678
Changes from 3 commits
ba72d74
02a2fd2
f4cc8d3
05a7e7c
7623593
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,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 { createDiffChangeSet } from './api/util/cloudformation'; | ||
import { createDiffChangeSet, 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'; | ||
|
@@ -205,6 +205,8 @@ export class CdkToolkit { | |
const elapsedSynthTime = new Date().getTime() - startSynthTime; | ||
print('\n✨ Synthesis time: %ss\n', formatTime(elapsedSynthTime)); | ||
|
||
await this.tryMigrateResources(stackCollection, options); | ||
|
||
const requireApproval = options.requireApproval ?? RequireApproval.Broadening; | ||
|
||
const parameterMap = buildParameterMap(options.parameters); | ||
|
@@ -539,9 +541,7 @@ export class CdkToolkit { | |
// Import the resources according to the given mapping | ||
print('%s: importing resources into stack...', chalk.bold(stack.displayName)); | ||
const tags = tagsForStack(stack); | ||
await resourceImporter.importResources(actualImport, { | ||
stack, | ||
deployName: stack.stackName, | ||
await resourceImporter.importResourcesFromMap(actualImport, { | ||
roleArn: options.roleArn, | ||
toolkitStackName: options.toolkitStackName, | ||
tags, | ||
|
@@ -874,6 +874,63 @@ 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, a new stack is created and the resources are | ||
* migrated to the stack using an IMPORT changeset. The normal deployment will resume after this is complete | ||
* to add back in any outputs and the CDKMetadata. | ||
*/ | ||
private async tryMigrateResources(stacks: StackCollection, options: DeployOptions): Promise<void> { | ||
const stack = stacks.stackArtifacts[0]; | ||
const migrateDeployment = new ResourceImporter(stack, this.props.deployments); | ||
const resourcesToImport = await this.tryGetResources(migrateDeployment); | ||
|
||
if (resourcesToImport) { | ||
print('%s: creating stack for resource migration...', chalk.bold(stack.displayName)); | ||
print('%s: importing resources into stack...', chalk.bold(stack.displayName)); | ||
|
||
await this.performResourceMigration(migrateDeployment, resourcesToImport, options); | ||
|
||
fs.rmSync('migrate.json'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is a non-blocking comment. I don't have the full picture of migrate, just want to call attention to this and if there's no problem, then ignore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intent here was to make deployment after using cdk migrate to generate an app zero touch, or as close to it as possible. CDK migrate generates this file that contains all the import data needed. So, the attempt at an import is only made when this file is present. Since the import only needs to happen on the first deployment, it deletes it at the end so that future deployments are not of the import type. |
||
print('%s: applying CDKMetadata and Outputs to stack (if applicable)...', chalk.bold(stack.displayName)); | ||
} | ||
} | ||
|
||
private async tryGetResources(migrateDeployment: ResourceImporter) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we swap the order of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do |
||
try { | ||
const migrateFile = fs.readJsonSync('migrate.json', { encoding: 'utf-8' }); | ||
const sourceEnv = (migrateFile.Source as string).split(':'); | ||
const environment = await migrateDeployment.resolveEnvironment(); | ||
if (sourceEnv[0] === 'localfile' || | ||
(sourceEnv[4] === environment.account && sourceEnv[3] === environment.region)) { | ||
return migrateFile.Resources; | ||
} | ||
} catch (e) { | ||
// Nothing to do | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new stack with just the resources to be migrated | ||
*/ | ||
private async performResourceMigration(migrateDeployment: ResourceImporter, resourcesToImport: ResourcesToImport, options: DeployOptions) { | ||
const startDeployTime = new Date().getTime(); | ||
let elapsedDeployTime = 0; | ||
|
||
// Initial Deployment | ||
await migrateDeployment.importResourcesFromMigrate(resourcesToImport, { | ||
roleArn: options.roleArn, | ||
toolkitStackName: options.toolkitStackName, | ||
deploymentMethod: options.deploymentMethod, | ||
usePreviousParameters: true, | ||
progress: options.progress, | ||
rollback: options.rollback, | ||
}); | ||
|
||
elapsedDeployTime = new Date().getTime() - startDeployTime; | ||
print('\n✨ Resource migration time: %ss\n', formatTime(elapsedDeployTime)); | ||
} | ||
} | ||
|
||
export interface DiffOptions { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about the other stacks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CDK migrate only creates one stack. It is not compatible with multiple stacks at this time.