AWS nodes and user-data created from config map.

new-vpc
Curtis Wilson 4 years ago
parent a730a9d940
commit df53ae047d

@ -1,4 +1,4 @@
vm-name-prefix = "docker-ansible-test" vm-name-prefix = "k8s-tf"
# A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0' # A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0'
# equates to any ip address. # equates to any ip address.
@ -13,6 +13,10 @@ worker-nodes = 2
node-memory = 2048 node-memory = 2048
node-vcpus = 2 node-vcpus = 2
################################################################################
# AWS EC2 instance types
################################################################################
# 1 GiB, 1 vcpu, only one that is free. # 1 GiB, 1 vcpu, only one that is free.
# This one won't work with k8s because it requires at least 2 vcpus. # This one won't work with k8s because it requires at least 2 vcpus.
aws-ec2-instance-type = "t2.micro" aws-ec2-instance-type = "t2.micro"
@ -20,6 +24,19 @@ aws-ec2-instance-type = "t2.micro"
# 4 GiB, 2 vcpus # 4 GiB, 2 vcpus
# aws-ec2-instnce-type = "t2.medium" # aws-ec2-instnce-type = "t2.medium"
################################################################################
nodes-config = {
"master" = {
base-image = "ami-0dd0ccab7e2801812"
num = 1
},
"worker" = {
base-image = "ami-0dd0ccab7e2801812"
num = 2
}
}
################################################################################ ################################################################################
# AWS images (AMIs) # AWS images (AMIs)
################################################################################ ################################################################################
@ -53,6 +70,10 @@ base-image = "ami-0dd0ccab7e2801812"
# From https://cloud.centos.org/centos/7/images/ from 2020-11-12 06:52 # From https://cloud.centos.org/centos/7/images/ from 2020-11-12 06:52
# base-image = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2" # base-image = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2"
################################################################################
# Keys/Passwords
################################################################################
# Password hash created with: # Password hash created with:
# python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))' # python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))'
# where "linux" is the password. # where "linux" is the password.

@ -1,8 +1,8 @@
terraform { terraform {
required_version = ">= 0.13" required_version = ">= 1.0.8"
required_providers { required_providers {
libvirt = { libvirt = {
source = "dmacvicar/libvirt" source = "dmacvicar/libvirt"
version = "0.6.11" version = "0.6.11"
} }
} }
@ -12,24 +12,14 @@ terraform {
# cloud-init # cloud-init
################################################################################ ################################################################################
data "template_file" "master-node-user-datas" { module "cloud-init-config" {
template = file("${path.module}/cloud_init.cfg") for_each = var.nodes-config
vars = { source = "./modules/cloud-init-config"
admin-passwd = "${var.root-admin-passwd}" cloud-init-template = "${path.module}/cloud_init.cfg"
admin-pub-key = "${var.root-admin-pub-key}" hostname-prefix = "${var.vm-name-prefix}-${each.key}"
hostname = "${var.vm-name-prefix}-master-${count.index}" num = each.value.num
} root-admin-passwd = var.root-admin-passwd
count = var.master-nodes root-admin-pub-key = var.root-admin-pub-key
}
data "template_file" "worker-node-user-datas" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.vm-name-prefix}-worker-${count.index}"
}
count = var.worker-nodes
} }
################################################################################ ################################################################################
@ -42,16 +32,20 @@ provider "aws" {
region = "us-east-2" region = "us-east-2"
} }
module "aws-amis" { # This module will grab the latest ami for a variety of distros.
source = "./modules/aws-amis" # module "aws-amis" {
} # source = "./modules/aws-amis"
# }
# output "amis" {
# value = module.aws-amis.amis
# }
module "aws-network" { module "aws-network" {
source = "./modules/aws-network" source = "./modules/aws-network"
name-prefix = var.vm-name-prefix name-prefix = var.vm-name-prefix
vpc-cidr-block = var.aws-vpc-cidr-block vpc-cidr-block = var.aws-vpc-cidr-block
subnet-cidr-block = var.aws-subnet-cidr-block subnet-cidr-block = var.aws-subnet-cidr-block
admin-ips = var.admin-ips admin-ips = var.admin-ips
} }
# This key pair is not actually used. Keys are added to the nodes via cloud-init # This key pair is not actually used. Keys are added to the nodes via cloud-init
@ -64,30 +58,16 @@ resource "aws_key_pair" "key" {
} }
} }
module "master-nodes" { module "nodes" {
for_each = var.nodes-config
source = "./modules/aws-nodes" source = "./modules/aws-nodes"
ami = var.base-image ami = each.value.base-image
ec2-instance-type = var.aws-ec2-instance-type ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id] security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.master-node-user-datas user-datas = lookup(module.cloud-init-config, each.key, null).user-datas
num-nodes = var.master-nodes num-nodes = each.value.num
name-prefix = "${var.vm-name-prefix}-master" name-prefix = "${var.vm-name-prefix}-${each.key}"
}
module "worker-nodes" {
source = "./modules/aws-nodes"
ami = var.base-image
ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.worker-node-user-datas
num-nodes = var.worker-nodes
name-prefix = "${var.vm-name-prefix}-worker"
}
output "amis" {
value = module.aws-amis.amis
} }
################################################################################ ################################################################################
@ -142,11 +122,6 @@ output "amis" {
# end libvirt # end libvirt
################################################################################ ################################################################################
# TODO REM move to other file? output "ips" {
output "master-ips" { value = { for type, node in module.nodes : type => node.ips }
value = module.master-nodes.ips
}
output "worker-ips" {
value = module.worker-nodes.ips
} }

@ -0,0 +1,9 @@
data "template_file" "user-datas" {
template = file("${var.cloud-init-template}")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.hostname-prefix}-${count.index}"
}
count = var.num
}

@ -0,0 +1,3 @@
output "user-datas" {
value = data.template_file.user-datas
}

@ -0,0 +1,22 @@
variable "cloud-init-template" {
default = "../../cloud_init.cfg"
description = "The path to the cloud-init config template."
type = string
}
variable "hostname-prefix" {
description = "This prefix wil be applied as a prefix for the hostnames."
}
variable "num" {
description = "The number of user-datas to create with these parameters."
}
variable "root-admin-passwd" {
description = "This value will be substituted for any occurence of 'admin-password' in the cloud-init config template."
}
variable "root-admin-pub-key" {
description = "This value will be substituted for any occurence of 'admin-pub-key' in the cloud-init config template."
}

@ -43,6 +43,11 @@ variable "node-vcpus" {
type = number type = number
} }
variable "nodes-config" {
description = "A config that declares how many nodes of each type you want created."
type = map(object({base-image=string,num=number}))
}
variable "root-admin-passwd" { variable "root-admin-passwd" {
description = "This will be the password for the root and admin user. The format of this can by any format accepted by cloud-init's chpasswd module." description = "This will be the password for the root and admin user. The format of this can by any format accepted by cloud-init's chpasswd module."
} }

Loading…
Cancel
Save