2021年12月13日において、Oracle Cloud上の特定のコンパートメントのリソースを一括で削除するために、Terraformを使用する

首先

本文是为 Oracle Cloud Infrastructure Advent Calendar 2021 Day 13 编写的文章。
通过使用 Terraform/OCI Resource Manager,在 OCI 专属区内批量删除资源进行了实验。

从现有的容器中创建一个堆栈

image.png
image.png
image.png
image.png
image.png

在资源管理器中删除资源。

image.png
image.png

填充

image.png
image.png

在用户的根目录下创建一个名为”bin”的文件夹。
使用wget <链接>命令,将OCI Provider下载到bin文件夹中,并解压下载的文件。

@cloudshell:~ (ap-tokyo-1)$ mkdir bin
@cloudshell:~ (ap-tokyo-1)$ cd bin
@cloudshell:bin (ap-tokyo-1)$ wget https://releases.hashicorp.com/terraform-provider-oci/4.55.0/terraform-provider-oci_4.55.0_linux_amd64.zip
--2021-12-05 07:47:07--  https://releases.hashicorp.com/terraform-provider-oci/4.55.0/terraform-provider-oci_4.55.0_linux_amd64.zip
Resolving releases.hashicorp.com (releases.hashicorp.com)... 146.75.37.183, 2a04:4e42:79::439
Connecting to releases.hashicorp.com (releases.hashicorp.com)|146.75.37.183|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25541306 (24M) [application/zip]
Saving to: ‘terraform-provider-oci_4.55.0_linux_amd64.zip’

100%[===============================================================================================================================================================================================================================================================================================================================================>] 25,541,306   150MB/s   in 0.2s   

2021-12-05 07:47:07 (150 MB/s) - ‘terraform-provider-oci_4.55.0_linux_amd64.zip’ saved [25541306/25541306]

@cloudshell:bin (ap-tokyo-1)$ ls
terraform-provider-oci_4.55.0_linux_amd64.zip
@cloudshell:bin (ap-tokyo-1)$ unzip terraform-provider-oci_4.55.0_linux_amd64.zip 
Archive:  terraform-provider-oci_4.55.0_linux_amd64.zip
  inflating: terraform-provider-oci_v4.55.0_x4  
@cloudshell:bin (ap-tokyo-1)$ ls
terraform-provider-oci_4.55.0_linux_amd64.zip  terraform-provider-oci_v4.55.0_x4
@cloudshell:bin (ap-tokyo-1)$ cd 

在用户的主目录下创建一个名为cleaning的文件夹,并进入该文件夹
指定要清理的区域的OCID,并执行下面的导出命令(请注意下载的OCI Provider的版本并执行命令)

@cloudshell:~ (ap-tokyo-1)$ mkdir cleaning
@cloudshell:~ (ap-tokyo-1)$ cd cleaning/
@cloudshell:cleaning (ap-tokyo-1)$ ~/bin/terraform-provider-oci_v4.55.0_x4 -command=export -compartment_id=ocid1.compartment.oc1..aaaaaaaan -output_path=. -generate_state
2021/12/05 07:51:08 [INFO] terraform-provider-oci 4.55.0

略

INFO 2021/12/XX 07:51:46.315147 === COMPLETED ===
INFO 2021/12/XX 07:51:46.315152 ========= PERFORMANCE SUMMARY ==========
INFO 2021/12/XX 07:51:46.315157 Total resources: 2
INFO 2021/12/XX 07:51:46.315165 Total time taken for discovering all services: 18.93891921s
INFO 2021/12/XX 07:51:46.315175 Total time taken for generating state of all services: 16.43681961s
INFO 2021/12/XX 07:51:46.315183 Total time taken by entire export: 35.380211205s
@cloudshell:cleaning (ap-tokyo-1)$ 

执行命令后,将检查资源,并创建一个被定义为Terraform Plan文件(.tf)的Terraform资源,然后将其导入到Terraform状态文件中。
查看这个.tf文件,可以找到要删除的云资源的定义。

执行Terraform Destroy命令以执行资源删除。请确认要被销毁的内容,输入”yes”并按回车键确认删除资源。

@cloudshell:cleaning (ap-tokyo-1)$ ls -l
total 44
-rw-r--r--. 1 kenji_wata oci  6112 Dec  X 07:51 database.tf
-rw-r--r--. 1 kenji_wata oci    38 Dec  X 07:51 provider.tf
-rw-r--r--. 1 kenji_wata oci 15641 Dec  X 07:51 terraform.tfstate
-rw-r--r--. 1 kenji_wata oci  8826 Dec  X 07:51 terraform.tfstate.tmp.backup
-rw-r--r--. 1 kenji_wata oci   171 Dec  X 07:51 vars.tf
@cloudshell:cleaning (ap-tokyo-1)$ terraform destroy
oci_database_db_system.export_DBSystem1: Refreshing state... [id=ocid1.dbsystem.oc1.phx.anyhqljrssl]
oci_database_autonomous_database.export_ADB01: Refreshing state... [id=ocid1.autonomousdatabase.oc1.phx.anyhqljrssl]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

略

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

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

请注意:类似于策略标识资源等的一些资源仅存在于租户级别,所以需要单独删除。

最后

OCI Resource Manager的资源发现功能是一种非常强大的机制,可以了解OCI区段内的资源。通过使用所得到的Terraform文件,可以按照适当的顺序考虑所有基础细节和子元素,实现从区段销毁资源的目的,从而节省时间和精力。

OCI Cloud Shell为掌握Terraform提供了便捷的访问机制。

请提供参考信息。

    • Terraform OCI Provider Docs – Resource Discovery

 

    • OCI Docs – Resource Discovery

 

    Cleaning Out a Compartment on Oracle Cloud using Terraform provider
广告
将在 10 秒后关闭
bannerAds