13

I'm trying to initialize the AWS CDK on a new website I just created via Visual Studio.

But when I run the init commmand I get the error: cdk init cannot be run in a non-empty directory

❯ cdk init app --language=csharp
`cdk init` cannot be run in a non-empty directory!

The CDK Workshop uses the example of starting from scrach (in an empty directory).

Is it possible to initialize the CDK inside an existing solution/project?

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123

2 Answers2

10

cdk init creates a basic structure and a skeleton, such as an app and subdirectory in which it creates a stack.

You can run it in an empty directory, back up the files you will override, and then copy an actual cdk application/project (such as workshop one) into it.

I use this way to create another CDK project when I re-use the existing one.

fabrizioM
  • 46,639
  • 15
  • 102
  • 119
mon
  • 18,789
  • 22
  • 112
  • 205
  • 3
    Using the [--generate-only](https://docs.aws.amazon.com/cdk/v2/guide/cli.html#cli-ref) flag makes this task easier as it only creates the bare minimum files necessary to make a project a "CDK" project. – Nick A. Watts May 16 '22 at 18:37
  • @mon By "back up" you mean just "delete", since you won't be putting those files back anyway? – MEMark Jul 16 '22 at 11:58
  • @MEMark, basically yes, but preserve them so that we can compare just in case CDK has made some changes. – mon Feb 14 '23 at 01:55
5

At the moment there is no way to execute cdk init in a non-empty directory. This is a safety net against cluttering a directory with the CDK files where they might not be wanted.

That said, there is no practical reason a command flag to override this protection couldnt be added. You could consider putting in a feature-request on the repo.

Noah Litov
  • 96
  • 4