Search code examples
amazon-web-servicesterraformdatabricksterraform-provider-awsaws-databricks

DataBricks Sample Terraform Code causes error in AWS VPC module


I'm completely new to DataBricks and trying to deploy an E2 workspace using the sample Terraform code provided by DataBricks. I've just started with the VPC part:

data "aws_availability_zones" "available" {}

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

  name = local.prefix
  cidr = var.cidr_block
  azs  = data.aws_availability_zones.available.names

  enable_dns_hostnames = true
  enable_nat_gateway   = true
  single_nat_gateway   = true
  create_igw           = true

  private_subnets = [cidrsubnet(var.cidr_block, 3, 1),
                     cidrsubnet(var.cidr_block, 3, 2)]

  manage_default_security_group = true
  default_security_group_name = "${local.prefix}-sg"

  default_security_group_egress = [{
    cidr_blocks = "0.0.0.0/0"
  }]

  default_security_group_ingress = [{
    description = "Allow all internal TCP and UDP"
    self        = true
  }]
}

When I run terraform plan I get this error:

│ Error: Error in function call
│
│   on .terraform/modules/vpc/main.tf line 1090, in resource "aws_nat_gateway" "this":
│ 1090:   subnet_id = element(
│ 1091:     aws_subnet.public.*.id,
│ 1092:     var.single_nat_gateway ? 0 : count.index,
│ 1093:   )
│     ├────────────────
│     │ aws_subnet.public is empty tuple
│     │ count.index is 0
│     │ var.single_nat_gateway is true
│
│ Call to function "element" failed: cannot use element function with an empty list.

Would really appreciate any pointers on what is going wrong here.


Solution

  • You set that you want internet gateway create_igw = true, but you haven't specified public_subnets. You must have public_subnets if you have igw.