From 97f358332c728cb6c71b7ee38a2db1154770b9f4 Mon Sep 17 00:00:00 2001 From: shnee Date: Thu, 23 Dec 2021 13:06:45 -0500 Subject: [PATCH] Nodes are created across the given subnets. --- main.tf | 22 +++++++++++++++++----- modules/aws-network-existing/main.tf | 8 ++++++++ modules/aws-network-existing/outputs.tf | 9 +++++++-- modules/aws-network-existing/variables.tf | 6 ++++++ modules/aws-nodes/main.tf | 2 +- modules/aws-nodes/variables.tf | 6 +++--- variables.tf | 6 ++++++ 7 files changed, 48 insertions(+), 11 deletions(-) diff --git a/main.tf b/main.tf index d6f4495..d2127ae 100644 --- a/main.tf +++ b/main.tf @@ -1,24 +1,32 @@ locals { + k8s-subnets-ids = [ + module.aws-network-existing.subnet-by-name["subnet_1"].id, + module.aws-network-existing.subnet-by-name["subnet_3"].id, + ] nodes-config = { "k8s-master" = { base-image = var.ubuntu-ami aws-ec2-type = var.t2-medium-4gib-2vcpu - num = 0 + subnet-ids = local.k8s-subnets-ids + num = 1 }, "k8s-worker" = { base-image = var.ubuntu-ami aws-ec2-type = var.t2-medium-4gib-2vcpu - num = 0 + subnet-ids = local.k8s-subnets-ids + num = 2 }, "ansible-test" = { base-image = var.ubuntu-ami aws-ec2-type = var.t2-micro-1gib-1vcpu + subnet-ids = [module.aws-network-existing.subnet-by-name["subnet_2"].id] num = 0 }, "nfs" = { base-image = var.ubuntu-ami aws-ec2-type = var.t2-micro-1gib-1vcpu + subnet-ids = [module.aws-network-existing.subnet-by-name["subnet_4"].id] num = 1 }, } @@ -74,6 +82,7 @@ module "aws-network-existing" { source = "./modules/aws-network-existing" default-vpc-name = var.aws-existing-vpc-name default-security-group-name = var.aws-existing-sg-name + existing-subnet-names = var.aws-existing-subnet-names } ################################################################################ @@ -88,11 +97,14 @@ resource "aws_key_pair" "key" { } } +# resource "aws_ebs_volume" "zfs" { +# availability_zone = + module "nodes" { for_each = local.nodes-config source = "./modules/aws-nodes" ami = each.value.base-image - subnet-id = module.aws-network-existing.k8s-subnets-ids[0] + subnet-ids = each.value.subnet-ids security-group-ids = [module.aws-network-existing.default-sg.id] user-datas = lookup(module.cloud-init-config, each.key, null).user-datas num-nodes = each.value.num @@ -114,7 +126,7 @@ module "nodes" { # provider "libvirt" { # uri = var.libvirt-connection-url # } -# +# # module "nodes" { # for_each = local.nodes-config # source = "./modules/libvirt-nodes" @@ -131,7 +143,7 @@ module "nodes" { # libvirt-connection-url = var.libvirt-connection-url # user-datas = lookup(module.cloud-init-config, each.key, null).user-datas # } -# +# # resource "libvirt_pool" "images" { # name = var.disk-image-pool-name # type = "dir" diff --git a/modules/aws-network-existing/main.tf b/modules/aws-network-existing/main.tf index 19fff56..398afbc 100644 --- a/modules/aws-network-existing/main.tf +++ b/modules/aws-network-existing/main.tf @@ -22,6 +22,14 @@ data "aws_subnet" "subnets" { id = each.key } +data "aws_subnet" "subnet-by-name" { + for_each = toset(var.existing-subnet-names) + filter { + name = "tag:Name" + values = [each.key] + } +} + data "aws_security_group" "default" { name = var.default-security-group-name } diff --git a/modules/aws-network-existing/outputs.tf b/modules/aws-network-existing/outputs.tf index affe10d..80cc4fd 100644 --- a/modules/aws-network-existing/outputs.tf +++ b/modules/aws-network-existing/outputs.tf @@ -11,11 +11,16 @@ output "subnets" { value = data.aws_subnet.subnets } -output "k8s-subnets-ids" { - description = "An array of subnets to be used for k8s VMs. These subnets were chosen by selecting a single subnet from each availability_zone." +output "one-subnet-per-az" { + description = "An array of subnets that selects 1 subnet per az." value = [for k,v in local.az-to-subnets : v[0]] } +output "subnet-by-name" { + description = "A map of subnet name to subnet resource." + value = data.aws_subnet.subnet-by-name +} + output "az-to-subnets" { description = "A map of availability zone to array of subnets that are in thet availability zone." value = local.az-to-subnets diff --git a/modules/aws-network-existing/variables.tf b/modules/aws-network-existing/variables.tf index 9add409..eac4756 100644 --- a/modules/aws-network-existing/variables.tf +++ b/modules/aws-network-existing/variables.tf @@ -5,3 +5,9 @@ variable "default-security-group-name" { variable "default-vpc-name" { description = "The name of the existing default VPC. This module will query AWS for a VPC with this name," } + +variable "existing-subnet-names" { + description = "A list of subnet names that already exist in default-vpc-name" + default = [] + type = list(string) +} diff --git a/modules/aws-nodes/main.tf b/modules/aws-nodes/main.tf index a9f0c1e..15cf0fd 100644 --- a/modules/aws-nodes/main.tf +++ b/modules/aws-nodes/main.tf @@ -3,7 +3,7 @@ resource "aws_instance" "nodes" { instance_type = var.ec2-instance-type # TODO Make this a variable. associate_public_ip_address = true - subnet_id = var.subnet-id + subnet_id = element(var.subnet-ids, count.index % length(var.subnet-ids)) vpc_security_group_ids = var.security-group-ids user_data = element(var.user-datas.*.rendered, count.index) count = var.num-nodes diff --git a/modules/aws-nodes/variables.tf b/modules/aws-nodes/variables.tf index 4968522..5e526b8 100644 --- a/modules/aws-nodes/variables.tf +++ b/modules/aws-nodes/variables.tf @@ -25,9 +25,9 @@ variable "user-datas" { description = "A list of cloud-init configs that get applied to their corresponding node." } -variable "subnet-id" { - description = "The ID of the subnet that all the nodes will be added to." - type = string +variable "subnet-ids" { + description = "An array of subnet ids. These subnets will be round robined as the subnet to use for each node." + type = list(string) } variable "security-group-ids" { diff --git a/variables.tf b/variables.tf index a9bb332..7107858 100644 --- a/variables.tf +++ b/variables.tf @@ -14,6 +14,12 @@ variable "aws-existing-vpc-name" { description = "The name of the existing VPC when using aws-network-existing." } +variable "aws-existing-subnet-names" { + description = "A list of subnet names that already exist in aws-existing-vpc-name" + default = [] + type = list(string) +} + variable "aws-region" { default = "us-east-1" description = "The AWS region to use."