From 7c266ec4aa37af12f11cc7b8afa203781d207edc Mon Sep 17 00:00:00 2001 From: Curtis Wilson Date: Tue, 16 Nov 2021 21:01:34 -0500 Subject: [PATCH] Added aws-ami module. --- main.tf | 46 +++++++++++++++++------------ modules/aws-amis/main.tf | 58 +++++++++++++++++++++++++++++++++++++ modules/aws-amis/outputs.tf | 3 ++ 3 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 modules/aws-amis/main.tf create mode 100644 modules/aws-amis/outputs.tf diff --git a/main.tf b/main.tf index 50982d6..7113df0 100644 --- a/main.tf +++ b/main.tf @@ -12,25 +12,25 @@ 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 -# } +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 +} data "template_file" "amzn2-node-user-datas" { template = file("${path.module}/cloud_init.cfg") @@ -92,6 +92,10 @@ provider "aws" { region = "us-east-2" } +module "aws-amis" { + source = "./modules/aws-amis" +} + module "aws-network" { source = "./modules/aws-network" name-prefix = var.vm-name-prefix @@ -165,6 +169,10 @@ module "centos8-nodes" { name-prefix = "${var.vm-name-prefix}-centos8" } +output "amis" { + value = module.aws-amis.amis +} + # module "master-nodes" { # source = "./modules/aws-nodes" # ami = var.base-image diff --git a/modules/aws-amis/main.tf b/modules/aws-amis/main.tf new file mode 100644 index 0000000..1949653 --- /dev/null +++ b/modules/aws-amis/main.tf @@ -0,0 +1,58 @@ +locals { + amis = { + amzn2 = { + owner-id = "137112412989" + name = "amzn2-ami-hvm-2*x86_64-gp2" + }, + ubuntu = { + owner-id = "099720109477" + name = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*" + }, + centos7 = { + owner-id = "125523088429" + name = "CentOS 7.*x86_64" + }, + centos8 = { + owner-id = "125523088429" + name = "CentOS 8.*x86_64" + }, + arch = { + owner-id = "093273469852" + name = "arch-linux-lts-hvm*x86_64-ebs" + }, + rhel7 = { + owner-id = "309956199498" + name = "RHEL-7.*HVM*x86_64*GP2" + }, + rhel8 = { + owner-id = "309956199498" + name = "RHEL-8.*HVM*x86_64*GP2" + } + } +} + +data "aws_ami" "amis" { + for_each = local.amis + most_recent = true + owners = [each.value.owner-id] + + filter { + name = "name" + values = [each.value.name] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + filter { + name = "architecture" + values = ["x86_64"] + } + + filter { + name = "root-device-type" + values = ["ebs"] + } +} diff --git a/modules/aws-amis/outputs.tf b/modules/aws-amis/outputs.tf new file mode 100644 index 0000000..3174fd9 --- /dev/null +++ b/modules/aws-amis/outputs.tf @@ -0,0 +1,3 @@ +output "amis" { + value = tomap({ for type, ami in data.aws_ami.amis : type => ami.id }) +}