Skip to content
1 change: 1 addition & 0 deletions src/ON.Installer/InstallerApp/DeployWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> lines = new();
Expand Down
50 changes: 50 additions & 0 deletions src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/Runner.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>();
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);
}
}
}
167 changes: 167 additions & 0 deletions src/ON.Installer/InstallerApp/Terraform/CreateServer/AWS/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions src/ON.Installer/InstallerApp/Terraform/ResourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down