adrian-goe/nx-aws-cdk-v2

How to run cdk synthesis?

Opened this issue ยท 6 comments

I want to run cdk synth as stated here
https://docs.aws.amazon.com/cdk/v2/guide/cli.html#cli-synth

But when run this

 yarn cdk synth

I get this error:

$ /e/node_modules/.bin/cdk synthesize

--app is required either in command-line, in cdk.json or in ~/.cdk.json
error Command failed with exit code 1.

Then I figured out this plugin only 2 command is available, which is deploy and destroy.

Any chance I can run cdk synth ?

I made a workaround for this like following:

Assuming you created your cdk project in packages/cdk-stack, you can add another run target called synth in packages/cdk-stack/project.json. The resulting file should look like the following:

{
  "name": "cdk-stack",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "projectType": "application",
  "sourceRoot": "packages/cdk-stack/src",
  "targets": {
    "synth": {
      "executor": "nx:run-commands",
      "options": {
        "command": "cdk synth",
        "cwd": "packages/cdk-stack"
      }
    },
    "deploy": {
      "executor": "@ago-dev/nx-aws-cdk-v2:deploy",
      "options": {}
    },
    "destroy": {
      "executor": "@ago-dev/nx-aws-cdk-v2:destroy",
      "options": {}
    },
    "bootstrap": {
      "executor": "@ago-dev/nx-aws-cdk-v2:bootstrap",
      "options": {}
    },
    "lint": {
      "executor": "@nrwl/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": ["packages/cdk-stack/**/*.ts"]
      }
    },
    "test": {
      "executor": "@nrwl/jest:jest",
      "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
      "options": {
        "jestConfig": "packages/cdk-stack/jest.config.ts",
        "passWithNoTests": true
      }
    }
  },
  "tags": []
}

Then you can just run nx run cdk-stack:synth.

OH, im sorry, I saw this just now. Somehow this got lost in the stress of moving houses.

I will change the core a bit and pass all things down to the cdk. Unfortunately, this might take 1 or 2 weeks until i have more time for that.

@adrian-goe Thank you very much for this nx plugin.

In order to make the synth command work with library packages, it's also needed to change the app entry in cdk.json to "app": "npx ts-node -r tsconfig-paths/register --prefer-ts-exts bin/main.ts" and to install npm install --save-dev tsconfig-paths.
It would also be nice to have a typescript build command, but I was not able to figure it out because it looks like the ts-node config ignores tsconfig-paths.

+1 on this. Nx is truly an incredible platform, especially for something like IAC, and synth support would be really great to have

+1

As a follow on from @sandrokeil 's comment (which is a perfect workaround for the missing :synth command!), I found I had to replace all references of

"executor": "@ago-dev/nx-aws-cdk-v2:foo",
"options": {}

with

"executor": "nx:run-commands",
"options": {
  "command": "cdk foo",
  "cwd": "apps/cdk"
}

(where apps/cdk is just the path to my main cdk app called cdk; yours might be different)

Otherwise running any nx foo cdk command littered the project root with cdk.context.json and cdk.out/* files and not obey the output property in /apps/cdk/cdk.json.

This means all my commands now have to be nx run cdk:foo and not nx foo cdk as per the docs.

Not sure if this is also a bug as it does mean bypassing all the nx goodness? It could well be that I have something misconfigured.