From 81a69b0f0917ae48ec06404b8f204cba0e7dbd6d Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Mon, 28 Mar 2022 23:20:24 -0600 Subject: [PATCH 01/11] AWS Terraform File Terraform file for AWS, user must create Secret Access Key information from their AWS Console before. Runner class almost ready. --- .../Terraform/CreateServer/AWS/main.tf | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf new file mode 100644 index 00000000..ebffbb23 --- /dev/null +++ b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf @@ -0,0 +1,64 @@ +variable prefix {} +variable location {} +variable username {} +variable sshPub {} + +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + profile = "default" + region = var.location +} + +resource "aws_instance" "vm1" { + ami = "ami-830c94e3" + instance_type = "t2.micro" + key_name = aws_key_pair.deployer.key_name + security_groups = ["cw-blog-3-sg-using-terraform"] + + tags = { + Name = "${var.prefix}-ServerInstance" + } +} + +resource "aws_key_pair" "deployer" { + key_name = "${var.prefix}-deployer-key" + public_key = var.sshPub + #public_key = "ssh-rsa DUMMY/KEYy1yc2EAAAADAQABAAABAQD3F6tyEXAMPLEyX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com" +} + +resource "aws_security_group" "onf_sg_ssh" { + name = "${var.prefix}-security-group" + + #Incoming traffic + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["11.xx.xx.xx/32"] + } + + #Outgoing traffic + egress { + from_port = 0 + protocol = "-1" + to_port = 0 + cidr_blocks = ["0.0.0.0/0"] + } +} \ No newline at end of file From b141fefd3942f53f79f0e6f3f6385759bc441e52 Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Mon, 28 Mar 2022 23:36:27 -0600 Subject: [PATCH 02/11] Update main.tf --- .../InstallerApp/Terraform/CreateServer/AWS/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf index ebffbb23..561ef6d2 100644 --- a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf +++ b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf @@ -23,7 +23,7 @@ resource "aws_instance" "vm1" { ami = "ami-830c94e3" instance_type = "t2.micro" key_name = aws_key_pair.deployer.key_name - security_groups = ["cw-blog-3-sg-using-terraform"] + security_groups = ["onf_sg_ssh"] tags = { Name = "${var.prefix}-ServerInstance" @@ -61,4 +61,4 @@ resource "aws_security_group" "onf_sg_ssh" { to_port = 0 cidr_blocks = ["0.0.0.0/0"] } -} \ No newline at end of file +} From 362f9974fb485c264cd7f83a1600041f47ee89df Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Mon, 28 Mar 2022 23:41:03 -0600 Subject: [PATCH 03/11] Update main.tf --- .../InstallerApp/Terraform/CreateServer/AWS/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf index 561ef6d2..bbc04ade 100644 --- a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf +++ b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf @@ -23,7 +23,7 @@ resource "aws_instance" "vm1" { ami = "ami-830c94e3" instance_type = "t2.micro" key_name = aws_key_pair.deployer.key_name - security_groups = ["onf_sg_ssh"] + security_groups = ["${var.prefix}-security-group"] tags = { Name = "${var.prefix}-ServerInstance" @@ -37,7 +37,7 @@ resource "aws_key_pair" "deployer" { } resource "aws_security_group" "onf_sg_ssh" { - name = "${var.prefix}-security-group" + name = "onf-security-group" #Incoming traffic ingress { From 27caf9198dd14aedf956b6011cbca0d6c8e370e7 Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Wed, 30 Mar 2022 13:35:19 -0600 Subject: [PATCH 04/11] AWS Runner Class for CreateServer --- .../Terraform/CreateServer/AWS/Runner.cs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs new file mode 100644 index 00000000..a35623ce --- /dev/null +++ b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InstallerApp.Terraform.CreateServer.AWS +{ + internal class Runner + { + internal static async Task CreateServerAWS(DeployWindow window) + { + await window.AddLine("--- Create Server ---"); + + var targetD = new DirectoryInfo($"{window.DeployRootD.FullName}/createServer/aws"); + var terraD = new DirectoryInfo(targetD.FullName + "/.terraform"); + var varF = new FileInfo(targetD.FullName + "/variables.tf"); + + if (!targetD.Exists) + { + targetD.Create(); + await window.resHelper.SaveCreateAWS(targetD); + } + //Create ssh key to apply to server + var ssh = Security.SshHelper.CreateRSAKey("temp@onf"); + //Environment variables with the server information to create + var envVars = new Dictionary(); + envVars["prefix"] = "onf-" + window.MyModel.DNS.Name.Replace(".", "-"); + envVars["location"] = "us-east-2"; + envVars["username"] = "onfadmin"; + envVars["sshPub"] = ssh.pubKey; + // + if (!terraD.Exists) + { + await window.terraformHelper.RunTerraform(targetD, "init", envVars); + } + + await window.terraformHelper.RunTerraform(targetD, "apply -auto-approve", envVars); + await window.terraformHelper.RunTerraform(targetD, "refresh", envVars); + + var addyLine = (await File.ReadAllLinesAsync(targetD.FullName + "/terraform.tfstate")).FirstOrDefault(l => l.Contains("\"public_ip\"")); + var addy = addyLine.GetBetween(": \"", "\""); + window.MyModel.Server.IP = addy; + window.MyModel.Server.User = "onfadmin"; + + await Terraform.ChangeSsh.Runner.ChangeSshKey(window, ssh.privKey); + } + } +} From 39c148ca2701e3ef32d3e422faa9d4990e7ac4be Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Wed, 30 Mar 2022 13:36:16 -0600 Subject: [PATCH 05/11] Delete main.tf --- .../Terraform/CreateServer/AWS/main.tf | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf deleted file mode 100644 index bbc04ade..00000000 --- a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf +++ /dev/null @@ -1,64 +0,0 @@ -variable prefix {} -variable location {} -variable username {} -variable sshPub {} - -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 3.27" - } - } - - required_version = ">= 0.14.9" -} - -provider "aws" { - profile = "default" - region = var.location -} - -resource "aws_instance" "vm1" { - ami = "ami-830c94e3" - instance_type = "t2.micro" - key_name = aws_key_pair.deployer.key_name - security_groups = ["${var.prefix}-security-group"] - - tags = { - Name = "${var.prefix}-ServerInstance" - } -} - -resource "aws_key_pair" "deployer" { - key_name = "${var.prefix}-deployer-key" - public_key = var.sshPub - #public_key = "ssh-rsa DUMMY/KEYy1yc2EAAAADAQABAAABAQD3F6tyEXAMPLEyX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com" -} - -resource "aws_security_group" "onf_sg_ssh" { - name = "onf-security-group" - - #Incoming traffic - ingress { - from_port = 80 - to_port = 80 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["11.xx.xx.xx/32"] - } - - #Outgoing traffic - egress { - from_port = 0 - protocol = "-1" - to_port = 0 - cidr_blocks = ["0.0.0.0/0"] - } -} From 9d97dc4468f09e01dfc026782d4360b1aabf4801 Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Wed, 30 Mar 2022 13:37:25 -0600 Subject: [PATCH 06/11] .tf file for AWS CreateServer --- .../Terraform/CreateServer/AWS/main.tf | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf new file mode 100644 index 00000000..76aac120 --- /dev/null +++ b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf @@ -0,0 +1,169 @@ +variable prefix {} +variable location {} +variable username {} +variable sshPub {} + +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + profile = "default" + region = var.location + #access_key = AWS Generated Access Key + #secret_key = AWS Generated Secret Key +} + +resource "aws_instance" "vm1" { + ami = "ami-03a0c45ebc70f98ea" + instance_type = "t2.micro" + key_name = aws_key_pair.deployer.key_name + #security_groups = ["${var.prefix}-security-group"] + network_interface { + network_interface_id = aws_network_interface.nic1.id + device_index = 0 + delete_on_termination = false + } + tags = { + Name = "${var.prefix}-vm-1" + } +} + +resource "aws_key_pair" "deployer" { + key_name = "${var.prefix}-deployer-key" + public_key = var.sshPub + #public_key = "ssh-rsa DUMMY/KEYy1yc2EAAAADAQABAAABAQD3F6tyEXAMPLEyX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com" +} + +resource "aws_vpc" "vpc1" { + cidr_block = "10.20.20.0/25" + tags = { + "Name" = "${var.prefix}-vpc-1" + } +} + +resource "aws_vpc_ipv4_cidr_block_association" "secondary_cidr" { + vpc_id = aws_vpc.vpc1.id + cidr_block = "172.2.0.0/16" +} + +resource "aws_subnet" "in_secondary_cidr" { + vpc_id = aws_vpc_ipv4_cidr_block_association.secondary_cidr.vpc_id + cidr_block = "172.2.0.0/24" +} + +resource "aws_subnet" "public" { + vpc_id = aws_vpc.vpc1.id + cidr_block = "10.20.20.64/26" + availability_zone = "${var.location}b" + tags = { + "Name" = "${var.prefix}-public-1" + } +} + +resource "aws_route_table" "vpc1-rt" { + vpc_id = aws_vpc.vpc1.id + tags = { + "Name" = "${var.prefix}-route-table-1" + } +} + +resource "aws_route_table_association" "public" { + subnet_id = aws_subnet.public.id + route_table_id = aws_route_table.vpc1-rt.id +} + +resource "aws_internet_gateway" "vpc1-igw" { + vpc_id = aws_vpc.vpc1.id + tags = { + "Name" = "${var.prefix}-gateway-1" + } +} + +resource "aws_route" "internet-route" { + destination_cidr_block = "0.0.0.0/0" + route_table_id = aws_route_table.vpc1-rt.id + gateway_id = aws_internet_gateway.vpc1-igw.id +} + +resource "aws_network_interface" "nic1" { + subnet_id = aws_subnet.public.id + private_ips = ["10.20.20.120"] + security_groups = [aws_security_group.sg_ssh.id] + tags = { + "Name" = "${var.prefix}-nic-1" + } +} + +resource "aws_eip" "ip-one" { + vpc = true + network_interface = aws_network_interface.nic1.id + tags = { + "Name" = "${var.prefix}-ip-1" + } +} + +resource "aws_security_group" "sg_ssh" { + name = "${var.prefix}-security-group" + description = "allow inbound traffic" + vpc_id = aws_vpc.vpc1.id + + #Incoming traffic + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + #cidr_blocks = ["11.xx.xx.xx/32"] + } + + ingress { + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 8 + to_port = 0 + protocol = "icmp" + cidr_blocks = ["0.0.0.0/0"] + } + + #Outgoing traffic + egress { + from_port = 0 + protocol = "-1" + to_port = 0 + cidr_blocks = ["0.0.0.0/0"] + } +} + +resource "aws_subnet" "private" { + vpc_id = aws_vpc.vpc1.id + cidr_block = "10.20.20.0/26" + availability_zone = "us-east-2a" + tags = { + "Name" = "${var.prefix}-private-1" + } +} + +resource "aws_route_table_association" "private" { + subnet_id = aws_subnet.private.id + route_table_id = aws_route_table.vpc1-rt.id +} \ No newline at end of file From 1d901c0b0eb86e706fd238f1ca6a96437eab030f Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Wed, 30 Mar 2022 19:03:52 -0600 Subject: [PATCH 07/11] For now, availability zone used is us-east-2b Line 160 to aws region as prefix for 'b', to display us-east-2b (or eu-west-2b for example) --- .../InstallerApp/Terraform/CreateServer/AWS/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf index 76aac120..ad7a8cd0 100644 --- a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf +++ b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf @@ -157,7 +157,7 @@ resource "aws_security_group" "sg_ssh" { resource "aws_subnet" "private" { vpc_id = aws_vpc.vpc1.id cidr_block = "10.20.20.0/26" - availability_zone = "us-east-2a" + availability_zone = "${var.location}b" tags = { "Name" = "${var.prefix}-private-1" } @@ -166,4 +166,4 @@ resource "aws_subnet" "private" { resource "aws_route_table_association" "private" { subnet_id = aws_subnet.private.id route_table_id = aws_route_table.vpc1-rt.id -} \ No newline at end of file +} From 2f6ac6805184f593a7520f141395144174d75357 Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:43:15 -0500 Subject: [PATCH 08/11] Delete src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS directory --- .../Terraform/CreateServer/AWS/Runner.cs | 50 ------ .../Terraform/CreateServer/AWS/main.tf | 169 ------------------ 2 files changed, 219 deletions(-) delete mode 100644 src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs delete mode 100644 src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs deleted file mode 100644 index a35623ce..00000000 --- a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace InstallerApp.Terraform.CreateServer.AWS -{ - internal class Runner - { - internal static async Task CreateServerAWS(DeployWindow window) - { - await window.AddLine("--- Create Server ---"); - - var targetD = new DirectoryInfo($"{window.DeployRootD.FullName}/createServer/aws"); - var terraD = new DirectoryInfo(targetD.FullName + "/.terraform"); - var varF = new FileInfo(targetD.FullName + "/variables.tf"); - - if (!targetD.Exists) - { - targetD.Create(); - await window.resHelper.SaveCreateAWS(targetD); - } - //Create ssh key to apply to server - var ssh = Security.SshHelper.CreateRSAKey("temp@onf"); - //Environment variables with the server information to create - var envVars = new Dictionary(); - envVars["prefix"] = "onf-" + window.MyModel.DNS.Name.Replace(".", "-"); - envVars["location"] = "us-east-2"; - envVars["username"] = "onfadmin"; - envVars["sshPub"] = ssh.pubKey; - // - if (!terraD.Exists) - { - await window.terraformHelper.RunTerraform(targetD, "init", envVars); - } - - await window.terraformHelper.RunTerraform(targetD, "apply -auto-approve", envVars); - await window.terraformHelper.RunTerraform(targetD, "refresh", envVars); - - var addyLine = (await File.ReadAllLinesAsync(targetD.FullName + "/terraform.tfstate")).FirstOrDefault(l => l.Contains("\"public_ip\"")); - var addy = addyLine.GetBetween(": \"", "\""); - window.MyModel.Server.IP = addy; - window.MyModel.Server.User = "onfadmin"; - - await Terraform.ChangeSsh.Runner.ChangeSshKey(window, ssh.privKey); - } - } -} diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf deleted file mode 100644 index ad7a8cd0..00000000 --- a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf +++ /dev/null @@ -1,169 +0,0 @@ -variable prefix {} -variable location {} -variable username {} -variable sshPub {} - -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 3.27" - } - } - - required_version = ">= 0.14.9" -} - -provider "aws" { - profile = "default" - region = var.location - #access_key = AWS Generated Access Key - #secret_key = AWS Generated Secret Key -} - -resource "aws_instance" "vm1" { - ami = "ami-03a0c45ebc70f98ea" - instance_type = "t2.micro" - key_name = aws_key_pair.deployer.key_name - #security_groups = ["${var.prefix}-security-group"] - network_interface { - network_interface_id = aws_network_interface.nic1.id - device_index = 0 - delete_on_termination = false - } - tags = { - Name = "${var.prefix}-vm-1" - } -} - -resource "aws_key_pair" "deployer" { - key_name = "${var.prefix}-deployer-key" - public_key = var.sshPub - #public_key = "ssh-rsa DUMMY/KEYy1yc2EAAAADAQABAAABAQD3F6tyEXAMPLEyX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com" -} - -resource "aws_vpc" "vpc1" { - cidr_block = "10.20.20.0/25" - tags = { - "Name" = "${var.prefix}-vpc-1" - } -} - -resource "aws_vpc_ipv4_cidr_block_association" "secondary_cidr" { - vpc_id = aws_vpc.vpc1.id - cidr_block = "172.2.0.0/16" -} - -resource "aws_subnet" "in_secondary_cidr" { - vpc_id = aws_vpc_ipv4_cidr_block_association.secondary_cidr.vpc_id - cidr_block = "172.2.0.0/24" -} - -resource "aws_subnet" "public" { - vpc_id = aws_vpc.vpc1.id - cidr_block = "10.20.20.64/26" - availability_zone = "${var.location}b" - tags = { - "Name" = "${var.prefix}-public-1" - } -} - -resource "aws_route_table" "vpc1-rt" { - vpc_id = aws_vpc.vpc1.id - tags = { - "Name" = "${var.prefix}-route-table-1" - } -} - -resource "aws_route_table_association" "public" { - subnet_id = aws_subnet.public.id - route_table_id = aws_route_table.vpc1-rt.id -} - -resource "aws_internet_gateway" "vpc1-igw" { - vpc_id = aws_vpc.vpc1.id - tags = { - "Name" = "${var.prefix}-gateway-1" - } -} - -resource "aws_route" "internet-route" { - destination_cidr_block = "0.0.0.0/0" - route_table_id = aws_route_table.vpc1-rt.id - gateway_id = aws_internet_gateway.vpc1-igw.id -} - -resource "aws_network_interface" "nic1" { - subnet_id = aws_subnet.public.id - private_ips = ["10.20.20.120"] - security_groups = [aws_security_group.sg_ssh.id] - tags = { - "Name" = "${var.prefix}-nic-1" - } -} - -resource "aws_eip" "ip-one" { - vpc = true - network_interface = aws_network_interface.nic1.id - tags = { - "Name" = "${var.prefix}-ip-1" - } -} - -resource "aws_security_group" "sg_ssh" { - name = "${var.prefix}-security-group" - description = "allow inbound traffic" - vpc_id = aws_vpc.vpc1.id - - #Incoming traffic - ingress { - from_port = 80 - to_port = 80 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - #cidr_blocks = ["11.xx.xx.xx/32"] - } - - ingress { - from_port = 443 - to_port = 443 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8 - to_port = 0 - protocol = "icmp" - cidr_blocks = ["0.0.0.0/0"] - } - - #Outgoing traffic - egress { - from_port = 0 - protocol = "-1" - to_port = 0 - cidr_blocks = ["0.0.0.0/0"] - } -} - -resource "aws_subnet" "private" { - vpc_id = aws_vpc.vpc1.id - cidr_block = "10.20.20.0/26" - availability_zone = "${var.location}b" - tags = { - "Name" = "${var.prefix}-private-1" - } -} - -resource "aws_route_table_association" "private" { - subnet_id = aws_subnet.private.id - route_table_id = aws_route_table.vpc1-rt.id -} From 3781496f4b95d31f0ae4d87298b890f505819f1e Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:44:45 -0500 Subject: [PATCH 09/11] AWS Terraform file and Runner Class --- .../Terraform/CreateServer/AWS/Runner.cs | 50 ++++++ .../Terraform/CreateServer/AWS/main.tf | 167 ++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs create mode 100644 src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs new file mode 100644 index 00000000..4098689d --- /dev/null +++ b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InstallerApp.Terraform.CreateServer.AWS +{ + internal class Runner + { + internal static async Task CreateServerAWS(DeployWindow window) + { + await window.AddLine("--- Create Server ---"); + + var targetD = new DirectoryInfo($"{window.DeployRootD.FullName}/createServer/aws"); + var terraD = new DirectoryInfo(targetD.FullName + "/.terraform"); + var varF = new FileInfo(targetD.FullName + "/variables.tf"); + + if (!targetD.Exists) + { + targetD.Create(); + await window.resHelper.SaveCreateAWS(targetD); + } + //Create ssh key to apply to server + var ssh = Security.SshHelper.CreateRSAKey("temp@onf"); + //Environment variables with the server information to create + var envVars = new Dictionary(); + envVars["prefix"] = "onf-" + window.MyModel.DNS.Name.Replace(".", "-"); + envVars["location"] = "us-east-2"; + envVars["username"] = "ubuntu"; + envVars["sshPub"] = ssh.pubKey; + // + if (!terraD.Exists) + { + await window.terraformHelper.RunTerraform(targetD, "init", envVars); + } + + await window.terraformHelper.RunTerraform(targetD, "apply -auto-approve", envVars); + await window.terraformHelper.RunTerraform(targetD, "refresh", envVars); + + var addyLine = (await File.ReadAllLinesAsync(targetD.FullName + "/terraform.tfstate")).FirstOrDefault(l => l.Contains("\"public_ip\"")); + var addy = addyLine.GetBetween(": \"", "\""); + window.MyModel.Server.IP = addy; + window.MyModel.Server.User = "ubuntu"; + + await Terraform.ChangeSsh.Runner.ChangeSshKey(window, ssh.privKey); + } + } +} diff --git a/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf new file mode 100644 index 00000000..e28a8f21 --- /dev/null +++ b/src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf @@ -0,0 +1,167 @@ +variable prefix {} +variable location {} +variable username {} +variable sshPub {} + +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + profile = "default" + region = var.location + #access_key = AWS Generated Access Key + #secret_key = AWS Generated Secret Key +} + +resource "aws_instance" "vm1" { + ami = "ami-03a0c45ebc70f98ea" + instance_type = "t2.micro" + key_name = aws_key_pair.deployer.key_name + network_interface { + network_interface_id = aws_network_interface.nic1.id + device_index = 0 + delete_on_termination = false + } + tags = { + Name = "${var.prefix}-vm-1" + } +} + +resource "aws_key_pair" "deployer" { + key_name = "${var.prefix}-deployer-key" + public_key = var.sshPub +} + +resource "aws_vpc" "vpc1" { + cidr_block = "10.20.20.0/25" + tags = { + "Name" = "${var.prefix}-vpc-1" + } +} + +resource "aws_vpc_ipv4_cidr_block_association" "secondary_cidr" { + vpc_id = aws_vpc.vpc1.id + cidr_block = "172.2.0.0/16" +} + +resource "aws_subnet" "in_secondary_cidr" { + vpc_id = aws_vpc_ipv4_cidr_block_association.secondary_cidr.vpc_id + cidr_block = "172.2.0.0/24" +} + +resource "aws_subnet" "public" { + vpc_id = aws_vpc.vpc1.id + cidr_block = "10.20.20.64/26" + availability_zone = "${var.location}b" + tags = { + "Name" = "${var.prefix}-public-1" + } +} + +resource "aws_route_table" "vpc1-rt" { + vpc_id = aws_vpc.vpc1.id + tags = { + "Name" = "${var.prefix}-route-table-1" + } +} + +resource "aws_route_table_association" "public" { + subnet_id = aws_subnet.public.id + route_table_id = aws_route_table.vpc1-rt.id +} + +resource "aws_internet_gateway" "vpc1-igw" { + vpc_id = aws_vpc.vpc1.id + tags = { + "Name" = "${var.prefix}-gateway-1" + } +} + +resource "aws_route" "internet-route" { + destination_cidr_block = "0.0.0.0/0" + route_table_id = aws_route_table.vpc1-rt.id + gateway_id = aws_internet_gateway.vpc1-igw.id +} + +resource "aws_network_interface" "nic1" { + subnet_id = aws_subnet.public.id + private_ips = ["10.20.20.120"] + security_groups = [aws_security_group.sg_ssh.id] + tags = { + "Name" = "${var.prefix}-nic-1" + } +} + +resource "aws_eip" "ip-one" { + vpc = true + network_interface = aws_network_interface.nic1.id + tags = { + "Name" = "${var.prefix}-ip-1" + } +} + +resource "aws_security_group" "sg_ssh" { + name = "${var.prefix}-security-group" + description = "allow inbound traffic" + vpc_id = aws_vpc.vpc1.id + + #Incoming traffic + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + #cidr_blocks = ["11.xx.xx.xx/32"] + } + + ingress { + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 8 + to_port = 0 + protocol = "icmp" + cidr_blocks = ["0.0.0.0/0"] + } + + #Outgoing traffic + egress { + from_port = 0 + protocol = "-1" + to_port = 0 + cidr_blocks = ["0.0.0.0/0"] + } +} + +resource "aws_subnet" "private" { + vpc_id = aws_vpc.vpc1.id + cidr_block = "10.20.20.0/26" + availability_zone = "${var.location}b" + tags = { + "Name" = "${var.prefix}-private-1" + } +} + +resource "aws_route_table_association" "private" { + subnet_id = aws_subnet.private.id + route_table_id = aws_route_table.vpc1-rt.id +} \ No newline at end of file From 83005ffb6b01b9a95b9233abb7f9dccb3f5e21f4 Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:46:17 -0500 Subject: [PATCH 10/11] AWS - Save/Create Function --- src/ON.Installer/InstallerApp/Terraform/ResourceHelper.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ON.Installer/InstallerApp/Terraform/ResourceHelper.cs b/src/ON.Installer/InstallerApp/Terraform/ResourceHelper.cs index 0493b49b..8ff18ae8 100644 --- a/src/ON.Installer/InstallerApp/Terraform/ResourceHelper.cs +++ b/src/ON.Installer/InstallerApp/Terraform/ResourceHelper.cs @@ -33,6 +33,11 @@ public async Task SaveCreateDigitalocean(DirectoryInfo dir) { await Save("InstallerApp.Terraform.CreateServer.Digitalocean.", dir); } + + public async Task SaveCreateAWS(DirectoryInfo dir) + { + await Save("InstallerApp.Terraform.CreateServer.AWS.", dir); + } public async Task SaveDeploySite(DirectoryInfo dir) { From be785a04f6eda57096c8b32913ddc9a94617ec32 Mon Sep 17 00:00:00 2001 From: Jose <40406513+dawiseguy55@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:48:24 -0500 Subject: [PATCH 11/11] AWS - Added call to Runner --- src/ON.Installer/InstallerApp/DeployWindow.xaml.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ON.Installer/InstallerApp/DeployWindow.xaml.cs b/src/ON.Installer/InstallerApp/DeployWindow.xaml.cs index 9321c026..4f13c4de 100644 --- a/src/ON.Installer/InstallerApp/DeployWindow.xaml.cs +++ b/src/ON.Installer/InstallerApp/DeployWindow.xaml.cs @@ -89,6 +89,7 @@ internal async Task CreateServer() { //await Terraform.CreateServer.Azure.Runner.CreateServerAzure(this); await Terraform.CreateServer.Digitalocean.Runner.CreateServeDigitalOcean(this); + //await Terraform.CreateServer.AWS.Runner.CreateServerAWS(this); } List lines = new();