Warning
This content has been generated by machine translation. The translations are automated and have not undergone human review or validation.
14.2.5 Using workspaces for multiple environments
OCI provides several Regions. Depending on your needs, you may need to create the same resource in multiple Regions at once. However, as we saw briefly in the previous practice, when the terraform setting is executed, the state is managed as terraform.tfstate in the corresponding folder by default. You can copy the entire Terraform setting folder itself and use it for each target region, but management becomes cumbersome. So, in Terraform, only the workspace concept is provided, and the default workspace is used by default.
Terraform Configuration File
We will use the vcn creation settings we used in the previous lab.
Create a Workspace
-
terraform workspace syntax
# Retrieve workspace terraform workspace list # Create a new workspace terraform workspace new {name} # delete the workspace terraform workspace delete {name} # switch current workspace terraform workspace select {name}
-
Create a new workspace.
terraform workspace new ap-seoul-1 terraform workspace new ap-chuncheon-1
-
example
[opc@bastion-host example_vcn]$ terraform workspace list * default [opc@bastion-host example_vcn]$ terraform workspace new ap-seoul-1 Created and switched to workspace "ap-seoul-1"! You're now on a new, empty workspace. Workspaces isolate their state, so if you run "terraform plan" Terraform will not see any existing state for this configuration. [opc@bastion-host example_vcn]$ terraform workspace new ap-chuncheon-1 Created and switched to workspace "ap-chuncheon-1"! You're now on a new, empty workspace. Workspaces isolate their state, so if you run "terraform plan" Terraform will not see any existing state for this configuration. [opc@bastion-host example_vcn]$ terraform workspace list default * ap-chuncheon-1 ap-seoul-1 [opc@bastion-host example_vcn]$
-
Run terraform in the 1st workspace: ap-seoul-1
-
Switch workspace first and run terraform to create vcn.
[opc@bastion-host example_vcn]$ terraform workspace select ap-seoul-1 Switched to workspace "ap-seoul-1". [opc@bastion-host example_vcn]$ terraform apply -var="region=ap-seoul-1" 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: # oci_core_virtual_network.vcn1 will be created + resource "oci_core_virtual_network" "vcn1" { + cidr_block = "10.0.0.0/16" + cidr_blocks = (known after apply) + compartment_id = "ocid1.compartment.oc1..aaaaaaaa54ryitndueosfezrxvxvcuosutofi2d6f53rbgwz2dpqrgeci7lq" + default_dhcp_options_id = (known after apply) + default_route_table_id = (known after apply) + default_security_list_id = (known after apply) + defined_tags = (known after apply) + display_name = "vcn1" + dns_label = "vcn1" + freeform_tags = (known after apply) + id = (known after apply) + ipv6cidr_blocks = (known after apply) + is_ipv6enabled = (known after apply) + state = (known after apply) + time_created = (known after apply) + vcn_domain_name = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + vcn1_ocid = [ + (known after apply), ] Do you want to perform these actions in workspace "ap-seoul-1"? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes oci_core_virtual_network.vcn1: Creating... oci_core_virtual_network.vcn1: Creation complete after 1s [id=ocid1.vcn.oc1.ap-seoul-1.amaaaaaavsea7yiamf4ktoajkby4sh45zx4e52cwscm6xljilibl5o3prpkq] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: vcn1_ocid = [ "ocid1.vcn.oc1.ap-seoul-1.amaaaaaavsea7yiamf4ktoajkby4sh45zx4e52cwscm6xljilibl5o3prpkq", ] [opc@bastion-host example_vcn]$
-
Check the results
If you change to Seoul Region in the OCI console, you can see that the VCN has been created.
Run terraform in the second workspace: ap-chuncheon-1
-
In the same location, switch workspace first and run terraform to create vcn in the same way.
[opc@bastion-host example_vcn]$ terraform workspace select ap-chuncheon-1 Switched to workspace "ap-chuncheon-1". [opc@bastion-host example_vcn]$ terraform apply -var="region=ap-chuncheon-1" 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: # oci_core_virtual_network.vcn1 will be created + resource "oci_core_virtual_network" "vcn1" { + cidr_block = "10.0.0.0/16" + cidr_blocks = (known after apply) + compartment_id = "ocid1.compartment.oc1..aaaaaaaa54ryitndueosfezrxvxvcuosutofi2d6f53rbgwz2dpqrgeci7lq" + default_dhcp_options_id = (known after apply) + default_route_table_id = (known after apply) + default_security_list_id = (known after apply) + defined_tags = (known after apply) + display_name = "vcn1" + dns_label = "vcn1" + freeform_tags = (known after apply) + id = (known after apply) + ipv6cidr_blocks = (known after apply) + is_ipv6enabled = (known after apply) + state = (known after apply) + time_created = (known after apply) + vcn_domain_name = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + vcn1_ocid = [ + (known after apply), ] Do you want to perform these actions in workspace "ap-chuncheon-1"? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes oci_core_virtual_network.vcn1: Creating... oci_core_virtual_network.vcn1: Creation complete after 0s [id=ocid1.vcn.oc1.ap-chuncheon-1.amaaaaaavsea7yiamplordafqo25ulky46sjoz74r724wxnd5dyplya3ptoq] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: vcn1_ocid = [ "ocid1.vcn.oc1.ap-chuncheon-1.amaaaaaavsea7yiamplordafqo25ulky46sjoz74r724wxnd5dyplya3ptoq", ] [opc@bastion-host example_vcn]$
-
Check the results
If you change to Chuncheon Region in the OCI console, you can see that the VCN has been created.
-
Check Terraform State
If you use a workspace, you can see that the terrafrom.tfstate file is created under the terraform.tfstate.d folder under the folder for each workspace as shown below, and the state is managed for each workspace.
[opc@bastion-host example_vcn]$ ls terraform.tfstate.d/ -la total 4 drwxr-xr-x. 4 opc opc 46 Jan 18 12:39 . drwxrwxr-x. 4 opc opc 4096 Jan 18 12:47 .. drwxr-xr-x. 2 opc opc 31 Jan 18 12:51 ap-chuncheon-1 drwxr-xr-x. 2 opc opc 31 Jan 18 12:47 ap-seoul-1 [opc@bastion-host example_vcn]$ ls terraform.tfstate.d/ap-seoul-1/ -la total 4 drwxr-xr-x. 2 opc opc 31 Jan 18 12:47 . drwxr-xr-x. 4 opc opc 46 Jan 18 12:39 .. -rw-rw-r--. 1 opc opc 2147 Jan 18 12:47 terraform.tfstate [opc@bastion-host example_vcn]$ ls terraform.tfstate.d/ap-chuncheon-1/ -la total 4 drwxr-xr-x. 2 opc opc 31 Jan 18 12:51 . drwxr-xr-x. 4 opc opc 46 Jan 18 12:39 .. -rw-rw-r--. 1 opc opc 2168 Jan 18 12:51 terraform.tfstate [opc@bastion-host example_vcn]$
As an individual, this article was written with my personal time. There may be errors in the content of the article, and the opinions in the article are personal opinions.