Reputation: 14255
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.
There are several ways to use existing resources in a CDK app, for example:
S3.Bucket.fromBucketName()
cdk import
cdk migrate
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.
Here's a summary of cdk import steps for a normal app (not wrapped in a cdk-pipeline):
- Run a
cdk diff
to make sure your CDK stack has no pending changes. [...]- 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. [...]
- Run cdk import. If there are multiple stacks in the CDK app, pass a specific stack name as an argument.
- The CDK CLI will prompt you to pass in the actual names of the resources you are importing. [...]
- 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:
cdk diff
(without args) on a CDK-pipeline app only shows changes to the pipeline stack, without showing changes to the wrapped "application" stacks inside the pipeline stages.cdk deploy
for every change, as for a normal CDK app, changes to the pipeline app are triggered automatically by git push
to the repository.So, what is the proper workflow for importing an existing resource into a CDK-pipeline app?
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:
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.)git commit
the change. (Added the commit part)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
.)git push
to update the remote repository. This will trigger the pipeline.Does that seem right?
Upvotes: 1
Views: 161
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