From df53ae047d0b11dcb1aa378ed107cda191932096 Mon Sep 17 00:00:00 2001 From: Curtis Wilson Date: Wed, 17 Nov 2021 21:50:07 -0500 Subject: [PATCH] AWS nodes and user-data created from config map. --- example.tfvars | 23 ++++++- main.tf | 83 +++++++++----------------- modules/cloud-init-config/main.tf | 9 +++ modules/cloud-init-config/outputs.tf | 3 + modules/cloud-init-config/variables.tf | 22 +++++++ variables.tf | 5 ++ 6 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 modules/cloud-init-config/main.tf create mode 100644 modules/cloud-init-config/outputs.tf create mode 100644 modules/cloud-init-config/variables.tf diff --git a/example.tfvars b/example.tfvars index 4b6100d..4ea7b3c 100644 --- a/example.tfvars +++ b/example.tfvars @@ -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. diff --git a/main.tf b/main.tf index 69c7943..ce42da9 100644 --- a/main.tf +++ b/main.tf @@ -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 } } diff --git a/modules/cloud-init-config/main.tf b/modules/cloud-init-config/main.tf new file mode 100644 index 0000000..e1c75c4 --- /dev/null +++ b/modules/cloud-init-config/main.tf @@ -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 +} diff --git a/modules/cloud-init-config/outputs.tf b/modules/cloud-init-config/outputs.tf new file mode 100644 index 0000000..15415d4 --- /dev/null +++ b/modules/cloud-init-config/outputs.tf @@ -0,0 +1,3 @@ +output "user-datas" { + value = data.template_file.user-datas +} diff --git a/modules/cloud-init-config/variables.tf b/modules/cloud-init-config/variables.tf new file mode 100644 index 0000000..fc4f437 --- /dev/null +++ b/modules/cloud-init-config/variables.tf @@ -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." +} + diff --git a/variables.tf b/variables.tf index 52cf7a2..e8f3211 100644 --- a/variables.tf +++ b/variables.tf @@ -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." }