terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

cdktf forces latest aws provider

Open howdoicomputer opened this issue 10 months ago • 2 comments

Expected Behavior

When specifying an AWS provider constraint in cdktf.json then that provider version should be respected during cdktf deploy

{
  "language": "typescript",
  "app": "npx ts-node main.ts",
  "projectId": "30757798-f725-4911-93f3-408b04e39e77",
  "sendCrashReports": "false",
  "terraformProviders": ["aws@~> 4.0"],
  "terraformModules": [],
  "context": {}
}

Actual Behavior

cdktf will resolve the latest provider version and use that instead to deploy. When cdktf provider list is ran, two versions of the same provider are installed.

Steps to Reproduce

  1. Run through the Build AWS infrastructure with CDK for Terraform tutorial
  2. Edit cdktf.json so that there is a version constraint locking the AWS provider to anything with the 4.0 range.

Example:

{
  "language": "typescript",
  "app": "npx ts-node main.ts",
  "projectId": "30757798-f725-4911-93f3-408b04e39e77",
  "sendCrashReports": "false",
  "terraformProviders": ["aws@~> 4.0"],
  "terraformModules": [],
  "context": {}
}
  1. Run cdktf provider get
  2. Run cdktf deploy
  3. Observe that the AWS provider being used to deploy resources is ~> 5.0

Versions

language: typescript cdktf-cli: 0.18.2 node: v18.18.0 cdktf: 0.18.2 constructs: 10.3.0 jsii: null terraform: 1.6.0-dev arch: x64 os: linux 6.1.56

Providers

[howdoicomputer@framework:~/workspace/aws-cdktf-example]$ cdktf provider list ┌───────────────┬──────────────────┬─────────┬────────────┬─────────────────────┬─────────────────┐ │ Provider Name │ Provider Version │ CDKTF │ Constraint │ Package Name │ Package Version │ ├───────────────┼──────────────────┼─────────┼────────────┼─────────────────────┼─────────────────┤ │ aws │ 4.67.0 │ │ ~> 4.0 │ │ │ ├───────────────┼──────────────────┼─────────┼────────────┼─────────────────────┼─────────────────┤ │ aws │ 5.20.1 │ ^0.18.0 │ │ @cdktf/provider-aws │ 17.0.10 │ └───────────────┴──────────────────┴─────────┴────────────┴─────────────────────┴─────────────────┘

Gist

https://gist.github.com/howdoicomputer/601137636cdc079799e1c2c7daee2b64

Possible Solutions

None.

Workarounds

None.

Anything Else?

No response

References

No response

Help Wanted

  • [X] I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

howdoicomputer avatar Oct 12 '23 21:10 howdoicomputer

The tutorial you are following is using the pre-built provider as you can see by the imports being @cdktf/provider-aws/... instead of ./.gen/providers/aws/.... You added the 4.x aws provider as a local provider binding so cdktf provider list shows both being installed, one in version 4 with local bindings and one in version 5 with pre-built ones. If you want to switch which one is used you can change your imports to the local format (and also remove the pre-built provider from your package.json)

DanielMSchmidt avatar Oct 13 '23 08:10 DanielMSchmidt

@DanielMSchmidt You are right but the tutorial probably should be updated. So following the tutorial from absolute scratch by making a learn-tf directory, cding into it, generating scaffolding, copy and pasting the tutorial code, and then running cdktf deploy I get:

[howdoicomputer@framework:~/workspace/learn-cdktf]$ cdktf deploy

⠋  Synthesizing
[2023-10-13T21:44:12.908] [ERROR] default - /home/howdoicomputer/workspace/learn-cdktf/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^

[2023-10-13T21:44:12.908] [ERROR] default - TSError: ⨯ Unable to compile TypeScript:
main.ts(3,29): error TS2307: Cannot find module '@cdktf/provider-aws/lib/provider' or its corresponding type declarations.
main.ts(4,26): error TS2307: Cannot find module '@cdktf/provider-aws/lib/instance' or its corresponding type declarations.
main.ts(28,5): error TS2304: Cannot find name 'RemoteBackend'.

This is after following every step to-the-letter in of the tutorial. However, I had missed this message from the scaffolding:

[2023-10-13T21:42:00.423] [INFO] default - Checking whether pre-built provider exists for the following constraints:
  provider: aws
  version : ~>4.0
  language: typescript
  cdktf   : 0.18.2

[2023-10-13T21:42:01.505] [INFO] default - Pre-built provider does not exist for the given constraints.
[2023-10-13T21:42:01.505] [INFO] default - Adding local provider registry.terraform.io/hashicorp/aws with version constraint ~>4.0 to cdktf.json
Local providers have been updated. Running cdktf get to update...
Generated typescript constructs in the output directory: .gen

The code that is in the tutorial assumes a pre-built provider. However, a pre-built provider wasn't matched for the constraint passed into the command line: cdktf init --template="typescript" --providers="aws@~>4.0". I initially fixed this by just using yarn to install an older prebuilt for the provider version that matched the tutorial.

Yeah, it's fixed if I change the import paths to the locally built provider but, like, it's the tutorial. I don't mind drafting a PR for this. Maybe just change the import statements in the tutorial code?

howdoicomputer avatar Oct 14 '23 05:10 howdoicomputer