简体   繁体   English

Terraform 命令未使用随 --var-file 提供的 tfvars 文件中的值

[英]Terraform command is not using the values from the tfvars file provide with --var-file

I am pretty new in Terraform, trying to create separate backend for each AWS Region under each environment (dev, stg, prod) of my application.我在 Terraform 中很新,试图在我的应用程序的每个环境(dev、stg、prod)下为每个 AWS 区域创建单独的后端。 So, I am using separate .config files to configure separate backend, and separate .tfvars files to create resources on relevant environmet/regions.因此,我使用单独的.config文件来配置单独的后端,并使用单独.tfvars文件在相关环境/区域上创建资源。

I am detailing everything below:我正在详细说明以下所有内容:

Folder structure:文件夹结构:

.
├── config
│   ├── stg-useast1.config
│   ├── stg-uswest2.config
│   ├── prod-useast1.config
│   └── prod-uswest2.config
├── vars
│   ├── stg-useast1.tfvars
│   ├── stg-uswest2.tfvars
│   ├── prod-useast1.tfvars
│   └── prod-useast1.tfvars
└── modules
    ├── backend.tf
    ├── main.tf
    ├── variables.tf
    └── module-ecs
        ├── main.tf
        └── variables.tf

File contents necessary for this question are showing below (just one region):此问题所需的文件内容如下所示(仅一个区域):

./config/stg-useast1.config ./config/stg-useast1.config

profile        = "myapp-stg"
region         = "us-east-1"
bucket         = "myapp-tfstate-stg-useast1"
key            = "myapp-stg-useast1.tfstate"
dynamodb_table = "myapp-tfstate-stg-useast1-lock"

./vars/stg-useast1.tfvars ./vars/stg-useast1.tfvars

environment = "stg"
app_name    = "myapp-ecs"
aws_region  = "us-east-1"
aws_profile = "myapp-stg"

./modules/backend.tf ./modules/backend.tf

terraform {
  backend "s3" {
  }
}

./modules/main.tf ./modules/main.tf

provider "aws" {
  region                   = var.aws_region
  shared_credentials_files = ["~/.aws/credentials"]
  profile                  = var.aws_profile
}

module "aws-ecr" {
  source = "./ecs"
}

./modules/variables.tf ./modules/variables.tf

variable "app_name" {}
variable "environment" {}
variable "aws_region" {}
variable "aws_profile" {}

./modules/module-ecs/main.tf ./modules/module-ecs/main.tf

resource "aws_ecr_repository" "aws-ecr" {
  name = "${var.app_name}-${var.environment}-ecr"
  tags = {
    Name        = "${var.app_name}-ecr"
    Environment = var.environment
  }
}

./modules/module-ecs/variables.tf ./modules/module-ecs/variables.tf

variable "app_name" {
    default = "myapp-ecs"
}
variable "environment" {
    default = "stg"
}
variable "aws_region" {
    default = "us-east-1"
}
variable "aws_profile" {
    default = "myapp-stg"
}

The terraform init went well. terraform init进展顺利。

$ terraform init --backend-config=../config/stg-useast1.config
Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v4.31.0

Terraform has been successfully initialized!

I ran terraform plan as following:我运行terraform plan如下:

terraform plan --var-file=../vars/stg-useast1.tfvars

But it did not use the values from this .tfvars file.但它没有使用此.tfvars文件中的值。 I had to supply them in ./modules/module-ecs/variables.tf as default = <value> for each variables.我必须在./modules/module-ecs/variables.tf中为每个变量提供它们作为default = <value>

How can I make use of the .tfvars file with terraform plan command?如何使用带有terraform plan命令的.tfvars文件?

Any restructuring suggestion is welcomed.欢迎任何重组建议。

The local module you've created doesn't inherit variables.您创建的本地模块不继承变量。 You will need to pass them in. For example:您需要将它们传递进来。例如:

module "aws-ecr" {
  source = "./ecs" # in your example it looks like the folder is ecs-module?
  app_name = var.app_name
  environment = var.environment
  aws_region = var.region
  aws_profile = var.profile
}

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

相关问题 *.tfvars 文件与 terraform 中 main.tf 中的传递值之间的区别 - Difference between *.tfvars file and passing values in main.tf in terraform 无法使用模块 python_terraform 通过 python 包装器将 variables.tfvars 文件中的值替换为输入变量 - Cannot replace values in variables.tfvars file with input variables through a python wrapper using module python_terraform 将 yaml 用于 terraform tfvars - using yaml for terraform tfvars 在 Terraform 中调用本地模块时不考虑 Terraform.tfvars 文件 - Terraform.tfvars file not considered when calling local module in Terraform Terraform.tfvars 变量.tf 文件的解析列表 - Terraform.tfvars parsing lists for variables.tf file Terraform不接受来自vars / tfvars文件的AWS凭证 - Terraform not accepting AWS credentials from vars/tfvars files 合并 2 个以上的 tfvars 文件内容 - Merge more than 2 tfvars file contents 将 AWS SSM 参数存储中的加密数据提取到 terraform var 文件中并将其加密传递,直到在 terraform 代码中调用它 - Pull encrypted data from AWS SSM Parameter store into terraform var file and pass it encrypted till it is called inside the terraform code 从文件中读取 terraform 变量 - Reading terraform variable from file 有没有办法将 Monitoring_role_arn 从 RDS 读入 terraform.tfvars? - Is there a way to read the monitoring_role_arn from RDS into terraform.tfvars?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM