網頁

2021/10/30

Terraform local_file hello world

使用Terrform local provider的local_file resource在檔案系統建立一個文字檔。


範例環境



建立一個目錄terraform-demo並在目錄中新增一個terraform配置檔hello.tf內容如下。

hello.tf

resource "local_file" "hello" {
    filename = "${path.module}/hello.txt"
    content  = "Hello world!"
}

${path.module}為terraform module,即.tf所在的檔案系統路徑,也就是目前的terraform-demo

terraform-demo目錄以命令列輸入terraform init將此目錄初始為terraform工作資料夾。

~/.../terraform-demo$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/local...
- Installing hashicorp/local v2.1.0...
- Installed hashicorp/local v2.1.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

初始化後輸入terraform validate驗證配置檔語法是否正確。

~/.../terraform-demo$ terraform validate
Success! The configuration is valid.

驗證成功後輸入terraform plan根據hello.tf產生執行計畫,在此可檢視terraform計畫執行的動作。

~/.../terraform-demo$ terraform plan

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:

  # local_file.hello will be created
  + resource "local_file" "hello" {
      + content              = "Hello world!"
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "./hello.txt"
      + id                   = (known after apply)
    }

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

───────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.

輸入terraform apply則會產生執行計畫並詢問是否執行,輸入yes確定執行。

~/.../terraform-demo$ terraform apply

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:

  # local_file.hello will be created
  + resource "local_file" "hello" {
      + content              = "Hello world!"
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "./hello.txt"
      + id                   = (known after apply)
    }

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

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

local_file.hello: Creating...
local_file.hello: Creation complete after 0s [id=d3486ae9136e7856bc42212385ea797094475802]

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

執行完後可在terraform-demo目錄看到新增的hello.txt內容如下。

Hello world!

參考github


輸入terraform destroy則是摧毀配置檔管理的物件,執行時會詢問是否執行,輸入yes確定執行。所以這邊執行後會刪除hello.txt

~/.../terraform-demo$ terraform destroy
local_file.hello: Refreshing state... [id=d3486ae9136e7856bc42212385ea797094475802]

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:

  # local_file.hello will be destroyed
  - resource "local_file" "hello" {
      - content              = "Hello world!" -> null
      - directory_permission = "0777" -> null
      - file_permission      = "0777" -> null
      - filename             = "./hello.txt" -> null
      - id                   = "d3486ae9136e7856bc42212385ea797094475802" -> null
    }

Plan: 0 to add, 0 to change, 1 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

local_file.hello: Destroying... [id=d3486ae9136e7856bc42212385ea797094475802]
local_file.hello: Destruction complete after 0s

Destroy complete! Resources: 1 destroyed.

執行後可看到hello.txt已不存在於terraform-demo目錄中了。


沒有留言:

張貼留言