Search code examples
terraformterraform-provider-awsamazon-eks

How to pass values from the Terraform output without putting it in the tfvars file


everything good?

I am studying how to make an eks deployment using terraform , as a good practice I separated it into modules to make it reusable , but when I run the plan in testcluster-vpc , the generated outputs are the public and private subnets and vpc_id , how do I use these parameters without needing to put these values in terraform.tfvars inside the testcluster folder ?

I thought about using Data Sources , but it didn't work it still asks to pass the values during the plan

I put the structure to get a sense

├── testclusters
│   ├── config.tf
│   ├── main.tf
│   ├── output.tf
│   ├── terraform.tfvars
│   └── variables.tf
├── testclusters-vpc
│   ├── config.tf
│   ├── main.tf
│   ├── outputs.tf
│   ├── terraform.tfvars
│   └── variables.tf
├── modules
│   ├── cluster
│   │   ├── eks_control_plane.tf
│   │   ├── eks_workers.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   ├── eks-control-plane
│   │   ├── iam.tf
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   ├── security-groups.tf
│   │   └── variables.tf
│   ├── eks-vpc
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   └── eks-workers
│       ├── authconfig.tf
│       ├── iam.tf
│       ├── main.tf
│       ├── outputs.tf
│       ├── security-groups.tf
│       ├── user-data.tf
│       └── variables.tf
└── terraform-state
    ├── config.tf
    ├── terraform-state-dynamodb.tf
    ├── terraform-state-s3.tf
    ├── terraform.tfstate
    ├── terraform.tfstate.backup
    ├── terraform.tfvars
    └── variables.tf
module "testcluster" {
  source                = "../modules/cluster"
  vpc_id                = data.aws_vpc.vpc.id # var.vpc_id
  public_subnets        = data.aws_subnet_ids.public.ids # var.public_subnet_ids
  private_subnets       = data.aws_subnet_ids.private.ids # var.private_subnet_ids
  cluster_full_name     = "${var.clusters_name_prefix}-${terraform.workspace}"
  cluster_version       = var.cluster_version
  workers_instance_type = var.workers_instance_type
  workers_ami_id        = data.aws_ssm_parameter.workers_ami_id.value
  workers_number_min    = var.workers_number_min
  workers_number_max    = var.workers_number_max
  workers_storage_size  = var.workers_storage_size
  commom_tags           = local.commom_tags
  aws_region            = var.aws_region
}

locals {
  commom_tags = {
    ManagedBy   = "terraform"
    ClusterName = "${var.clusters_name_prefix}-${terraform.workspace}"
  }
}

this is the output file that generates the VPC parameters to create the cluster

output "vpc_id" {
  value = module.vpc.eks_cluster_vpc_id
}

output "private_subnet_ids" {
  value = module.vpc.eks_private_subnet_ids
}

output "public_subnets_ids" {
  value = module.vpc.eks_public_subnet_ids
}


Solution

  • As i undesrtand , you want to use output variables from one terraform folder to another . One way to achieve this using terraform_remote_state

    EX : In testcluster-vpc folder, expose outputs in map

    output "testclusters_vpc_net" {
      value = {
        "vpc_id"             = module.vpc.eks_cluster_vpc_id
        "private_subnet_ids" = module.vpc.eks_private_subnet_ids
        "public_subnets_ids" = module.vpc.eks_public_subnet_ids
        
      }
    }
    

    In testcluster folder refer to your terraform state file of testcluster-vpc folder like

    data "terraform_remote_state" "testcluster-vpc" {
      backend = "s3"
    
      config = {
        bucket = "<bucket-name>"
        key    = "<path-to-tfstate-file>"
        region = "<region>"
      }
    }
    

    Now you can access the values as

    vpc_id = data.terraform_remote_state.testcluster-vpc.outputs.testclusters_vpc_net.vpc_id
    

    Note: to achieve this testclusters_vpc_net should always run before testcluster folder to access the updated remote state and its outputs