Search code examples
amazon-ec2terraformamazon-vpcmlflow

How can I refactor terraform module that creates vpc?


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.


Solution

  • 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:
    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.
    https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/4.0.2