From 0260b2346d7256c973a3cb3cc9834a8484cd15b0 Mon Sep 17 00:00:00 2001 From: Marcus Wyche Date: Thu, 23 Jul 2026 06:01:00 -0400 Subject: [PATCH 1/8] Updates to start pushing independent terraform modules --- terraform/.gitignore | 34 +++++++++++++++++++++++++++++++++ terraform/README.md | 4 ++++ terraform/home-ec2/README.md | 3 +++ terraform/home-ec2/main.tf | 27 ++++++++++++++++++++++++++ terraform/home-ec2/outputs.tf | 4 ++++ terraform/home-ec2/providers.tf | 12 ++++++++++++ terraform/home-ec2/variables.tf | 4 ++++ 7 files changed, 88 insertions(+) create mode 100644 terraform/.gitignore create mode 100644 terraform/README.md create mode 100644 terraform/home-ec2/README.md create mode 100644 terraform/home-ec2/main.tf create mode 100644 terraform/home-ec2/outputs.tf create mode 100644 terraform/home-ec2/providers.tf create mode 100644 terraform/home-ec2/variables.tf diff --git a/terraform/.gitignore b/terraform/.gitignore new file mode 100644 index 0000000..9d8f551 --- /dev/null +++ b/terraform/.gitignore @@ -0,0 +1,34 @@ +*.auto.tfvars +.terraform.lock.hcl +terraform.state.* + +# Local .terraform directories (contains provider plugins and modules) +.terraform/ +.terraform.lock.hcl.tmp + +# Terraform state files (may contain sensitive data/passwords) +*.tfstate +*.tfstate.* +*.tfstate.backup + +# Crash logs +crash.log +crash.*.log + +# Variable files containing sensitive values / secrets +# (Uncomment the line below if you keep non-sensitive examples like 'terraform.tfvars.example' in git) +*.tfvars +*.tfvars.json + +# Override files (used for local development overrides) +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# CLI configuration files +.terraformrc +terraform.rc + +# Execution plan files (generated by 'terraform plan -out=...') +*.tfplan diff --git a/terraform/README.md b/terraform/README.md new file mode 100644 index 0000000..fccba44 --- /dev/null +++ b/terraform/README.md @@ -0,0 +1,4 @@ +# Terraform + +Sandox for learning terraform and hold onto any modules that are easy recipes that I may use other places and willing to share. + diff --git a/terraform/home-ec2/README.md b/terraform/home-ec2/README.md new file mode 100644 index 0000000..b0021bc --- /dev/null +++ b/terraform/home-ec2/README.md @@ -0,0 +1,3 @@ +# Home EC2 + +Simple Module to create an ec2 instance for you to login to from your home network and only you can access. diff --git a/terraform/home-ec2/main.tf b/terraform/home-ec2/main.tf new file mode 100644 index 0000000..3d168c5 --- /dev/null +++ b/terraform/home-ec2/main.tf @@ -0,0 +1,27 @@ +# Automatically query your current local workstation's public IP +data "http" "my_public_ip" { + url = "https://ifconfig.me/ip" +} + +# Creates the security group needed for connecting only from your local box +resource "aws_security_group" "ssh_restricted" { + name = "ssh-only-my-ip" + description = "Block all inbound SSH access except for the deployer workspace" + + ingress { + description = "SSH from my workstation only" + from_port = 22 + to_port = 22 + protocol = "tcp" + # Append the /32 routing suffix dynamically to lock it to your single IP + cidr_blocks = ["${chomp(data.http.my_public_ip.response_body)}/32"] + } + + egress { + description = "Allow all outbound infrastructure traffic" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} \ No newline at end of file diff --git a/terraform/home-ec2/outputs.tf b/terraform/home-ec2/outputs.tf new file mode 100644 index 0000000..749c020 --- /dev/null +++ b/terraform/home-ec2/outputs.tf @@ -0,0 +1,4 @@ +output "home_ip" { + description = "IP of your local workstation" + value = data.http.my_public_ip.body +} \ No newline at end of file diff --git a/terraform/home-ec2/providers.tf b/terraform/home-ec2/providers.tf new file mode 100644 index 0000000..e9e5876 --- /dev/null +++ b/terraform/home-ec2/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 6.0" + } + } +} + +provider "aws" { + profile = var.aws_profile_name +} \ No newline at end of file diff --git a/terraform/home-ec2/variables.tf b/terraform/home-ec2/variables.tf new file mode 100644 index 0000000..11578b3 --- /dev/null +++ b/terraform/home-ec2/variables.tf @@ -0,0 +1,4 @@ +variable "aws_profile_name" { + description = "Name of your local aws profile" + type = string +} \ No newline at end of file From b481a545050ad333a1459cd53cddc1069e45b12e Mon Sep 17 00:00:00 2001 From: Marcus Wyche Date: Thu, 23 Jul 2026 07:12:15 -0400 Subject: [PATCH 2/8] Not quite working yet but code is there now just need to debug --- terraform/home-ec2/main.tf | 29 ++++++++++++++++++++++++++ terraform/home-ec2/outputs.tf | 19 ++++++++++++++++- terraform/home-ec2/variables.tf | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/terraform/home-ec2/main.tf b/terraform/home-ec2/main.tf index 3d168c5..ca5c60c 100644 --- a/terraform/home-ec2/main.tf +++ b/terraform/home-ec2/main.tf @@ -24,4 +24,33 @@ resource "aws_security_group" "ssh_restricted" { protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } +} + +# Key pair ssh +data "aws_key_pair" "key_pair" { + key_name = var.aws_key_pair_name +} + +# Try filtering things out +data "aws_ami" "ami" { + most_recent = true + owners = ["amazon"] # Official Canonical AWS Account ID + + filter { + name = "image-id" + values = [var.aws_ami_image_id] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } +} + +resource "aws_instance" "ec2_instance" { + ami = data.aws_ami.ami.id + instance_type = var.aws_instance_type + vpc_security_group_ids = concat([aws_security_group.ssh_restricted.id], var.aws_additional_security_groups) + key_name = data.aws_key_pair.key_pair.key_name + user_data = var.aws_instance_startup_script } \ No newline at end of file diff --git a/terraform/home-ec2/outputs.tf b/terraform/home-ec2/outputs.tf index 749c020..09d7461 100644 --- a/terraform/home-ec2/outputs.tf +++ b/terraform/home-ec2/outputs.tf @@ -1,4 +1,21 @@ output "home_ip" { description = "IP of your local workstation" - value = data.http.my_public_ip.body + value = data.http.my_public_ip.response_body +} + +output "home_sg" { + description = "Reference to the home security group" + value = { + arn = aws_security_group.ssh_restricted.arn + id = aws_security_group.ssh_restricted.id + } +} + +output "ec2_info" { + value = aws_instance.ec2_instance +} + +output "ssh_command" { + description = "SSH Command you can run to access the image" + value = "ssh ec2-user@${aws_instance.ec2_instance.public_dns}" } \ No newline at end of file diff --git a/terraform/home-ec2/variables.tf b/terraform/home-ec2/variables.tf index 11578b3..b8041d0 100644 --- a/terraform/home-ec2/variables.tf +++ b/terraform/home-ec2/variables.tf @@ -1,4 +1,40 @@ variable "aws_profile_name" { description = "Name of your local aws profile" type = string +} + +variable "aws_key_pair_name" { + description = "Name of your AWS key pair that will be used for ssh" + type = string +} + +# TODO: Make this work... +# variable "aws_ami_name_filter" { +# description = "Filter for finding the hvm ami" +# type = list(string) +# default = ["ubuntu/images/hvm-ssd/ubuntu-resolute-26.04-amd64-server-*"] +# } + +variable "aws_ami_image_id" { + description = "image id for the ami." + type = string + default = "ami-0b6d9d3d33ba97d99" +} + +variable "aws_instance_type" { + description = "Size of the image to stand up" + type = string + default = "t3a.nano" +} + +variable "aws_additional_security_groups" { + description = "Additional security groups on top of the ssh only one created by module" + type = list(string) + default = [] +} + +variable "aws_instance_startup_script" { + description = "Startup script for instance." + type = string + default = null } \ No newline at end of file From af0c07d8b438c10aff43d26ca4069abfcf8f09c7 Mon Sep 17 00:00:00 2001 From: Marcus Wyche Date: Sat, 25 Jul 2026 06:36:49 -0400 Subject: [PATCH 3/8] Wrapping this module up --- terraform/home-ec2/README.md | 6 +++ terraform/home-ec2/main.tf | 90 ++++++++++++++++++++++++++------- terraform/home-ec2/outputs.tf | 13 +++++ terraform/home-ec2/providers.tf | 8 ++- terraform/home-ec2/variables.tf | 3 +- 5 files changed, 101 insertions(+), 19 deletions(-) diff --git a/terraform/home-ec2/README.md b/terraform/home-ec2/README.md index b0021bc..7cb7adc 100644 --- a/terraform/home-ec2/README.md +++ b/terraform/home-ec2/README.md @@ -1,3 +1,9 @@ # Home EC2 Simple Module to create an ec2 instance for you to login to from your home network and only you can access. + + +## TODO + +- Figure out SSH from ipv6 instead of needing to force ipv4 +- Add a default user so you're not dependent on the default user in the AMI the issue I have with this is it makes passing down userdata a bit more complex but I'm sure it can be handled. \ No newline at end of file diff --git a/terraform/home-ec2/main.tf b/terraform/home-ec2/main.tf index ca5c60c..b74a209 100644 --- a/terraform/home-ec2/main.tf +++ b/terraform/home-ec2/main.tf @@ -1,29 +1,82 @@ +locals { + current_timestamp = timestamp() + formatted_timestamp = formatdate("YYYY-MM-DD'T'hh:mmZ", local.current_timestamp) +} + +data "external" "local_user" { + program = ["sh", "-c", "echo \"{\\\"user\\\": \\\"$(whoami)\\\"}\""] +} + +data "external" "force_ipv4" { + program = ["sh", "-c", "echo \"{\\\"ipv4\\\": \\\"$(curl -4 ifconfig.me)\\\"}\""] +} + # Automatically query your current local workstation's public IP data "http" "my_public_ip" { url = "https://ifconfig.me/ip" } -# Creates the security group needed for connecting only from your local box +locals { + is_ipv6 = can(regex(":", data.http.my_public_ip.response_body)) +} + +# 1. Base Security Group Container (Always created) resource "aws_security_group" "ssh_restricted" { - name = "ssh-only-my-ip" + name = "ssh-only-my-ip-test" description = "Block all inbound SSH access except for the deployer workspace" +} - ingress { - description = "SSH from my workstation only" - from_port = 22 - to_port = 22 - protocol = "tcp" - # Append the /32 routing suffix dynamically to lock it to your single IP - cidr_blocks = ["${chomp(data.http.my_public_ip.response_body)}/32"] - } +# ========================================== +# IPv4 Rules (Created when local.is_ipv6 = false) +# ========================================== - egress { - description = "Allow all outbound infrastructure traffic" - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } +# Ingress: SSH via IPv4 +# TODO: figure out how to make this work. +resource "aws_vpc_security_group_ingress_rule" "ssh_ingress_v4" { + # TODO: If you can figure out the ipv6 rule hanging then you can make this conditional + # count = !local.is_ipv6 ? 1 : 0 + security_group_id = aws_security_group.ssh_restricted.id + description = "SSH from my workstation (IPv4)" + + ip_protocol = "tcp" + from_port = 22 + to_port = 22 + cidr_ipv4 = "${chomp(data.external.force_ipv4.result["ipv4"])}/32" +} + +# Ingress: SSH via IPv6 +# TODO: Below does not work need to figure out proper way to set this up... +resource "aws_vpc_security_group_ingress_rule" "ssh_ingress_v6" { + count = local.is_ipv6 ? 1 : 0 + security_group_id = aws_security_group.ssh_restricted.id + description = "SSH from my workstation (IPv6)" + + ip_protocol = "tcp" + from_port = 22 + to_port = 22 + cidr_ipv6 = "${chomp(data.http.my_public_ip.response_body)}/128" +} + +# Egress: All Outbound via IPv4 +resource "aws_vpc_security_group_egress_rule" "all_egress_v4" { + security_group_id = aws_security_group.ssh_restricted.id + description = "Allow all outbound IPv4 traffic" + + ip_protocol = "-1" + cidr_ipv4 = "0.0.0.0/0" +} + +# ========================================== +# IPv6 Rules (Created when local.is_ipv6 = true) +# ========================================== + +# Egress: All Outbound via IPv6 +resource "aws_vpc_security_group_egress_rule" "all_egress_v6" { + security_group_id = aws_security_group.ssh_restricted.id + description = "Allow all outbound IPv6 traffic" + + ip_protocol = "-1" + cidr_ipv6 = "::/0" } # Key pair ssh @@ -53,4 +106,7 @@ resource "aws_instance" "ec2_instance" { vpc_security_group_ids = concat([aws_security_group.ssh_restricted.id], var.aws_additional_security_groups) key_name = data.aws_key_pair.key_pair.key_name user_data = var.aws_instance_startup_script + tags = { + Name = "home-ec2-${local.formatted_timestamp}" + } } \ No newline at end of file diff --git a/terraform/home-ec2/outputs.tf b/terraform/home-ec2/outputs.tf index 09d7461..099170e 100644 --- a/terraform/home-ec2/outputs.tf +++ b/terraform/home-ec2/outputs.tf @@ -15,7 +15,20 @@ output "ec2_info" { value = aws_instance.ec2_instance } +output "local_os_username" { + value = data.external.local_user.result["user"] +} + +output "focer_ipv4" { + value = data.external.force_ipv4.result +} + output "ssh_command" { description = "SSH Command you can run to access the image" value = "ssh ec2-user@${aws_instance.ec2_instance.public_dns}" +} + +output "router_is_ipv6" { + description = "Is router defaulting to ipv6" + value = local.is_ipv6 } \ No newline at end of file diff --git a/terraform/home-ec2/providers.tf b/terraform/home-ec2/providers.tf index e9e5876..bfb2fda 100644 --- a/terraform/home-ec2/providers.tf +++ b/terraform/home-ec2/providers.tf @@ -4,9 +4,15 @@ terraform { source = "hashicorp/aws" version = "~> 6.0" } + external = { + source = "hashicorp/external" + version = "~> 2.3" + } } } provider "aws" { profile = var.aws_profile_name -} \ No newline at end of file +} + +provider "external" {} \ No newline at end of file diff --git a/terraform/home-ec2/variables.tf b/terraform/home-ec2/variables.tf index b8041d0..a13c554 100644 --- a/terraform/home-ec2/variables.tf +++ b/terraform/home-ec2/variables.tf @@ -18,7 +18,8 @@ variable "aws_key_pair_name" { variable "aws_ami_image_id" { description = "image id for the ami." type = string - default = "ami-0b6d9d3d33ba97d99" + default = "ami-01edba92f9036f76e" + # default = "ami-0b6d9d3d33ba97d99" } variable "aws_instance_type" { From c4f84273ddde92550bcd1299e9373cc414f89c09 Mon Sep 17 00:00:00 2001 From: Marcus Wyche Date: Sat, 25 Jul 2026 06:44:52 -0400 Subject: [PATCH 4/8] Updates to my readme --- terraform/home-ec2/README.md | 69 +++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/terraform/home-ec2/README.md b/terraform/home-ec2/README.md index 7cb7adc..6e99728 100644 --- a/terraform/home-ec2/README.md +++ b/terraform/home-ec2/README.md @@ -1,6 +1,73 @@ # Home EC2 -Simple Module to create an ec2 instance for you to login to from your home network and only you can access. +Simple Module to create an ec2 instance for you to login to from your home network and only you can access. This can be used for tunneling to various resources in your VPC from your local box. + +## Commands + +### Create the EC2 + +```bash +MacBookPro:home-ec2 mkwyche$ terraform apply +data.external.force_ipv4: Reading... +data.external.local_user: Reading... +data.http.my_public_ip: Reading... +data.external.local_user: Read complete after 0s [id=-] +data.external.force_ipv4: Read complete after 0s [id=-] +data.http.my_public_ip: Read complete after 0s [id=https://ifconfig.me/ip] +data.aws_key_pair.key_pair: Reading... +data.aws_ami.ami: Reading... +data.aws_key_pair.key_pair: Read complete after 0s [id=key-0aacf6008840baa11] +data.aws_ami.ami: Read complete after 0s [id=ami-01edba92f9036f76e] + +Apply complete! Resources: 6 added, 0 changed, 0 destroyed. +``` + +### SSH into the EC2 + +If your key has been added locally to ssh(`ssh-add -l`) you can now easily ssh in. + +```bash +ssh ec2-user@ec2-13-220-44-159.compute-1.amazonaws.com +The authenticity of host 'ec2-13-220-44-159.compute-1.amazonaws.com (13.220.44.159)' can't be established. +Are you sure you want to continue connecting (yes/no/[fingerprint])? yes +Warning: Permanently added 'ec2-13-220-44-159.compute-1.amazonaws.com' (ED25519) to the list of known hosts. +** WARNING: connection is not using a post-quantum key exchange algorithm. +** This session may be vulnerable to "store now, decrypt later" attacks. +** The server may need to be upgraded. See https://openssh.com/pq.html + , #_ + ~\_ ####_ Amazon Linux 2023 + ~~ \_#####\ + ~~ \###| + ~~ \#/ ___ https://aws.amazon.com/linux/amazon-linux-2023 + ~~ V~' '-> + ~~~ / + ~~._. _/ + _/ _/ + _/m/' +[ec2-user@ip-172-31-13-45 ~]$ +``` + +### Destroy the EC2 + +```bash +aws_vpc_security_group_ingress_rule.ssh_ingress_v4: Destroying... [id=sgr-0de0b6d7878d047ff] +aws_vpc_security_group_egress_rule.all_egress_v4: Destroying... [id=sgr-00fe6cc6be11c98e3] +aws_vpc_security_group_ingress_rule.ssh_ingress_v6[0]: Destroying... [id=sgr-014f4d42101b6e6c1] +aws_vpc_security_group_egress_rule.all_egress_v6: Destroying... [id=sgr-0f4ddbc6d5e168af3] +aws_instance.ec2_instance: Destroying... [id=i-03edc197398ff1491] +aws_vpc_security_group_egress_rule.all_egress_v6: Destruction complete after 1s +aws_vpc_security_group_ingress_rule.ssh_ingress_v6[0]: Destruction complete after 1s +aws_vpc_security_group_ingress_rule.ssh_ingress_v4: Destruction complete after 1s +aws_vpc_security_group_egress_rule.all_egress_v4: Destruction complete after 1s +aws_instance.ec2_instance: Still destroying... [id=i-03edc197398ff1491, 00m10s elapsed] +aws_instance.ec2_instance: Still destroying... [id=i-03edc197398ff1491, 00m20s elapsed] +aws_instance.ec2_instance: Still destroying... [id=i-03edc197398ff1491, 00m30s elapsed] +aws_instance.ec2_instance: Destruction complete after 30s +aws_security_group.ssh_restricted: Destroying... [id=sg-08ad998def9119e33] +aws_security_group.ssh_restricted: Destruction complete after 1s + +Destroy complete! Resources: 6 destroyed. +``` ## TODO From a9afa0f275c1b1f1b1e01ebb31132a8e02985359 Mon Sep 17 00:00:00 2001 From: Marcus Wyche Date: Sun, 26 Jul 2026 07:38:48 -0400 Subject: [PATCH 5/8] Formatting and adding in ability to specify Name and Subnet ID for the ec2 instance --- terraform/home-ec2/main.tf | 34 +++++++++++++++-------- terraform/home-ec2/outputs.tf | 22 +++++++-------- terraform/home-ec2/variables.tf | 48 ++++++++++++++++++++++----------- 3 files changed, 67 insertions(+), 37 deletions(-) diff --git a/terraform/home-ec2/main.tf b/terraform/home-ec2/main.tf index b74a209..3c676ec 100644 --- a/terraform/home-ec2/main.tf +++ b/terraform/home-ec2/main.tf @@ -1,6 +1,17 @@ +# Pulls the AWS Subnet if provided by the user. +data "aws_subnet" "subnet" { + count = var.aws_subnet_id != null ? 1 : 0 + id = var.aws_subnet_id +} + +locals { + current_timestamp = timestamp() + formatted_timestamp = formatdate("YYYY-MM-DD'T'hh:mmZ", local.current_timestamp) +} + locals { - current_timestamp = timestamp() - formatted_timestamp = formatdate("YYYY-MM-DD'T'hh:mmZ", local.current_timestamp) + # Just pull out the first default subnet + provided_subnet = var.aws_subnet_id != null ? data.aws_subnet.subnet[0].id : null } data "external" "local_user" { @@ -81,7 +92,7 @@ resource "aws_vpc_security_group_egress_rule" "all_egress_v6" { # Key pair ssh data "aws_key_pair" "key_pair" { - key_name = var.aws_key_pair_name + key_name = var.aws_key_pair_name } # Try filtering things out @@ -101,12 +112,13 @@ data "aws_ami" "ami" { } resource "aws_instance" "ec2_instance" { - ami = data.aws_ami.ami.id - instance_type = var.aws_instance_type - vpc_security_group_ids = concat([aws_security_group.ssh_restricted.id], var.aws_additional_security_groups) - key_name = data.aws_key_pair.key_pair.key_name - user_data = var.aws_instance_startup_script - tags = { - Name = "home-ec2-${local.formatted_timestamp}" - } + ami = data.aws_ami.ami.id + instance_type = var.aws_instance_type + vpc_security_group_ids = concat([aws_security_group.ssh_restricted.id], var.aws_additional_security_groups) + key_name = data.aws_key_pair.key_pair.key_name + user_data = var.aws_instance_startup_script + subnet_id = local.provided_subnet + tags = { + Name = var.aws_ec2_instance_name + } } \ No newline at end of file diff --git a/terraform/home-ec2/outputs.tf b/terraform/home-ec2/outputs.tf index 099170e..2bcd3d9 100644 --- a/terraform/home-ec2/outputs.tf +++ b/terraform/home-ec2/outputs.tf @@ -4,15 +4,15 @@ output "home_ip" { } output "home_sg" { - description = "Reference to the home security group" - value = { - arn = aws_security_group.ssh_restricted.arn - id = aws_security_group.ssh_restricted.id - } + description = "Reference to the home security group" + value = { + arn = aws_security_group.ssh_restricted.arn + id = aws_security_group.ssh_restricted.id + } } output "ec2_info" { - value = aws_instance.ec2_instance + value = aws_instance.ec2_instance } output "local_os_username" { @@ -20,15 +20,15 @@ output "local_os_username" { } output "focer_ipv4" { - value = data.external.force_ipv4.result + value = data.external.force_ipv4.result } output "ssh_command" { - description = "SSH Command you can run to access the image" - value = "ssh ec2-user@${aws_instance.ec2_instance.public_dns}" + description = "SSH Command you can run to access the image" + value = "ssh ec2-user@${aws_instance.ec2_instance.public_dns}" } output "router_is_ipv6" { - description = "Is router defaulting to ipv6" - value = local.is_ipv6 + description = "Is router defaulting to ipv6" + value = local.is_ipv6 } \ No newline at end of file diff --git a/terraform/home-ec2/variables.tf b/terraform/home-ec2/variables.tf index a13c554..6ec6473 100644 --- a/terraform/home-ec2/variables.tf +++ b/terraform/home-ec2/variables.tf @@ -4,8 +4,8 @@ variable "aws_profile_name" { } variable "aws_key_pair_name" { - description = "Name of your AWS key pair that will be used for ssh" - type = string + description = "Name of your AWS key pair that will be used for ssh" + type = string } # TODO: Make this work... @@ -16,26 +16,44 @@ variable "aws_key_pair_name" { # } variable "aws_ami_image_id" { - description = "image id for the ami." - type = string - default = "ami-01edba92f9036f76e" - # default = "ami-0b6d9d3d33ba97d99" + description = "image id for the ami." + type = string + default = "ami-01edba92f9036f76e" + # default = "ami-0b6d9d3d33ba97d99" } variable "aws_instance_type" { - description = "Size of the image to stand up" - type = string - default = "t3a.nano" + description = "Size of the image to stand up" + type = string + default = "t3a.nano" } variable "aws_additional_security_groups" { - description = "Additional security groups on top of the ssh only one created by module" - type = list(string) - default = [] + description = "Additional security groups on top of the ssh only one created by module" + type = list(string) + default = [] } variable "aws_instance_startup_script" { - description = "Startup script for instance." - type = string - default = null + description = "Startup script for instance." + type = string + default = null +} + +variable "aws_vpc_id" { + description = "Id of your vpc" + type = string + default = null +} + +variable "aws_subnet_id" { + description = "id for the subnet" + type = string + default = null +} + +variable "aws_ec2_instance_name" { + description = "Name for the EC2 Instance" + type = string + default = "home-ec2" } \ No newline at end of file From d64d7f928149f7c984d1e90e18be47be3bdd2393 Mon Sep 17 00:00:00 2001 From: Marcus Wyche Date: Sun, 26 Jul 2026 07:54:39 -0400 Subject: [PATCH 6/8] Cleaning up outputs --- terraform/home-ec2/README.md | 3 ++- terraform/home-ec2/main.tf | 18 +++++++++++++++++ terraform/home-ec2/outputs.tf | 38 +++++++++-------------------------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/terraform/home-ec2/README.md b/terraform/home-ec2/README.md index 6e99728..0d48663 100644 --- a/terraform/home-ec2/README.md +++ b/terraform/home-ec2/README.md @@ -73,4 +73,5 @@ Destroy complete! Resources: 6 destroyed. ## TODO - Figure out SSH from ipv6 instead of needing to force ipv4 -- Add a default user so you're not dependent on the default user in the AMI the issue I have with this is it makes passing down userdata a bit more complex but I'm sure it can be handled. \ No newline at end of file +- Add a default user so you're not dependent on the default user in the AMI the issue I have with this is it makes passing down userdata a bit more complex but I'm sure it can be handled. +- Add dynamic filtering to just provide a OS name and it'll pull latest AMI for that OS. \ No newline at end of file diff --git a/terraform/home-ec2/main.tf b/terraform/home-ec2/main.tf index 3c676ec..87ba00d 100644 --- a/terraform/home-ec2/main.tf +++ b/terraform/home-ec2/main.tf @@ -111,6 +111,24 @@ data "aws_ami" "ami" { } } +# 2. Infer default username from AMI name +# TODO: Eventually just add the user but below will work in the mean time. +locals { + ami_name = lower(data.aws_ami.ami.name) + + default_user = ( + can(regex("ubuntu", local.ami_name)) ? "ubuntu" : + can(regex("amzn|amazon", local.ami_name)) ? "ec2-user" : + can(regex("centos", local.ami_name)) ? "centos" : + can(regex("rhel|redhat", local.ami_name)) ? "ec2-user" : + can(regex("debian", local.ami_name)) ? "admin" : + can(regex("fedora", local.ami_name)) ? "fedora" : + can(regex("suse|sles", local.ami_name)) ? "ec2-user" : + can(regex("arch", local.ami_name)) ? "arch" : + "ec2-user" # Safe fallback for most custom/Linux AMIs + ) +} + resource "aws_instance" "ec2_instance" { ami = data.aws_ami.ami.id instance_type = var.aws_instance_type diff --git a/terraform/home-ec2/outputs.tf b/terraform/home-ec2/outputs.tf index 2bcd3d9..32895c4 100644 --- a/terraform/home-ec2/outputs.tf +++ b/terraform/home-ec2/outputs.tf @@ -1,34 +1,14 @@ -output "home_ip" { - description = "IP of your local workstation" - value = data.http.my_public_ip.response_body -} +# output "home_ip" { +# description = "IP of your local workstation" +# value = data.http.my_public_ip.response_body +# } -output "home_sg" { - description = "Reference to the home security group" - value = { - arn = aws_security_group.ssh_restricted.arn - id = aws_security_group.ssh_restricted.id - } -} - -output "ec2_info" { - value = aws_instance.ec2_instance -} - -output "local_os_username" { - value = data.external.local_user.result["user"] -} - -output "focer_ipv4" { - value = data.external.force_ipv4.result -} +# TODO: Bring this back once you add the user on your own. +# output "local_os_username" { +# value = data.external.local_user.result["user"] +# } output "ssh_command" { description = "SSH Command you can run to access the image" - value = "ssh ec2-user@${aws_instance.ec2_instance.public_dns}" -} - -output "router_is_ipv6" { - description = "Is router defaulting to ipv6" - value = local.is_ipv6 + value = "ssh ${local.default_user}@${aws_instance.ec2_instance.public_dns}" } \ No newline at end of file From d861937a5ec77442e226b2be2c929130744bb28c Mon Sep 17 00:00:00 2001 From: Marcus Wyche Date: Sun, 26 Jul 2026 07:59:20 -0400 Subject: [PATCH 7/8] More readme.md fixes --- terraform/home-ec2/README.md | 23 +++++++++++++++++++++++ terraform/home-ec2/variables.tf | 15 --------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/terraform/home-ec2/README.md b/terraform/home-ec2/README.md index 0d48663..2d73017 100644 --- a/terraform/home-ec2/README.md +++ b/terraform/home-ec2/README.md @@ -2,6 +2,29 @@ Simple Module to create an ec2 instance for you to login to from your home network and only you can access. This can be used for tunneling to various resources in your VPC from your local box. +## Variables + +Here is the Markdown table documenting your Terraform variables: + +| Variable Name | Description | Type | Default Value | Required? | +| --- | --- | --- | --- | --- | +| `aws_profile_name` | Name of your local aws profile | `string` | *None* | **Yes** | +| `aws_key_pair_name` | Name of your AWS key pair that will be used for ssh | `string` | *None* | **Yes** | +| `aws_ami_image_id` | image id for the ami. | `string` | *None* | **Yes** | +| `aws_instance_type` | Size of the image to stand up | `string` | `"t3a.nano"` | No | +| `aws_additional_security_groups` | Additional security groups on top of the ssh only one created by module | `list(string)` | `[]` | No | +| `aws_instance_startup_script` | Startup script for instance. | `string` | `null` | No | +| `aws_subnet_id` | id for the subnet | `string` | `null` | No | +| `aws_ec2_instance_name` | Name for the EC2 Instance | `string` | `"home-ec2"` | No | + +## Outputs + +Here is the Markdown table for your output variable: + +| Output Name | Description | Type | +| --- | --- | --- | +| `ssh_command` | SSH Command you can run to access the image | `string` | + ## Commands ### Create the EC2 diff --git a/terraform/home-ec2/variables.tf b/terraform/home-ec2/variables.tf index 6ec6473..9c8179b 100644 --- a/terraform/home-ec2/variables.tf +++ b/terraform/home-ec2/variables.tf @@ -8,18 +8,9 @@ variable "aws_key_pair_name" { type = string } -# TODO: Make this work... -# variable "aws_ami_name_filter" { -# description = "Filter for finding the hvm ami" -# type = list(string) -# default = ["ubuntu/images/hvm-ssd/ubuntu-resolute-26.04-amd64-server-*"] -# } - variable "aws_ami_image_id" { description = "image id for the ami." type = string - default = "ami-01edba92f9036f76e" - # default = "ami-0b6d9d3d33ba97d99" } variable "aws_instance_type" { @@ -40,12 +31,6 @@ variable "aws_instance_startup_script" { default = null } -variable "aws_vpc_id" { - description = "Id of your vpc" - type = string - default = null -} - variable "aws_subnet_id" { description = "id for the subnet" type = string From 15ed577cdd47fc00481cda7f03c26b2529b89083 Mon Sep 17 00:00:00 2001 From: Marcus Wyche Date: Sun, 26 Jul 2026 08:01:31 -0400 Subject: [PATCH 8/8] Some more minor cleanup of the docs --- terraform/home-ec2/README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/terraform/home-ec2/README.md b/terraform/home-ec2/README.md index 2d73017..9e73e6d 100644 --- a/terraform/home-ec2/README.md +++ b/terraform/home-ec2/README.md @@ -4,8 +4,6 @@ Simple Module to create an ec2 instance for you to login to from your home netwo ## Variables -Here is the Markdown table documenting your Terraform variables: - | Variable Name | Description | Type | Default Value | Required? | | --- | --- | --- | --- | --- | | `aws_profile_name` | Name of your local aws profile | `string` | *None* | **Yes** | @@ -19,8 +17,6 @@ Here is the Markdown table documenting your Terraform variables: ## Outputs -Here is the Markdown table for your output variable: - | Output Name | Description | Type | | --- | --- | --- | | `ssh_command` | SSH Command you can run to access the image | `string` |