如何重构创建VPC的Terraform模块?

huangapple go评论56阅读模式
英文:

How can I refactor terraform module that creates vpc?

问题

我在这里找到了一个使用Terraform在AWS上部署ML流的示例:https://github.com/Glovo/terraform-aws-mlflow/blob/master/terratest/examples/main.tf。问题是它声明了一个"vpc"模块,而这似乎在最新版本的Terraform(v 1.4.6)中已经弃用。VPC被声明如下:

    module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.44.0"

  name               = "mlflow-${random_id.id.hex}"
  cidr               = "10.0.0.0/16"
  azs                = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
  private_subnets    = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets     = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
  database_subnets   = ["10.0.201.0/24", "10.0.202.0/24", "10.0.203.0/24"]
  enable_nat_gateway = true

  tags = {
    "built-using" = "terratest"
    "env"         = "test"
  }
}

然后在ML流模块中引用了这个VPC:

module "mlflow" {
  source = "../../"

  unique_name = "mlflow-terratest-${random_id.id.hex}"
  tags = {
    "owner" = "terratest"
  }
  vpc_id                            = module.vpc.vpc_id
  database_subnet_ids               = module.vpc.database_subnets
  service_subnet_ids                = module.vpc.private_subnets
  load_balancer_subnet_ids          = var.is_private ? module.vpc.private_subnets : module.vpc.public_subnets
  load_balancer_ingress_cidr_blocks = var.is_private ? [module.vpc.vpc_cidr_block] : ["0.0.0.0/0"]
  load_balancer_is_internal         = var.is_private
  artifact_bucket_id                = var.artifact_bucket_id
  database_password_secret_arn      = aws_secretsmanager_secret_version.db_password.secret_id
  database_skip_final_snapshot      = true

我猜我需要重构这个,使用VPC的资源声明而不是模块。我不明白的是为什么在VPC模块中将子网声明为字符串列表,然后在ML流模块中将其分配给子网ID变量?我应该如何在最新版本的Terraform中做到这一点?

任何帮助将不胜感激。

英文:

I found an example of an ML flow deployment on aws using terraform here: https://github.com/Glovo/terraform-aws-mlflow/blob/master/terratest/examples/main.tf. The problem is that it declares a "vpc" module, and it seems this is deprecated with the latest version of terraform (v 1.4.6). The vpc is declared like this

    module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.44.0"

  name               = "mlflow-${random_id.id.hex}"
  cidr               = "10.0.0.0/16"
  azs                = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
  private_subnets    = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets     = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
  database_subnets   = ["10.0.201.0/24", "10.0.202.0/24", "10.0.203.0/24"]
  enable_nat_gateway = true

  tags = {
    "built-using" = "terratest"
    "env"         = "test"
  }
}

The vpc is then referenced in the mlf flow module:

module "mlflow" {
  source = "../../"

  unique_name = "mlflow-terratest-${random_id.id.hex}"
  tags = {
    "owner" = "terratest"
  }
  vpc_id                            = module.vpc.vpc_id
  database_subnet_ids               = module.vpc.database_subnets
  service_subnet_ids                = module.vpc.private_subnets
  load_balancer_subnet_ids          = var.is_private ? module.vpc.private_subnets : module.vpc.public_subnets
  load_balancer_ingress_cidr_blocks = var.is_private ? [module.vpc.vpc_cidr_block] : ["0.0.0.0/0"]
  load_balancer_is_internal         = var.is_private
  artifact_bucket_id                = var.artifact_bucket_id
  database_password_secret_arn      = aws_secretsmanager_secret_version.db_password.secret_id
  database_skip_final_snapshot      = true
}

My guess is that I would have to refactor this using resource declarations for the vpc instead of the module. The thing I can't understand is why are the subnets declared as a list of strings in the vpc module, and then assigned to subnet id variables in the ml flow module? And how would I do this in the latest version of terraform?

Any help would be much appreciated.

答案1

得分: 2

你不需要重构那个模块,只需将版本更新到最新版(4.0.2),你正在使用一个非常旧的版本。
建议:<br>每次在Terraform仓库中看到一个模块调用时,请访问Terraform注册表,检查版本、文档和官方示例/仓库。<br>
https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/4.0.2

英文:

You don't need to refactor that module, just update the version to the latest one (4.0.2) you're using a really old one.
Recommendation: <br> Every time that you see a module calling in a terraform repo go to the Terraform registry and check the versions, documentation, and official examples/repos. <br>
https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/4.0.2

huangapple
  • 本文由 发表于 2023年5月14日 02:15:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定