系列文章
?? Terraform 系列文章
典型檔案布局
- modules/
- services/
- webserver-cluster/
- examples/
- main.tf
- outputs.tf
- vars.tf
- user-data.sh
- README.md
- [ ] versions.tf
- stage/
- vpc/
- services/
- frontend-app/
- backend-app/
- main.tf
- outputs.tf
- vars.tf
- user-data.sh
- README.md
- [ ] provider.tf
- [ ] versions.tf
- [ ] terraform.tfvars(or `*.auto.tfvars`)
- [ ] main.tfvars
- data-storage/
- mysql/
- redis/
- prod/
- vpc/
- services/
- frontend-app/
- backend-app/
- data-storage/
- mysql/
- redis/
- mgmt/
- vpc/
- services/
- bastion-host/
- jenkins/
- global/
- iam/
- s3/
- main.tf
- outputs.tf
- vars.tf
- user-data.sh
- README.md
- [ ] provider.tf
- [ ] versions.tf
?? 提示:
- [ ]表示該項為可選內容- 示例如
examples/表示該項為檔案夾
詳細說明
頂層檔案夾
用于隔離環境
modules:Terraform (可復用)模塊檔案夾stage:預發布 Envprod:生產 envmgmt:管理/DevOps 環境(如:堡壘機、Jenkins 等)global:用于運行各種環境下都要共享的資源(如:Terraform backend - S3、IAM)
二級檔案夾
用于環境中的組件
vpc:網路拓撲services:此環境中運行的應用環境或微服務,例如 NGINX 前端或 Java 后端,每個應用程式甚至都應該駐留在單獨的檔案夾中,與其他應用程式隔離data-storage:在此環境中運行的資料存盤,例如 MySQL 或 Redis,每個資料存盤應該駐留在它自己的檔案夾中,與其他資料存盤隔離,
檔案
每一個組件中,都會有相應的 Terraform 的組態檔,其命名規則如下:
vars.tf: 輸入變數outputs.tf: 輸出變數main.tf: 資源定義user-data.sh:(可選),用戶自定義腳本README.md:說明檔案provider.tf:(可選),provider 資訊,典型如:provider、region,不同環境,甚至同一環境的不同組件的 provider 可能不同,versions.tf:(可選),Terraform version、provider version、Terraform backend 資訊,terraform.tfvars(or*.auto.tfvars): (可選),terraform plan 及 apply默認會傳入該檔案中的變數值main.tfvars:(可選),terraform plan 及 apply可以通過-var-file=filename來手動指定,
變數賦值
可以通過如下 3 種方式指定變數:
-var 'foo=bar' Set a value for one of the input variables in the root
module of the configuration. Use this option more than
once to set more than one variable.
-var-file=filename Load variable values from the given file, in addition
to the default files terraform.tfvars and *.auto.tfvars.
Use this option more than once to include more than one
variables file.
-
命令列引數:
-var 'foo=bar' -
引數檔案:默認讀取 terraform.tfvars(or
*.auto.tfvars) 或通過命令列-var-file=filename指定 -
環境變數:可以通過設定名為
TF_VAR_<NAME>的環境變數為輸入變數賦值,例如:$ export TF_VAR_image_id=ami-abc123 $ terraform plan ...環境變數傳值非常適合在自動化流水線中使用,尤其適合用來傳遞敏感資料,類似密碼、訪問密鑰等
三人行, 必有我師; 知識共享, 天下為公. 本文由東風微鳴技術博客 EWhisper.cn 撰寫.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/548918.html
標籤:其他
