Skip to content
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

feat(cli): support CloudFormation simplified resource import #29087

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

tmokmss
Copy link
Contributor

@tmokmss tmokmss commented Feb 13, 2024

Issue # (if applicable)

Closes #28060.

Reason for this change

This feature allows to automatically import exsting resources with the same physical name, such as S3 bucket, DDB table, etc, during a CFn deployment.

Because resource import is a vital feature for CDK users e.g. to refactor a construct tree, cdk migrate, etc, it would benefit many potential users if cdk natively support it.

Description of changes

This PR adds a CLI option --import-exsting-resources: boolean to cdk deploy command and pass it to createChangeSet API call.

Description of how you validated changes

Added a cli integ test.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team February 13, 2024 07:21
@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1 star-contributor [Pilot] contributed between 25-49 PRs to the CDK labels Feb 13, 2024
@aws-cdk-automation aws-cdk-automation removed pr/needs-maintainer-review This PR needs a review from a Core Team Member labels Apr 9, 2024
@tmokmss
Copy link
Contributor Author

tmokmss commented Apr 10, 2024

@SankyRed Thanks! I guess we also need to add pr-linter/exempt-integ-test label to pass the linter?

@SankyRed SankyRed added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Apr 10, 2024
@aws-cdk-automation aws-cdk-automation dismissed their stale review April 10, 2024 03:19

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Apr 10, 2024
Copy link
Contributor

@comcalvi comcalvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably want this feature on diff changesets as well... @TheRealAmazonKendra what do you think?

Comment on lines 424 to +427
ResourcesToImport: this.options.resourcesToImport,
Description: `CDK Changeset for execution ${this.uuid}`,
ClientToken: `create${this.uuid}`,
ImportExistingResources: importExistingResources,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if they specify both ResourcesToImport and ImportExistingResources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it will not work, but the spec says nothing about such restriction, so I would like to keep it as-is.

Actually users cannot set both ImportExistingResources and ResourcesToImport from the CDK CLI, because the former is only used in cdk deploy, and cdk deploy does not have any command argument to directly set ResourcesToImport.

Copy link
Contributor

@HBobertz HBobertz Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec isn't really the source of truth we all wish it was so it's not uncommon we enforce logical things the spec does not. However in this specific instance I believe it is correct not to enforce these arguments be exclusive because there is a difference between the resources supported by importExistingResources and ResourcesToImport. Auto-import is a subset of resources supported by ResourcesToImport so it is entirely possible to have both. It is entirely possible then someone could build a command which fails, but I believe this won't really be a common occurence and this is fine to leave as is

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Apr 23, 2024
@rachit-shah
Copy link

@comcalvi @tmokmss @TheRealAmazonKendra
We use CDK deployment and face resource conflicts often. We'd like to avoid calling CFN APIs directly since we are using CDK app.

What exactly is this PR blocked on? It has been months since the PR was raised. If it's adding this feature in a different cdk operation (cdk diff), can that be a separate PR/issue?

@mergify mergify bot dismissed comcalvi’s stale review September 17, 2024 04:08

Pull request has been modified.

@tmokmss
Copy link
Contributor Author

tmokmss commented Sep 17, 2024

@comcalvi Hi, I resolved the merge conflict. Can you take a look at this once again?

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Sep 17, 2024
Comment on lines 424 to +427
ResourcesToImport: this.options.resourcesToImport,
Description: `CDK Changeset for execution ${this.uuid}`,
ClientToken: `create${this.uuid}`,
ImportExistingResources: importExistingResources,
Copy link
Contributor

@HBobertz HBobertz Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec isn't really the source of truth we all wish it was so it's not uncommon we enforce logical things the spec does not. However in this specific instance I believe it is correct not to enforce these arguments be exclusive because there is a difference between the resources supported by importExistingResources and ResourcesToImport. Auto-import is a subset of resources supported by ResourcesToImport so it is entirely possible to have both. It is entirely possible then someone could build a command which fails, but I believe this won't really be a common occurence and this is fine to leave as is

Comment on lines 399 to 402
This allows you to automatically import the resources in your template that already
exist in your AWS account during CloudFormation deployments.
With this feature, you can reduce the manual effort of import operations and avoid
deployment failures because of naming conflicts.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This allows you to automatically import the resources in your template that already
exist in your AWS account during CloudFormation deployments.
With this feature, you can reduce the manual effort of import operations and avoid
deployment failures because of naming conflicts.
Automatically import resources in your CDK application which represent
unmanaged resources in your account.
Reduces the manual effort of import operations and avoids
deployment failures due to naming conflicts with unmanaged resources in your account

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding some active voice to be more similar to how our other command readme's are written.

packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Sep 19, 2024
packages/aws-cdk/lib/api/deploy-stack.ts Outdated Show resolved Hide resolved
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
@@ -880,6 +880,41 @@ describe('disable rollback', () => {

});

describe('import-existing-resources', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a CLI integ test that creates a single resource and then imports it with this new flag?

Copy link
Contributor Author

@tmokmss tmokmss Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@comcalvi
Thanks, but I don't fully agree with implementing such test on CDK side. Because:

  1. Such test would more or less complicate the test code, not noly the success case, but also ensuring there is no orphaned resource when test failed halfway through. This possibly increases maintenance cost of the cli integ test code.
  2. It is not CDK's responsibility to ensure that a resource is successfully imported when the flag is set. It is CFn's responsibility to guarantee the functionality. CDK should just make sure the flag is set properly (as tested in the current code.)

What do you think?

@comcalvi
Copy link
Contributor

Existing Resource Import Methods

CDK has 3 ways to import stuff into a CDK stack:

  • From CFN:
    • CfnInclude
  • From AWS, but not in CFN:
    • cdk import
  • From either:
    • cdk migrate

CfnInclude requires you to create a CDK Stack with the same name as the CFN Stack, and imports everything from that CFN stack into CDK. This doesn't rely on any special changeset operation, it's just a regular stack update.

cdk import lets you specify...I think at least one resource ID to import into your stack. This resource is external to CDK / CFN. This executes an IMPORT type changeset. The key is that this type of changeset can only contain import operations; if you do anything else, it will fail.

cdk migrate also uses an import type changeset to import resources, but it also allows you to perform other operations; this is because cdk migrate first performs the import deployment, and then performs a second deployment containing any additional operations.

The New Resource Import Method

CFN launched a feature that allows a changeset to absorb new resources into the stack. This is specified as a flag on the changeset and it effectively turns any "Resource already exists" errors into resource import operations. This means that if your stack attempts to create a resource with the same physical ID as another resource in the same environment, it will import it instead.

It's unclear if this flag works with IMPORT type changesets. Those changesets should not create anything, but autoimport likely needs a CREATE to import the resource...maybe IMPORT changesets recognize that?

That doesn't affect this PR though, because this, conceptually, is a standard deploy option. It's saying "perform a regular deployment, but instead of "create this resource and throw if it doesn't exist", it's "try to create this resource, and import it if it already exists"

Bottom Line

We should add this as a flag to cdk deploy. --import-existing-resources is a good flag name.

@mergify mergify bot dismissed stale reviews from HBobertz and comcalvi September 20, 2024 00:51

Pull request has been modified.

Co-authored-by: Hogan Bobertz <[email protected]>
Co-authored-by: Calvin Combs <[email protected]>
@tmokmss
Copy link
Contributor Author

tmokmss commented Sep 20, 2024

@HBobertz @comcalvi

Thanks! I addressed all the comments from you :)

It's unclear if this flag works with IMPORT type changesets. Those changesets should not create anything, but autoimport likely needs a CREATE to import the resource...maybe IMPORT changesets recognize that?

As far as I tested, CFn decides whether a resource is created or imported when we create a changeset. If a resource will be imported, the changeset type for the resource will become IMPORT, and if not, it wil become CREATE.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: ced4fb1
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1 pr/needs-maintainer-review This PR needs a review from a Core Team Member pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested pr-linter/do-not-close The PR linter will not close this PR while this label is present pr-linter/exempt-integ-test The PR linter will not require integ test changes star-contributor [Pilot] contributed between 25-49 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cli: support CloudFormation simplified resource import
8 participants