简体   繁体   English

在Terraform中使用多个环境/变量

[英]Work with multiple environments/variables in Terraform

We're using terraform to spin up our infrastructure within AWS and we have 3 separate environments: Dev, Stage and Prod 我们使用terraform在AWS内部开发我们的基础设施,我们有3个独立的环境:Dev,Stage和Prod

Dev : Requires - public, private1a, privatedb and privatedb2 subnets Stage & Prod : Requires - public, private_1a, private_1b, privatedb and privatedb2 subnets 开发:需要 - public,private1a,privatedb和privatedb2子网Stage&Prod:需要 - public,private_1a,private_1b,privatedb和privatedb2子网

I have main.tf, variables, dev.tfvars, stage.tfvars and prod.tfvars. 我有main.tf,变量,dev.tfvars,stage.tfvars和prod.tfvars。 I'm trying to understand as of how can I use main.tf file that I'm currently using for dev environment and create resources required for stage and prod using .tfvars files. 我试图理解如何使用我目前用于开发环境的main.tf文件,并使用.tfvars文件创建stage和prod所需的资源。

terraform apply -var-file=dev.tfvars terraform apply -var-file = dev.tfvars

terraform apply -var-file=stage.tfvars (this should create subnet private_1b in addition to the other subnets) terraform apply -var-file = stage.tfvars(这应该创建子网private_1b以及其他子网)

terraform apply -var-file=prod.tfvars (this should create subnet private_1b in addition to the other subnets) terraform apply -var-file = prod.tfvars(这应该创建子网private_1b以及其他子网)

Please let me know if you need further clarification. 如果您需要进一步说明,请与我们联系。

Thanks, 谢谢,

What you are trying to do is indeed the correct approach. 你要做的确实是正确的方法。 You will also have to make use of terraform workspaces . 您还必须使用terraform工作区

Terraform starts with a single workspace named "default". Terraform以名为“default”的单个工作区开始。 This workspace is special both because it is the default and also because it cannot ever be deleted. 此工作空间是特殊的,因为它是默认值,也因为它永远不会被删除。 If you've never explicitly used workspaces, then you've only ever worked on the "default" workspace. 如果您从未明确使用过工作区,那么您只能使用“默认”工作区。

Workspaces are managed with the terraform workspace set of commands. 使用terraform工作空间命令集管理工作空间。 To create a new workspace and switch to it, you can use terraform workspace new; 要创建新工作区并切换到它,可以使用terraform workspace new; to switch environments you can use terraform workspace select; 切换环境你可以使用terraform工作区选择; etc. 等等

In essence this means you will have a workspace for each environment you have. 从本质上讲,这意味着您将拥有适用于您拥有的每个环境的工作区。

Lets see with some examples. 让我们看一些例子。

I have the following files: 我有以下文件:

  1. main.tf main.tf
  2. variables.tf variables.tf
  3. dev.tfvars dev.tfvars
  4. production.tfvars production.tfvars

main.tf main.tf

This file contains the VPC module 9Can be any resource ofc). 该文件包含VPC模块9可以是c)的任何资源。 We call the variables via the var. 我们通过var调用变量。 function: 功能:

module "vpc" {
  source          = "modules/vpc"
  cidr_block      = "${var.vpc_cidr_block}"
  subnets_private = "${var.vpc_subnets_private}"
  subnets_public  = "${var.vpc_subnets_public}"
}

variables.tf variables.tf

This file contains all our variables. 该文件包含我们所有的变量。 Please do not that we do not assign default here, this will make sure we are 100% certain that we are using the variables from the .tfvars files. 请注意,我们不在此处指定默认值 ,这将确保我们100%确定我们正在使用.tfvars文件中的变量。

variable "vpc_cidr_block" {}

variable "vpc_subnets_private" {
  type = "list"
}

variable "vpc_subnets_public" {
  type = "list"
}

That's basically it. 基本上就是这样。 Our .tfvars file will look like this: 我们的.tfvars文件如下所示:

dev.tfvars dev.tfvars

vpc_cidr_block = "10.40.0.0/16"
vpc_subnets_private = ["10.40.0.0/19", "10.40.64.0/19", "10.40.128.0/19"]
vpc_subnets_public = ["10.40.32.0/20", "10.40.96.0/20", "10.40.160.0/20"]

production.tfvars production.tfvars

vpc_cidr_block = "10.30.0.0/16"
vpc_subnets_private = ["10.30.0.0/19", "10.30.64.0/19", "10.30.128.0/19"]
vpc_subnets_public = ["10.30.32.0/20", "10.30.96.0/20", "10.30.160.0/20"]

If I would like to run terraform for my dev environment, these are the commands I would use (Assuming the workspaces are already created, see Terraform workspace docs ): 如果我想为我的开发环境运行terraform,这些是我将使用的命令(假设已经创建了工作空间,请参阅Terraform工作空间文档 ):

  1. Select the dev environment: terraform workspace select dev 选择开发环境: terraform workspace select dev
  2. Run a plan to see the changes: terraform plan -var-file=dev.tfvars -out=plan.out 运行计划以查看更改: terraform plan -var-file=dev.tfvars -out=plan.out
  3. Apply the changes: terraform apply plan.out 应用更改: terraform apply plan.out

You can replicate this for as many environments as you like. 您可以根据需要为多个环境复制此项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM