# Terraform tf file快速建置雲端服務 > NYUST GDSC Leads FKT --- ## 今天主題Terraform ![](https://i.imgur.com/EW1tWwp.png) 開發公司: HashiCorp 主要功能: 透過文件快速根據需求建置雲端服務 --- ## 基本介紹 Terraform支援的平台 1. Google Cloud Platform 2. Amazon Web Services 3. Microsoft Azure 4. Docker 5. Oracle 6. Terraform Cloud --- ## 操作方法 ---- ### 1. 安裝Terraform [根據環境去選擇你的安裝方式](https://developer.hashicorp.com/terraform/downloads) ```shell= terraform version#檢查Terraform版本 ``` ---- ### 2. 建置描述所需的雲端資源檔案 main.tf Terrform 的資源描述檔案是使用.tf的HCL檔案 範例main.tf建置VM在GCP上面並且建置好能啟動Flask的環境 ```terrform= # Create a single Compute Engine instance resource "google_compute_instance" "default" { name = "flask-vm" machine_type = "f1-micro" zone = "us-west1-a" tags = ["ssh"] boot_disk { initialize_params { image = "debian-cloud/debian-11" } } # Install Flask metadata_startup_script = "sudo apt-get update; sudo apt-get install -yq build-essential python3-pip rsync; pip install flask" network_interface { subnetwork = google_compute_subnetwork.default.id access_config { # Include this section to give the VM an external IP address } } } # 此為GCP官方網站的Example ``` ---- ### 3. 初始化Terraform 在你的終端機輸入 ```bash= terrform init ``` ---- ### 4. 檢驗Terraform的配置是否有錯誤 在你的終端機輸入 ```shell= terraform plan ``` 輸出畫面會告訴你新增/修改/刪除了哪些透過Terraform所建置的資源 ---- ### 5. 確認建置 在你的終端機輸入 ```shell= terrform apply ``` ---- ### 完成了 你的雲端資源就架設完畢!! > 真的蠻簡單蠻方便的 > 當然Terraform還有很多其他用法比如設定全域變數使用variablle.tf等等 --- ## GitLab 範例在GCP建置VM透過Terraform [Repository Link](https://gitlab.com/fan9704/fkt-terraform-tutorial) --- ## Terraform挑戰與體驗 > Qwiklabs 挑戰題目 都是非常簡單的題目 [HTTPS Content-Based Load Balancer with Terraform](https://www.cloudskillsboost.google/games/3710/labs/23537) [Modular Load Balancing with Terraform - Regional Load Balancer](https://www.cloudskillsboost.google/games/3710/labs/23538)
{"metaMigratedAt":"2023-06-17T18:38:08.816Z","metaMigratedFrom":"YAML","breaks":true,"title":"Terraform tf file快速建置雲端服務","contributors":"[{\"id\":\"4c8f8799-9dcd-430b-b7bc-8a5156d39d0b\",\"add\":1939,\"del\":1}]"}
    228 views