djvg
djvg

Reputation: 14255

How to import existing resources into a cdk-pipelines app?

Note this question refers to CDK-pipelines, which use the aws-cdk-lib.pipelines module. That is not the same as the CodePipeline module aws-cdk-lib.aws_codepipeline.

background

There are several ways to use existing resources in a CDK app, for example:

Then there's also the CloudFormation IaC generator to help with imports.

The docs and examples are mostly sufficient to get imports working.

However, as far as I know, the docs do not explain (explicitly) how the import mechanism applies to CDK-pipelines.

question

Here's a summary of cdk import steps for a normal app (not wrapped in a cdk-pipeline):

  1. Run a cdk diff to make sure your CDK stack has no pending changes. [...]
  2. Add constructs for the resources you want to import to your stack. [...] Do not add any other changes. You must also make sure to exactly model the state that the resource currently has. [...]
  3. Run cdk import. If there are multiple stacks in the CDK app, pass a specific stack name as an argument.
  4. The CDK CLI will prompt you to pass in the actual names of the resources you are importing. [...]
  5. When cdk import reports success, the resource will be managed by the CDK. [...]

However, it is not completely clear to me how this would work for a CDK-pipelines app.

My confusion comes from the fact that the typical deployment workflow for a CDK-pipeline is different from a normal CDK app. For example:

So, what is the proper workflow for importing an existing resource into a CDK-pipeline app?

what I tried

The following steps appear to do the job, but I could not find any documentation to confirm that this is the proper workflow.

Basically I used the steps described above, with some minor modifications and some additional steps at the end:

  1. Run cdk diff '**' to make sure there are no pending changes. (Added the stack name wildcard, as explained in fedonev's answer. The quotes are important.)
  2. Add constructs for the resources you want to import to your stack, and git commit the change. (Added the commit part)
  3. Run cdk import <stackname>. (There are multiple stacks, so we need to specify the stackname of the stack that imports the resource. Stack names can be found by running cdk list.)
  4. The CDK CLI will prompt you to pass in the actual names of the resources you are importing. After you provide this information, import will begin. (Unmodified)
  5. When cdk import reports success, the resource will be managed by the CDK. Any subsequent changes in the construct configuration will be reflected on the resource. (Unmodified)
  6. Run drift detection on the modified stack, in CloudFormation, to verify there is no drift.
  7. Run git push to update the remote repository. This will trigger the pipeline.

Does that seem right?

additional notes

Upvotes: 1

Views: 161

Answers (1)

gshpychka
gshpychka

Reputation: 11512

The fact that the stacks are deployed with CDK pipelines should not affect the process for importing at all.

All the underlying app stacks are still exposed as regular stacks (you can confirm this by running cdk ls).

Simply following the regular process but using the correct Stack IDs (obtained from cdk ls) should be sufficient.

Whether you let the pipeline perform the deployment by pushing it to git, or do cdk deploy locally, the result should be identical.

Upvotes: 2

Related Questions