Terraform for Code Engine のセットアップ

Terraform on IBM Cloud® は、予測可能で一貫性のある IBM Cloud サービスの作成を可能にし、Infrastructure as Code ( IaC ) の原則に従って複雑で多層なクラウド環境を迅速に構築することができる。 IBM Cloud CLI や API、SDK を使用するのと同様に、 HashiCorp Configuration Language (HCL) を使用して、 Code Engine ワークロードの作成、更新、削除を自動化できます。

管理対象の Terraform on IBM Cloud® ソリューションをお探しですか? IBM Cloud® Schematics をお試しください。 Schematics では、使い慣れた Terraform スクリプト言語を使用できますが、Terraform コマンド・ラインおよび IBM Cloud® Provider プラグインのセットアップと保守について心配する必要はありません。Schematics には、IBM Cloud® カタログから簡単にインストールできる定義済みの Terraform テンプレートも用意されています。

Terraform のインストールと Code Engine 用のリソースの構成

Terraform を使用してワークロードを作成する前に、以下の前提条件を満たしていることを確認してください。

  • Code Engine リソースの作成と操作に 必要なアクセス権を持って いることを確認してください。
  • Terraform CLI をインストールし、Terraform 用の IBM Cloud プロバイダー・プラグインを構成します。 詳しくは、 IBM Cloud® のチュートリアルを参照してください。 このプラグインは、以下のタスクを実行するために使用される IBM Cloud API を抽象化します。
  • main.tfという名前の Terraform 構成ファイルを作成します。 このファイルでは、 HashiCorp 構成言語を使用してリソースを定義します。 詳しくは Terraformのドキュメントを参照。
  1. main.tf ファイル内の code_engine_project リソース引数を使用して、 Code Engine プロジェクトを作成します。

    data "ibm_resource_group" "resource_group" {
       name = "<your_resource_group_name>"
    }
    
    resource "ibm_code_engine_project" "code_engine_project_instance" {
       name = "<your_project_name>"
       resource_group_id = data.ibm_resource_group.resource_group.id
    }
    
  2. 構成ファイルの作成が完了したら、Terraform CLI を初期化します。 詳しくは、 作業ディレクトリーの初期化を参照してください。

    terraform init
    
  3. main.tf ファイルからプロジェクトをプロビジョンします。 詳しくは、 Terraform を使用したインフラストラクチャーのプロビジョニングを参照してください。

    1. terraform plan を実行して、提案されたアクションをプレビューするための Terraform 実行プランを生成します。

      terraform plan
      
      data.ibm_resource_group.resource_group: Reading...
      data.ibm_resource_group.resource_group: Read complete after 1s [id=<your_resource_group_id>]
      
      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
         + create
      
      Terraform will perform the following actions:
      
         # ibm_code_engine_project.code_engine_project_instance will be created
         + resource "ibm_code_engine_project" "code_engine_project_instance" {
            + account_id        = (known after apply)
            + created_at        = (known after apply)
            + crn               = (known after apply)
            + href              = (known after apply)
            + id                = (known after apply)
            + name              = "<your_project_name>"
            + project_id        = (known after apply)
            + region            = (known after apply)
            + resource_group_id = "<your_resource_group_id>"
            + resource_type     = (known after apply)
            + status            = (known after apply)
         }
      
      Plan: 1 to add, 0 to change, 0 to destroy.
      
    2. terraform apply を実行して、計画に定義されているリソースを作成します。

      terraform apply
      
      data.ibm_resource_group.resource_group: Reading...
      data.ibm_resource_group.resource_group: Read complete after 1s [id=<your_resource_group_id>]
      
      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
         + create
      
      Terraform will perform the following actions:
      
         # ibm_code_engine_project.code_engine_project_instance will be created
         + resource "ibm_code_engine_project" "code_engine_project_instance" {
            + account_id        = (known after apply)
            + created_at        = (known after apply)
            + crn               = (known after apply)
            + href              = (known after apply)
            + id                = (known after apply)
            + name              = "my-project"
            + project_id        = (known after apply)
            + region            = (known after apply)
            + resource_group_id = "5218b5c8c749454b9fc8be28f32e458e"
            + resource_type     = (known after apply)
            + status            = (known after apply)
         }
      
      Plan: 1 to add, 0 to change, 0 to destroy.
      
      Changes to Outputs:
         + ibm_code_engine_project = {
            + account_id        = (known after apply)
            + created_at        = (known after apply)
            + crn               = (known after apply)
            + href              = (known after apply)
            + id                = (known after apply)
            + name              = "my-project"
            + project_id        = (known after apply)
            + region            = (known after apply)
            + resource_group_id = "5218b5c8c749454b9fc8be28f32e458e"
            + resource_type     = (known after apply)
            + status            = (known after apply)
            + timeouts          = null
         }
      
      Do you want to perform these actions?
         Terraform will perform the actions described above.
         Only 'yes' will be accepted to approve.
      
         Enter a value: yes
      
      ibm_code_engine_project.code_engine_project_instance: Creating...
      ibm_code_engine_project.code_engine_project_instance: Still creating... [10s elapsed]
      ibm_code_engine_project.code_engine_project_instance: Still creating... [20s elapsed]
      ibm_code_engine_project.code_engine_project_instance: Still creating... [30s elapsed]
      ibm_code_engine_project.code_engine_project_instance: Creation complete after 30s [id=13468527-84sa-619g-gr99-168fe364w98f]
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      ibm_code_engine_project = {
         "account_id" = "<your_account_id>"
         "created_at" = "2023-03-16T10:44:24.150838188Z"
         "crn" = "crn:v1:bluemix:public:codeengine:eu-de:a/dc5e6392e5d14021846031b83e95f190:13468527-84sa-619g-gr99-168fe364w98f::"
         "href" = "https://api.eu-de.codeengine.cloud.ibm.com/v2/projects/13468527-84sa-619g-gr99-168fe364w98f"
         "id" = "13468527-84sa-619g-gr99-168fe364w98f"
         "name" = "my-project"
         "project_id" = "<your_project_id>"
         "region" = "eu-de"
         "resource_group_id" = "<your_resource_group_id>"
         "resource_type" = "project_v2"
         "status" = "active"
         "timeouts" = null /* object */
      }
      
  4. プロジェクトは、 Code Engine コンソールのプロジェクトのリストで確認できます。 あるいは、カテゴリー Containers を展開してプロジェクト名を探すことにより、 IBM Cloud リソース・リストでプロジェクトを確認することもできます。

次のステップ

IBM Cloudで Terraform を使用して最初の Code Engine プロジェクトを正常に作成したので、追加リソースを作成できます。

リソース

データ・ソース

IBM Cloud と Terraform について詳しくは、 IBM Cloud Providerを参照してください。