AWS nodes and user-data created from config map.

ansible-test
shnee 4 years ago
parent 3088e2295f
commit 9aa0a68be8

@ -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'
# equates to any ip address.
@ -13,6 +13,10 @@ worker-nodes = 2
node-memory = 2048
node-vcpus = 2
################################################################################
# AWS EC2 instance types
################################################################################
# 1 GiB, 1 vcpu, only one that is free.
# This one won't work with k8s because it requires at least 2 vcpus.
aws-ec2-instance-type = "t2.micro"
@ -20,6 +24,19 @@ aws-ec2-instance-type = "t2.micro"
# 4 GiB, 2 vcpus
# 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)
################################################################################
@ -53,6 +70,10 @@ base-image = "ami-0dd0ccab7e2801812"
# 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"
################################################################################
# Keys/Passwords
################################################################################
# Password hash created with:
# python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))'
# where "linux" is the password.

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