Nodes are created across the given subnets.

new-vpc
Curtis Wilson 4 years ago
parent ff956b51d2
commit 18d4c0c4e8

@ -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

@ -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
}

@ -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

@ -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)
}

@ -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

@ -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" {

@ -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."

Loading…
Cancel
Save