diff --git a/data/data/aws/bootstrap/main.tf b/data/data/aws/bootstrap/main.tf index d65c583f7e0..2b5cbfa869b 100644 --- a/data/data/aws/bootstrap/main.tf +++ b/data/data/aws/bootstrap/main.tf @@ -1,3 +1,7 @@ +locals { + public_endpoints = var.publish_strategy == "External" ? true : false +} + resource "aws_s3_bucket" "ignition" { acl = "private" @@ -117,7 +121,7 @@ resource "aws_instance" "bootstrap" { subnet_id = var.subnet_id user_data = data.ignition_config.redirect.rendered vpc_security_group_ids = flatten([var.vpc_security_group_ids, aws_security_group.bootstrap.id]) - associate_public_ip_address = true + associate_public_ip_address = local.public_endpoints lifecycle { # Ignore changes in the AMI which force recreation of the resource. This @@ -147,7 +151,9 @@ resource "aws_instance" "bootstrap" { } resource "aws_lb_target_group_attachment" "bootstrap" { - count = var.target_group_arns_length + // Because of the issue https://github.com/hashicorp/terraform/issues/12570, the consumers cannot use a dynamic list for count + // and therefore are force to implicitly assume that the list is of aws_lb_target_group_arns_length - 1, in case there is no api_external + count = local.public_endpoints ? var.target_group_arns_length : var.target_group_arns_length - 1 target_group_arn = var.target_group_arns[count.index] target_id = aws_instance.bootstrap.private_ip @@ -173,7 +179,7 @@ resource "aws_security_group_rule" "ssh" { security_group_id = aws_security_group.bootstrap.id protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] + cidr_blocks = local.public_endpoints ? ["0.0.0.0/0"] : var.vpc_cidrs from_port = 22 to_port = 22 } @@ -183,7 +189,7 @@ resource "aws_security_group_rule" "bootstrap_journald_gateway" { security_group_id = aws_security_group.bootstrap.id protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] + cidr_blocks = local.public_endpoints ? ["0.0.0.0/0"] : var.vpc_cidrs from_port = 19531 to_port = 19531 } diff --git a/data/data/aws/bootstrap/variables.tf b/data/data/aws/bootstrap/variables.tf index 7522f374cfc..a37e9612e3a 100644 --- a/data/data/aws/bootstrap/variables.tf +++ b/data/data/aws/bootstrap/variables.tf @@ -62,9 +62,19 @@ variable "vpc_id" { description = "VPC ID is used to create resources like security group rules for bootstrap machine." } +variable "vpc_cidrs" { + type = list(string) + default = [] + description = "VPC CIDR blocks." +} + variable "vpc_security_group_ids" { type = list(string) default = [] description = "VPC security group IDs for the bootstrap node." } +variable "publish_strategy" { + type = string + description = "The publishing strategy for endpoints like load balancers" +} diff --git a/data/data/aws/main.tf b/data/data/aws/main.tf index 4eea5257557..a7b6ec6d823 100644 --- a/data/data/aws/main.tf +++ b/data/data/aws/main.tf @@ -18,11 +18,13 @@ module "bootstrap" { instance_type = var.aws_bootstrap_instance_type cluster_id = var.cluster_id ignition = var.ignition_bootstrap - subnet_id = module.vpc.az_to_public_subnet_id[var.aws_master_availability_zones[0]] + subnet_id = var.aws_publish_strategy == "External" ? module.vpc.az_to_public_subnet_id[var.aws_master_availability_zones[0]] : module.vpc.az_to_private_subnet_id[var.aws_master_availability_zones[0]] target_group_arns = module.vpc.aws_lb_target_group_arns target_group_arns_length = module.vpc.aws_lb_target_group_arns_length vpc_id = module.vpc.vpc_id + vpc_cidrs = module.vpc.vpc_cidrs vpc_security_group_ids = [module.vpc.master_sg_id] + publish_strategy = var.aws_publish_strategy tags = local.tags } @@ -46,6 +48,7 @@ module "masters" { target_group_arns_length = module.vpc.aws_lb_target_group_arns_length ec2_ami = aws_ami_copy.main.id user_data_ign = var.ignition_master + publish_strategy = var.aws_publish_strategy } module "iam" { @@ -70,17 +73,19 @@ module "dns" { etcd_ip_addresses = flatten(module.masters.ip_addresses) tags = local.tags vpc_id = module.vpc.vpc_id + publish_strategy = var.aws_publish_strategy } module "vpc" { source = "./vpc" - cidr_block = var.machine_cidr - cluster_id = var.cluster_id - region = var.aws_region - vpc = var.aws_vpc - public_subnets = var.aws_public_subnets - private_subnets = var.aws_private_subnets + cidr_block = var.machine_cidr + cluster_id = var.cluster_id + region = var.aws_region + vpc = var.aws_vpc + public_subnets = var.aws_public_subnets + private_subnets = var.aws_private_subnets + publish_strategy = var.aws_publish_strategy availability_zones = distinct( concat( diff --git a/data/data/aws/master/main.tf b/data/data/aws/master/main.tf index d338ee7d508..6a74f78e162 100644 --- a/data/data/aws/master/main.tf +++ b/data/data/aws/master/main.tf @@ -1,5 +1,9 @@ locals { arn = "aws" + + // Because of the issue https://github.com/hashicorp/terraform/issues/12570, the consumers cannot use a dynamic list for count + // and therefore are force to implicitly assume that the list is of aws_lb_target_group_arns_length - 1, in case there is no api_external + target_group_arns_length = var.publish_strategy == "External" ? var.target_group_arns_length : var.target_group_arns_length - 1 } resource "aws_iam_instance_profile" "master" { @@ -128,9 +132,9 @@ resource "aws_instance" "master" { } resource "aws_lb_target_group_attachment" "master" { - count = var.instance_count * var.target_group_arns_length + count = var.instance_count * local.target_group_arns_length - target_group_arn = var.target_group_arns[count.index % var.target_group_arns_length] - target_id = aws_instance.master[floor(count.index / var.target_group_arns_length)].private_ip + target_group_arn = var.target_group_arns[count.index % local.target_group_arns_length] + target_id = aws_instance.master[floor(count.index / local.target_group_arns_length)].private_ip } diff --git a/data/data/aws/master/variables.tf b/data/data/aws/master/variables.tf index 1554a8fd228..8ff122ed36c 100644 --- a/data/data/aws/master/variables.tf +++ b/data/data/aws/master/variables.tf @@ -70,3 +70,13 @@ variable "user_data_ign" { type = string } +variable "publish_strategy" { + type = string + description = <