6

Terraform 系列-Terraform 项目的典型文件布局 - 东风微鸣

 2 years ago
source link: https://www.cnblogs.com/east4ming/p/17278063.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

👉 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:预发布 Env
  • prod:生产 env
  • mgmt:管理/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.
  1. 命令行参数:-var 'foo=bar'

  2. 参数文件:默认读取 terraform.tfvars(or *.auto.tfvars) 或通过命令行 -var-file=filename 指定

  3. 环境变量:可以通过设置名为TF_VAR_<NAME>的环境变量为输入变量赋值,例如:

    $ export TF_VAR_image_id=ami-abc123
    $ terraform plan
    ...
    

    环境变量传值非常适合在自动化流水线中使用,尤其适合用来传递敏感数据,类似密码、访问密钥等

三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK