CDK for Terraformのプロジェクトを作成する!(TypeScript)

TypeScriptを使用するCDK for Terraformのプロジェクトを作成します。リポジトリは、AWS CodeCommitを使用します。

AWS CodeCommitのリポジトリを作成する!

以下の記事で作成したリポジトリを使用します。

Cloneしたリポジトリのディレクトリに移動する!

Cloneしたリポジトリのディレクトリに移動します。

$ cd sample-repo

TypeScriptを使用するプロジェクトを作成する!

TypeScriptを使用するプロジェクトを作成します。

$ cdktf init --template=typescript
Welcome to CDK for Terraform!

By default, cdktf allows you to manage the state of your stacks using Terraform Cloud for free.
cdktf will request an API token for app.terraform.io using your browser.

If login is successful, cdktf will store the token in plain text in
the following file for use by subsequent Terraform commands:
    /home/usradmin/.terraform.d/credentials.tfrc.json

Note: The local storage mode isn't recommended for storing the state of your stacks.

? Do you want to continue with Terraform Cloud remote state management? no
? Project Name sample_repo
? Project Description A simple getting started project for cdktf.
? Do you want to start from an existing Terraform project? no
? Do you want to send crash reports to the CDKTF team? Refer to 
https://developer.hashicorp.com/terraform/cdktf/create-and-deploy/configuration-file#enable-crash-reporting-for-the-cli 
for more information yes
Note: You can always add providers using 'cdktf provider add' later on
? What providers do you want to use? aws

added 2 packages, and audited 57 packages in 9s

7 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

added 303 packages, and audited 360 packages in 31s

37 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
========================================================================================================

  Your cdktf typescript project is ready!

  cat help                Print this message

  Compile:
    npm run get           Import/update Terraform providers and modules (you should check-in this directory)
    npm run compile       Compile typescript code to javascript (or "npm run watch")
    npm run watch         Watch for changes and compile typescript in the background
    npm run build         Compile typescript

  Synthesize:
    cdktf synth [stack]   Synthesize Terraform resources from stacks to cdktf.out/ (ready for 'terraform apply')

  Diff:
    cdktf diff [stack]    Perform a diff (terraform plan) for the given stack

  Deploy:
    cdktf deploy [stack]  Deploy the given stack

  Destroy:
    cdktf destroy [stack] Destroy the stack

  Test:
    npm run test        Runs unit tests (edit __tests__/main-test.ts to add your own tests)
    npm run test:watch  Watches the tests and reruns them on change

  Upgrades:
    npm run upgrade        Upgrade cdktf modules to latest version
    npm run upgrade:next   Upgrade cdktf modules to latest "@next" version (last commit)

 Use Providers:

  You can add prebuilt providers (if available) or locally generated ones using the add command:
  
  cdktf provider add "aws@~>3.0" null kreuzwerker/docker

  You can find all prebuilt providers on npm: https://www.npmjs.com/search?q=keywords:cdktf
  You can also install these providers directly through npm:

  npm install @cdktf/provider-aws
  npm install @cdktf/provider-google
  npm install @cdktf/provider-azurerm
  npm install @cdktf/provider-docker
  npm install @cdktf/provider-github
  npm install @cdktf/provider-null

  You can also build any module or provider locally. Learn more https://cdk.tf/modules-and-providers

========================================================================================================

[2023-08-03T07:13:00.325] [INFO] default - Checking whether pre-built provider exists for the following constraints:
  provider: aws
  version : latest
  language: typescript
  cdktf   : 0.17.3

[2023-08-03T07:13:08.330] [INFO] default - Found pre-built provider.
Adding package @cdktf/provider-aws @ 16.0.5
[2023-08-03T07:13:08.350] [INFO] default - Installing package @cdktf/provider-aws @ 16.0.5 using npm.
[2023-08-03T07:13:27.467] [INFO] default - Package installed.

おわりに

プロジェクトの作成が完了したら、Cloud9の環境の場合はCloud9のエディタ、ローカルの環境の場合はVisual Studio Codeを使用して、CDK for Terraformの開発を開始することができます。

参考情報

関連記事