From 02b28046c7bb91816a50a028013a4f03792bd2b3 Mon Sep 17 00:00:00 2001 From: matttrach Date: Tue, 6 Feb 2024 19:35:01 -0600 Subject: [PATCH] feat: add the option to map a public ip to servers on subnet Signed-off-by: matttrach --- main.tf | 2 ++ modules/subnet/main.tf | 10 ++++++---- modules/subnet/variables.tf | 7 +++++++ variables.tf | 7 +++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/main.tf b/main.tf index 36df290..257e001 100644 --- a/main.tf +++ b/main.tf @@ -9,6 +9,7 @@ locals { subnet_name = var.subnet_name subnet_cidr = var.subnet_cidr # create when cidr is given, otherwise select with name or skip subnet_availability_zone = var.availability_zone # only used when creating + subnet_public_ip = var.subnet_public_ip # set this to true to enable public ip addressing on servers skip_subnet = var.skip_subnet # if using the "specific" security group type you can skip subnet creation security_group_name = var.security_group_name @@ -43,6 +44,7 @@ module "subnet" { vpc_id = module.vpc[0].id owner = local.owner availability_zone = local.subnet_availability_zone + public_ip = local.subnet_public_ip } module "security_group" { diff --git a/modules/subnet/main.tf b/modules/subnet/main.tf index 7f28c07..b80f959 100644 --- a/modules/subnet/main.tf +++ b/modules/subnet/main.tf @@ -6,6 +6,7 @@ locals { vpc_id = var.vpc_id owner = var.owner availability_zone = var.availability_zone + public_ip = var.public_ip } data "aws_subnet" "selected" { @@ -16,10 +17,11 @@ data "aws_subnet" "selected" { } } resource "aws_subnet" "new" { - count = local.create - vpc_id = local.vpc_id - cidr_block = local.cidr - availability_zone = local.availability_zone + count = local.create + vpc_id = local.vpc_id + cidr_block = local.cidr + availability_zone = local.availability_zone + map_public_ip_on_launch = local.public_ip tags = { Name = local.name Owner = local.owner diff --git a/modules/subnet/variables.tf b/modules/subnet/variables.tf index 17d6b20..6b58f37 100644 --- a/modules/subnet/variables.tf +++ b/modules/subnet/variables.tf @@ -37,3 +37,10 @@ variable "availability_zone" { EOT default = "" } +variable "public_ip" { + type = bool + description = <<-EOT + Set this to true to enable the subnet to have public IP addresses. + EOT + default = false +} \ No newline at end of file diff --git a/variables.tf b/variables.tf index d50455d..e3e9bd1 100644 --- a/variables.tf +++ b/variables.tf @@ -66,6 +66,13 @@ variable "subnet_cidr" { EOT default = "" } +variable "subnet_public_ip" { + type = bool + description = <<-EOT + Set this to true to enable the subnet to have public IP addresses. + EOT + default = false +} variable "availability_zone" { type = string description = <<-EOT