構成の適用

Oracle Cloud Infrastructure (OCI) Terraformプロバイダをインストールおよび構成し、プロバイダとリソースまたはデータ・ソースを含むTerraform構成を作成したら、OCIインフラストラクチャに対してTerraformを実行できます。

ヒント

作業手順および例は、チュートリアルを参照してください。

Terraformの初期化

Terraform構成ファイルを含むディレクトリから次のコマンドを実行して、Terraformを初期化します:

terraform init

出力例:

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/oci...
- Installing hashicorp/oci v3.96.0...
- Installed hashicorp/oci v3.96.0 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,
so the latest version was installed.
...
* hashicorp/oci: version = "~> 3.96.0"

Terraform has been successfully initialized!

コマンドでエラーが発生した場合は、OCI Terraformプロバイダが正しく構成されていることを確認してください。

変更の確認

Terraformでは、既存のOCIリソースを変更せずに、変更を適用するように選択した場合に構成に基づいて行われる変更のリストを返すことができます。このリストは実行計画と呼ばれます。実行計画を確認するには、次のコマンドを実行します:

terraform plan

出力例:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # oci_identity_compartment.tf-compartment will be created
  + resource "oci_identity_compartment" "tf-compartment" {
      + compartment_id = "exampleuniqueID"
      + defined_tags   = (known after apply)
      + description    = "Compartment for Terraform resources."
      + freeform_tags  = (known after apply)
      + id             = (known after apply)
      + inactive_state = (known after apply)
      + is_accessible  = (known after apply)
      + name           = <your-compartment-name>
      + state          = (known after apply)
      + time_created   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

計画を確認し、計画に表示された変更が期待どおりかどうかをチェックします。

破壊的な変更

一部のOCIリソースには、既存のリソースを破棄して新しいプロパティ値で再作成しないとTerraformで更新できないプロパティがあります。terraform planを実行して更新不可能なプロパティを変更しようとすると、Terraformによって、置換されるリソースと更新できないプロパティが示されます。

たとえば、次の実行計画では、削除および再作成されるリソースの先頭に-/+が追加され、更新できないプロパティに# forces replacementというコメントが追加されています:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
 
Terraform will perform the following actions:
 
  # oci_core_vcn.vcn must be replaced
-/+ resource "oci_core_vcn" "vcn" {
      ~ cidr_block               = "10.0.0.0/16" -> "11.0.0.0/16" # forces replacement
      ~ cidr_blocks              = [
          - "10.0.0.0/16",
        ] -> (known after apply)
        compartment_id           = "ocid1.compartment.oc1..exampleuniqueID"
      ~ default_dhcp_options_id  = "ocid1.dhcpoptions.oc1.phx.exampleuniqueID" -> (known after apply)
      ~ default_route_table_id   = "ocid1.routetable.oc1.phx.exampleuniqueID" -> (known after apply)
      ~ default_security_list_id = "ocid1.securitylist.oc1.phx.exampleuniqueID" -> (known after apply)
      ~ defined_tags             = {} -> (known after apply)
        display_name             = "vcn"
        dns_label                = "vcn"
      ~ freeform_tags            = {} -> (known after apply)
      ~ id                       = "ocid1.vcn.oc1.phx.exampleuniqueID" -> (known after apply)
      + ipv6cidr_block           = (known after apply)
      + ipv6public_cidr_block    = (known after apply)
      + is_ipv6enabled           = (known after apply)
      ~ state                    = "AVAILABLE" -> (known after apply)
      ~ time_created             = "2021-01-25 20:54:53.255 +0000 UTC" -> (known after apply)
      ~ vcn_domain_name          = "vcn.oraclevcn.com" -> (known after apply)
    }
 
Plan: 1 to add, 0 to change, 1 to destroy.
注意

リソースを強制的に削除して再作成すると、親リソースも置換される可能性があります。terraform applyを実行する前に必ずterraform planを実行し、影響を受けるリソースを確認してください。

OCI Terraformプロバイダのサポートされているリソースおよびデータ・ソースの完全なリファレンスには、使用方法、引数および属性の詳細が含まれます。完全なリファレンスは、docs.oracle.comおよびTerraformレジストリから入手できます。

参照には、更新可能なプロパティが示されています。

変更の適用

構成の結果が期待どおりの変更になることを確認したら、次のコマンドを実行してTerraform構成を適用できます:

terraform apply

確認を求められたら、yesと入力してリソースを作成します。applyコマンドを実行すると、出力がターミナルに表示されます。出力例:

oci_identity_compartment.tf-compartment: Creating...
oci_identity_compartment.tf-compartment: Creation complete after 9s [id=exampleuniqueID]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

compartment-OCID = ocid1.compartment.exampleuniqueID
compartment-name = <your-compartment-name>
注意

Terraformの外部でTerraformを使用して管理しているリソースに変更を加えた場合、その変更は、次回Terraformを使用して構成を適用するときに上書きされます。上書きしない構成ファイルのリソースには、ignore_changesパラメータを追加してください。