Home > Net >  Terraform "vpc" module "private_subnets" value after "apply"
Terraform "vpc" module "private_subnets" value after "apply"

Time:07-15

I have created a vpc using module "vpc" , Please clarify how the variable private_subnets or public_subnets be assigned with Subnet ID after "apply" BUT my question is, in "resource" block these variables are assigned CIDR blocks.

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

  name = "my-vpc"
  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"]

  enable_nat_gateway = true
  enable_vpn_gateway = true

  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}

CodePudding user response:

A module contains multiple resources and when you take a look at the code of the terraform-aws-modules/vpc/aws module you will see that the private_subnets and public_subnets are used to create aws_subnet resources:

resource "aws_subnet" "private" {
  count = local.create_vpc && length(var.private_subnets) > 0 ? length(var.private_subnets) : 0

  vpc_id                          = local.vpc_id
  cidr_block                      = var.private_subnets[count.index]
}

(main.tf L. 384)

  • Related