如何使用一个 terraform 脚本和不同的变量值管理多个不同环境的部署 [英] How to manage deployment of several different environments with one terraform scripts and different variables values

查看:38
本文介绍了如何使用一个 terraform 脚本和不同的变量值管理多个不同环境的部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Terraform 在 Azure 上部署 3 层应用程序:前端、后端和数据库.它运作良好.该应用程序是一个产品,我需要为多个客户提供多个实例.

I am using Terraform to deploy an 3-tiers application on Azure: Frontend, Backend, and DB. It works well. The application is a product, and I need several instances for several customers.

我的问题是,我很难理解将多组值 (variables.tf) 应用于同一个脚本,以获得多个环境.

My problem is, I have a hard time understanding applying several sets of values (variables.tf) to the same script, in order to get several environments.

我希望有以下结构:

  • main.tf
  • 客户1
    • 变量.tf
    • 变量.tf

    然后选择我是部署 customer1 还是 customer2.我阅读了有关 terraform 工作空间的信息,并开始为每位客户创建一个工作空间.但我不明白如何根据工作工作区为相同的脚本应用一组不同的值.在网上很难找到一个全面的例子,我想做的应该是 terraform 的面包和黄油.

    And select whether I deploy customer1 or customer2. I read about terraform workspaces, and started creating one workspace per customer. But I don't understand how to apply a different set of values for the same scripts depending on the working workspace. It's difficult to find a comprehensive example online, what I am trying to do should be the bread and butter of terraform.

    谢谢!

    推荐答案

    您和@ydaetskcoR 都知道,工作空间是一种选择.如果它不适合您的业务.您还有另一种方法,我会为您的用例推荐它.

    As both of you and @ydaetskcoR knew, workspace is one choice. If it is not suitable for your business. There is another way for you, and I would recommend it for your use case.

    Terragrunt 是 Terraform 的瘦包装器,它提供了用于处理多个 Terraform 模块的额外工具.

    Terragrunt is a thin wrapper for Terraform that provides extra tools for working with multiple Terraform modules.

    所以在你的情况下,它可以很容易地用于管理不同的客户,如下结构.

    So in your case, it can be easily used to manage different customers as below structure.

    └── customer1
        ├── prod
        │   ├── app
        │   │   └── terraform.tfvars
        │   ├── mysql
        │   │   └── terraform.tfvars
        │   └── vpc
        │       └── terraform.tfvars
        └── stage
            ├── app
            │   └── terraform.tfvars
            ├── mysql
            │   └── terraform.tfvars
            └── vpc
                └── terraform.tfvars
    └── customer2
        ├── prod
        │   ├── app
        │   │   └── terraform.tfvars
        │   ├── mysql
        │   │   └── terraform.tfvars
        │   └── vpc
        │       └── terraform.tfvars
        └── stage
            ├── app
            │   └── terraform.tfvars
            ├── mysql
            │   └── terraform.tfvars
            └── vpc
                └── terraform.tfvars
    

    这篇关于如何使用一个 terraform 脚本和不同的变量值管理多个不同环境的部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆