From 2e96171329b214435e06ce7eb7a87103eab52776 Mon Sep 17 00:00:00 2001 From: Curtis Wilson Date: Mon, 13 Dec 2021 19:42:41 -0500 Subject: [PATCH] Shared state is working. Qeurying for default VPC. --- k8s-nodes/main.tf | 49 +++++++------------ .../modules/aws-network-existing/main.tf | 23 +++++++++ .../modules/aws-network-existing/outputs.tf | 18 +++++++ .../modules/aws-network-existing/variables.tf | 4 ++ .../main.tf | 0 .../outputs.tf | 0 .../variables.tf | 0 k8s-nodes/variables.tf | 5 ++ 8 files changed, 69 insertions(+), 30 deletions(-) create mode 100644 k8s-nodes/modules/aws-network-existing/main.tf create mode 100644 k8s-nodes/modules/aws-network-existing/outputs.tf create mode 100644 k8s-nodes/modules/aws-network-existing/variables.tf rename k8s-nodes/modules/{aws-network => aws-network-from-scratch}/main.tf (100%) rename k8s-nodes/modules/{aws-network => aws-network-from-scratch}/outputs.tf (100%) rename k8s-nodes/modules/{aws-network => aws-network-from-scratch}/variables.tf (100%) diff --git a/k8s-nodes/main.tf b/k8s-nodes/main.tf index 64849b6..0b7b0ab 100644 --- a/k8s-nodes/main.tf +++ b/k8s-nodes/main.tf @@ -1,23 +1,3 @@ -terraform { - required_version = ">= 1.0.8" - - backend "s3" { - - bucket = "mss-terraform-state" - key = "global/s3/terraform.tfstate" - region = "us-gov-west-1" - - dynamodb_table = "mss-terraform-state-lock" - encrypt = true - - } - required_providers { - libvirt = { - source = "dmacvicar/libvirt" - version = "0.6.11" - } - } -} locals { nodes-config = { @@ -54,10 +34,6 @@ module "cloud-init-config" { # libvirt modules/resources. ################################################################################ -provider "aws" { - region = "us-gov-west-1" -} - # This module will grab the latest ami for a variety of distros. Uncomment to # get a list of the latest AMIs for our supported distros. # module "aws-amis" { @@ -67,14 +43,27 @@ provider "aws" { # 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 - subnet-cidr-block = var.aws-subnet-cidr-block - admin-ips = var.admin-ips +################################################################################ +# AWS Networking +# Use of the 2 modules below to create resources for the AWS network. +# aws-network-from-scratch will build the AWS network from scratch. +# aws-network-existing will query AWS for an existing VPC. +################################################################################ + +# module "aws-network-from-scratch" { +# source = "./modules/aws-network-from-scratch" +# 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 +# } + +module "aws-network-existing" { + source = "./modules/aws-network-existing" } +################################################################################ + # This key pair is not actually used. Keys are added to the nodes via cloud-init # instead. We just add this here that this key will show up in the AWS console." resource "aws_key_pair" "key" { diff --git a/k8s-nodes/modules/aws-network-existing/main.tf b/k8s-nodes/modules/aws-network-existing/main.tf new file mode 100644 index 0000000..81d25c3 --- /dev/null +++ b/k8s-nodes/modules/aws-network-existing/main.tf @@ -0,0 +1,23 @@ +locals { + az-to-subnets = { + for s in data.aws_subnet.subnets : s.availability_zone => s.id... + } +} + +data "aws_vpc" "default" { + tags = { + Name = var.default-vpc-name + } +} + +data "aws_subnets" "subnet-ids" { + filter { + name = "vpc-id" + values = [data.aws_vpc.default.id] + } +} + +data "aws_subnet" "subnets" { + for_each = toset(data.aws_subnets.subnet-ids.ids) + id = each.key +} diff --git a/k8s-nodes/modules/aws-network-existing/outputs.tf b/k8s-nodes/modules/aws-network-existing/outputs.tf new file mode 100644 index 0000000..f16e968 --- /dev/null +++ b/k8s-nodes/modules/aws-network-existing/outputs.tf @@ -0,0 +1,18 @@ +output "default-vpc" { + value = data.aws_vpc.default +} + +output "subnets" { + description = "An array of all subnets in default-vpc." + value = data.aws_subnet.subnets +} + +output "k8s-subnets" { + description = "An array of subnets to be used for k8s VMs. These subnets were chosen by selecting a single subnet from each availability_zone." + value = [for k,v in local.az-to-subnets : v[0]] +} + +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/k8s-nodes/modules/aws-network-existing/variables.tf b/k8s-nodes/modules/aws-network-existing/variables.tf new file mode 100644 index 0000000..b03ebd1 --- /dev/null +++ b/k8s-nodes/modules/aws-network-existing/variables.tf @@ -0,0 +1,4 @@ +variable "default-vpc-name" { + description = "The name of the existing default VPC. This module will query AWS for a VPC with this name," + default = "Managed VPC" +} diff --git a/k8s-nodes/modules/aws-network/main.tf b/k8s-nodes/modules/aws-network-from-scratch/main.tf similarity index 100% rename from k8s-nodes/modules/aws-network/main.tf rename to k8s-nodes/modules/aws-network-from-scratch/main.tf diff --git a/k8s-nodes/modules/aws-network/outputs.tf b/k8s-nodes/modules/aws-network-from-scratch/outputs.tf similarity index 100% rename from k8s-nodes/modules/aws-network/outputs.tf rename to k8s-nodes/modules/aws-network-from-scratch/outputs.tf diff --git a/k8s-nodes/modules/aws-network/variables.tf b/k8s-nodes/modules/aws-network-from-scratch/variables.tf similarity index 100% rename from k8s-nodes/modules/aws-network/variables.tf rename to k8s-nodes/modules/aws-network-from-scratch/variables.tf diff --git a/k8s-nodes/variables.tf b/k8s-nodes/variables.tf index cdba553..aa13de2 100644 --- a/k8s-nodes/variables.tf +++ b/k8s-nodes/variables.tf @@ -8,6 +8,11 @@ variable "aws-ec2-instance-type" { description = "The AWS instance type to use for all nodes." } +variable "aws-region" { + default = "us-east-1" + description = "The AWS region to use." +} + variable "aws-subnet-cidr-block" { default = "10.0.1.0/24" description = "The address space to be used for this subnet."