How to setup cdktf with Typescript and Node in a Docker file to simplify the process for end-users?"

I am trying to create a Docker file that will include all the necessary packages and configurations for running the CDK for Terraform (cdktf) with Typescript and Node. The goal is to make it easier for end-users to quickly download and run the cdktf through a Docker container. Can anyone provide guidance on how to accomplish this task

I tried this without success:

FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
RUN npm install --global cdktf-cli@latest
CMD [ "sh" ]

and this is the error what I get:

/usr/local/bin/cdktf -> /usr/local/lib/node_modules/cdktf-cli/bundle/bin/cdktf
npm WARN ink-select-input@4.2.1 requires a peer of ink@^3.0.5 but none is installed. You must install peer dependencies yourself.
npm WARN ink-select-input@4.2.1 requires a peer of react@^16.5.2 || ^17.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN ink-table@3.0.0 requires a peer of ink@>=3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN ink-table@3.0.0 requires a peer of react@>=16.8.0 but none is installed. You must install peer dependencies yourself.
npm WARN graphology@0.25.1 requires a peer of graphology-types@>=0.24.0 but none is installed. You must install peer dependencies yourself.

this is my second try:

FROM public.ecr.aws/spacelift/runner-terraform:latest
WORKDIR /app
USER root
RUN apk add --no-cache nodejs npm
RUN npm install --global cdktf-cli@latest
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "sh" ]

and this is the error when I rund cdktf deploy:

/app # cdktf deploy "*"
[2023-02-08T23:01:08.036] [INFO] default - Unable to determine Terraform version: Error: Terraform CLI not present - Please install a current version https://learn.hashicorp.com/terraform/getting-started/install.html

0 Stacks deploying     2 Stacks done     0 Stacks waiting
Invoking Terraform CLI failed with exit code 1

Hi @fdervisi :wave:

CDKTF requires the terraform cli to be available in the PATH, alternatively you can set the TERRAFORM_BINARY_NAME environment variable to point to the terraform binary in that Docker container.

Alternatively, you could base your Docker image on the one we use in CI for testing CDKTF and install the CDKTF CLI there. That image however, contains all languages we support, so it might be a bit bigger than yours.

– Ansgar

1 Like

Thanks @ansgarm . I tried to pull your container and install the CDKTF for testing:

docker pull hashicorp/jsii-terraform
docker run -it hashicorp/jsii-terraform /bin/sh
# sudo npm install -g cdktf-cli
/usr/bin/cdktf -> /usr/lib/node_modules/cdktf-cli/bundle/bin/cdktf

> @cdktf/node-pty-prebuilt-multiarch@0.10.1-pre.10 install /usr/lib/node_modules/cdktf-cli/node_modules/@cdktf/node-pty-prebuilt-multiarch
> node scripts/check-prebuild.js || prebuild-install || node scripts/install.js

Prebuild binary exists: /usr/lib/node_modules/cdktf-cli/node_modules/@cdktf/node-pty-prebuilt-multiarch/prebuilds/linux-x64/node.abi83.node

> @cdktf/node-pty-prebuilt-multiarch@0.10.1-pre.10 postinstall /usr/lib/node_modules/cdktf-cli/node_modules/@cdktf/node-pty-prebuilt-multiarch
> node scripts/post-install.js

npm WARN ink-select-input@4.2.1 requires a peer of ink@^3.0.5 but none is installed. You must install peer dependencies yourself.
npm WARN ink-select-input@4.2.1 requires a peer of react@^16.5.2 || ^17.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN ink-table@3.0.0 requires a peer of ink@>=3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN ink-table@3.0.0 requires a peer of react@>=16.8.0 but none is installed. You must install peer dependencies yourself.
npm WARN graphology@0.25.1 requires a peer of graphology-types@>=0.24.0 but none is installed. You must install peer dependencies yourself.

+ cdktf-cli@0.15.4
added 311 packages from 208 contributors in 34.216s
# 

Do I miss something or how can I fix the issues with your docker container?

Hi @fdervisi :wave:

Is it actually failing? You can disregard those warning messages. The cdktf-cli package has those peer dependencies in case you want to programmatically invoke it using TypeScript.

The bundled CLI (invoked via cdktf) contains those dependencies.

– Ansgar

Hi @ansgarm ,

Unfortunately it does not work I used now this Dockerfile:

FROM hashicorp/jsii-terraform
WORKDIR /
COPY package*.json ./
RUN npm install
RUN npm install -g cdktf-cli
COPY . .

Inside the container I can start the cdktf deploy however I get this error now:

# cdktf deploy

[2023-02-10T12:15:07.493] [ERROR] default - npm
npm[2023-02-10T12:15:07.496] [ERROR] default -  WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

 WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

β Ό  Synthesizing
[2023-02-10T12:15:08.459] [ERROR] default - npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!
[2023-02-10T12:15:08.488] [ERROR] default - npm
npm[2023-02-10T12:15:08.489] [ERROR] default -  WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!


β Ή  Synthesizing
ERROR: cdktf encountered an error while synthesizing

Synth command: npx ts-node main.ts
Error:         The operation was aborted

Command output on stderr:

    npx: installed 17 in 5.192s




β ‡  Synthesizing
# 
# 
# cdktf deploy "*"

β §  Synthesizing
[2023-02-10T12:15:35.569] [ERROR] default - npm
npm[2023-02-10T12:15:35.581] [ERROR] default -  

β ‡  Synthesizing
[2023-02-10T12:15:35.589] [ERROR] default - read-shrinkwrap
read-shrinkwrap[2023-02-10T12:15:35.591] [ERROR] default -  This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!


⠏  Synthesizing
[2023-02-10T12:15:35.674] [ERROR] default - npm
npm[2023-02-10T12:15:35.676] [ERROR] default -  WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

 WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!
[2023-02-10T12:15:35.679] [ERROR] default - npm
npm[2023-02-10T12:15:35.682] [ERROR] default -  
 [2023-02-10T12:15:35.683] [ERROR] default - WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

Error: Backend initialization required: please run "terraform init"
          β”‚ 
          β”‚ Reason: Backend configuration block has changed
          β”‚ 
          β”‚ The "backend" is the interface that Terraform uses to store state,
          β”‚ perform operations, etc. If this message is showing up, it means that the
          β”‚ Terraform configuration you're using is using a custom configuration for
          β”‚ the Terraform backend.
          β”‚ 
          β”‚ Changes to backend configurations require reinitialization. This allows
          β”‚ Terraform to set up the new configuration, copy existing state, etc. Please
          β”‚ run
          β”‚ "terraform init" with either the "-reconfigure" or "-migrate-state" flags to
          β”‚ use the current configuration.
          β”‚ 
          β”‚ If the change reason above is incorrect, please verify your configuration
          β”‚ hasn't changed and try again. At this point, no changes to your existing
          β”‚ configuration or state have been made.
          β•΅
aws-eu-west-3  β•·
               β”‚ Error: Backend initialization required: please run "terraform init"
               β”‚ 
               β”‚ Reason: Backend configuration block has changed
               β”‚ 
               β”‚ The "backend" is the interface that Terraform uses to store state,
               β”‚ perform operations, etc. If this message is showing up, it means that the
               β”‚ Terraform configuration you're using is using a custom configuration for
               β”‚ the Terraform backend.
               β”‚ 
               β”‚ Changes to backend configurations require reinitialization. This allows
               β”‚ Terraform to set up the new configuration, copy existing state, etc. Please
               β”‚ run
               β”‚ "terraform init" with either the "-reconfigure" or "-migrate-state" flags to
               β”‚ use the current configuration.
               β”‚ 
               β”‚ If the change reason above is incorrect, please verify your configuration
               β”‚ hasn't changed and try again. At this point, no changes to your existing
               β”‚ configuration or state have been made.
               β•΅
aws-eu-south-1  β•·
                β”‚ Error: Backend initialization required: please run "terraform init"
                β”‚ 
                β”‚ Reason: Backend configuration block has changed
                β”‚ 
                β”‚ The "backend" is the interface that Terraform uses to store state,
                β”‚ perform operations, etc. If this message is showing up, it means that the
                β”‚ Terraform configuration you're using is using a custom configuration for
                β”‚ the Terraform backend.
                β”‚ 
                β”‚ Changes to backend configurations require reinitialization. This allows
                β”‚ Terraform to set up the new configuration, copy existing state, etc. Please
                β”‚ run
                β”‚ "terraform init" with either the "-reconfigure" or "-migrate-state" flags to
                β”‚ use the current configuration.
                β”‚ 
                β”‚ If the change reason above is incorrect, please verify your configuration
                β”‚ hasn't changed and try again. At this point, no changes to your existing
                β”‚ configuration or state have been made.
                β•΅


0 Stacks deploying     3 Stacks done     0 Stacks waiting
Invoking Terraform CLI failed with exit code 1

why do I need to do a terraform init with terraform cdk?

This might be unrelated to the actual error. What does npx ts-node main.ts return? That seems to fail before the rest. Or are those two errors?

I executed the command npx ts-node main.ts without encountering any errors. However, it’s possible that something else is missing besides npm install -g cdktf-cli ?

Hi @fdervisi :wave:

Does running cdktf synth work in the container?

Hi @ansgarm ,

cdktf synth works but the deployment not:

root@b9309ed8d192:/# cdktf synth
[2023-02-17T09:17:31.035] [ERROR] default - npm
npm[2023-02-17T09:17:31.043] [ERROR] default -  
 [2023-02-17T09:17:31.046] [ERROR] default - WARN
WARN[2023-02-17T09:17:31.047] [ERROR] default -  
 [2023-02-17T09:17:31.048] [ERROR] default - read-shrinkwrap
read-shrinkwrap[2023-02-17T09:17:31.049] [ERROR] default -  This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

 This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

β ‹  Synthesizing
[2023-02-17T09:17:32.459] [ERROR] default - npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!
[2023-02-17T09:17:32.465] [ERROR] default - npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!


β ΄  Synthesizing

Generated Terraform code for the stacks: AwsStack, AzureStack

However the deployment fails:

root@b9309ed8d192:/# cdktf deploy "AwsStack"

β Ό  Synthesizing
[2023-02-17T09:18:56.912] [ERROR] default - npm
npm[2023-02-17T09:18:56.915] [ERROR] default -  WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!


β Έ  Synthesizing

β Ό  Synthesizing
[2023-02-17T09:18:57.660] [ERROR] default - WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!


⠏  Synthesizing
[2023-02-17T09:18:58.074] [ERROR] default - npm
npm[2023-02-17T09:18:58.079] [ERROR] default -  
 [2023-02-17T09:18:58.081] [ERROR] default - WARN
WARN[2023-02-17T09:18:58.082] [ERROR] default -  
 [2023-02-17T09:18:58.083] [ERROR] default - read-shrinkwrap
read-shrinkwrap[2023-02-17T09:18:58.083] [ERROR] default -  This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!


β Ό  Synthesizing
AwsStack  β•·
AwsStack  β”‚ Error: Backend initialization required: please run "terraform init"
          β”‚ 
          β”‚ Reason: Backend configuration block has changed
          β”‚ 
          β”‚ The "backend" is the interface that Terraform uses to store state,
          β”‚ perform operations, etc. If this message is showing up, it means that the
          β”‚ Terraform configuration you're using is using a custom configuration for
          β”‚ the Terraform backend.
          β”‚ 
          β”‚ Changes to backend configurations require reinitialization. This allows
          β”‚ Terraform to set up the new configuration, copy existing state, etc. Please
          β”‚ run
          β”‚ "terraform init" with either the "-reconfigure" or "-migrate-state" flags to
          β”‚ use the current configuration.
          β”‚ 
          β”‚ If the change reason above is incorrect, please verify your configuration
          β”‚ hasn't changed and try again. At this point, no changes to your existing
          β”‚ configuration or state have been made.
          β•΅


0 Stacks deploying     1 Stack done     0 Stacks waiting
Invoking Terraform CLI failed with exit code 1
root@b9309ed8d192:/# 

I pushed the code in me public repo where you could reproduce this issue:

Hi @fdervisi :wave:

I saw that you example repository used CDKTF 0.13.3 – does this also fail when using the latest version? There have been a few bugs fixed since that version that might relate to this.