From 37bf5a6f8322b6f1a7c4b31cec197a51306d9bae Mon Sep 17 00:00:00 2001 From: DaMandal0rian <3614052+DaMandal0rian@users.noreply.github.com> Date: Tue, 19 Mar 2024 14:03:02 +0000 Subject: [PATCH] Rename folders (#281) * rename aws folder to resources * update readme.md folder name --- {aws => resources}/README.md | 10 +- {aws => resources}/devnet/backend.tf | 0 {aws => resources}/devnet/common.tf | 0 {aws => resources}/devnet/main.tf | 0 {aws => resources}/devnet/outputs.tf | 0 .../devnet/terrafrom.tfvars.example | 0 {aws => resources}/devnet/variables.tf | 0 {aws => resources}/gemini-3f/backend.tf | 0 {aws => resources}/gemini-3f/common.tf | 0 {aws => resources}/gemini-3f/main.tf | 0 {aws => resources}/gemini-3f/outputs.tf | 0 .../gemini-3f/terrafrom.tfvars.example | 0 {aws => resources}/gemini-3f/variables.tf | 0 {aws => resources}/gemini-3g/backend.tf | 0 {aws => resources}/gemini-3g/common.tf | 0 {aws => resources}/gemini-3g/main.tf | 0 {aws => resources}/gemini-3g/outputs.tf | 0 .../gemini-3g/terrafrom.tfvars.example | 0 {aws => resources}/gemini-3g/variables.tf | 0 {aws => resources}/gemini-3h/backend.tf | 0 {aws => resources}/gemini-3h/common.tf | 0 {aws => resources}/gemini-3h/main.tf | 0 {aws => resources}/gemini-3h/outputs.tf | 0 .../gemini-3h/terrafrom.tfvars.example | 0 {aws => resources}/gemini-3h/variables.tf | 0 {aws => resources}/telemetry/backend.tf | 0 {aws => resources}/telemetry/dns.tf | 0 resources/telemetry/ec2/base/ami.tf | 26 +++++ resources/telemetry/ec2/base/backend.tf | 9 ++ resources/telemetry/ec2/base/dns.tf | 11 ++ resources/telemetry/ec2/base/instances.tf | 49 ++++++++ .../telemetry/ec2/base}/network.tf | 0 resources/telemetry/ec2/base/outputs.tf | 23 ++++ .../telemetry/ec2/base}/provider.tf | 0 .../telemetry/ec2/base}/provision.tf | 0 .../telemetry/ec2/base/scripts/installer.sh | 27 +++++ .../telemetry/ec2/base}/variables.tf | 0 .../telemetry/ec2/modules/telemetry/main.tf | 24 ++++ .../ec2/modules/telemetry/outputs.tf | 9 ++ .../telemetry/terraform.tfvars.example | 0 .../ec2/modules/telemetry/variables.tf | 80 +++++++++++++ {aws => resources}/telemetry/main.tf | 0 resources/telemetry/network.tf | 107 +++++++++++++++++ {aws => resources}/telemetry/outputs.tf | 0 resources/telemetry/provider.tf | 31 +++++ resources/telemetry/provision.tf | 64 +++++++++++ resources/telemetry/terraform.tfvars.example | 13 +++ resources/telemetry/variables.tf | 108 ++++++++++++++++++ 48 files changed, 586 insertions(+), 5 deletions(-) rename {aws => resources}/README.md (94%) rename {aws => resources}/devnet/backend.tf (100%) rename {aws => resources}/devnet/common.tf (100%) rename {aws => resources}/devnet/main.tf (100%) rename {aws => resources}/devnet/outputs.tf (100%) rename {aws => resources}/devnet/terrafrom.tfvars.example (100%) rename {aws => resources}/devnet/variables.tf (100%) rename {aws => resources}/gemini-3f/backend.tf (100%) rename {aws => resources}/gemini-3f/common.tf (100%) rename {aws => resources}/gemini-3f/main.tf (100%) rename {aws => resources}/gemini-3f/outputs.tf (100%) rename {aws => resources}/gemini-3f/terrafrom.tfvars.example (100%) rename {aws => resources}/gemini-3f/variables.tf (100%) rename {aws => resources}/gemini-3g/backend.tf (100%) rename {aws => resources}/gemini-3g/common.tf (100%) rename {aws => resources}/gemini-3g/main.tf (100%) rename {aws => resources}/gemini-3g/outputs.tf (100%) rename {aws => resources}/gemini-3g/terrafrom.tfvars.example (100%) rename {aws => resources}/gemini-3g/variables.tf (100%) rename {aws => resources}/gemini-3h/backend.tf (100%) rename {aws => resources}/gemini-3h/common.tf (100%) rename {aws => resources}/gemini-3h/main.tf (100%) rename {aws => resources}/gemini-3h/outputs.tf (100%) rename {aws => resources}/gemini-3h/terrafrom.tfvars.example (100%) rename {aws => resources}/gemini-3h/variables.tf (100%) rename {aws => resources}/telemetry/backend.tf (100%) rename {aws => resources}/telemetry/dns.tf (100%) create mode 100644 resources/telemetry/ec2/base/ami.tf create mode 100644 resources/telemetry/ec2/base/backend.tf create mode 100644 resources/telemetry/ec2/base/dns.tf create mode 100644 resources/telemetry/ec2/base/instances.tf rename {aws/telemetry => resources/telemetry/ec2/base}/network.tf (100%) create mode 100644 resources/telemetry/ec2/base/outputs.tf rename {aws/telemetry => resources/telemetry/ec2/base}/provider.tf (100%) rename {aws/telemetry => resources/telemetry/ec2/base}/provision.tf (100%) create mode 100755 resources/telemetry/ec2/base/scripts/installer.sh rename {aws/telemetry => resources/telemetry/ec2/base}/variables.tf (100%) create mode 100644 resources/telemetry/ec2/modules/telemetry/main.tf create mode 100644 resources/telemetry/ec2/modules/telemetry/outputs.tf rename {aws => resources/telemetry/ec2/modules}/telemetry/terraform.tfvars.example (100%) create mode 100644 resources/telemetry/ec2/modules/telemetry/variables.tf rename {aws => resources}/telemetry/main.tf (100%) create mode 100644 resources/telemetry/network.tf rename {aws => resources}/telemetry/outputs.tf (100%) create mode 100644 resources/telemetry/provider.tf create mode 100644 resources/telemetry/provision.tf create mode 100644 resources/telemetry/terraform.tfvars.example create mode 100644 resources/telemetry/variables.tf diff --git a/aws/README.md b/resources/README.md similarity index 94% rename from aws/README.md rename to resources/README.md index 32ef3e86..2948ab10 100644 --- a/aws/README.md +++ b/resources/README.md @@ -16,9 +16,9 @@ Before using this framework, ensure you have the following installed: We use **Terraform** and **AWS** to provision the infrastructure. -Clone the repository and navigate to the testing framework directory **aws**: +Clone the repository and navigate to the testing framework directory **resources**: -### Terraform AWS Folder Structure: +### Terraform Resources Folder Structure: ``` . @@ -77,7 +77,7 @@ Clone the repository and navigate to the testing framework directory **aws**: ## Getting started. -- Go to **aws//** +- Go to **resources//** - rename the terraform.tfvars.example file inside the child module to terraform.tfvars. - modify the main.tf file if any further changes are needed to customize - Add your personal AWS access and secret in the terraform.tfvars file @@ -88,7 +88,7 @@ Clone the repository and navigate to the testing framework directory **aws**: ## Generate Node keys -Each network will need it's own keys, which you can gather from bitwarden, and extract the zip folder into **aws//**. The files should be named the following. +Each network will need it's own keys, which you can gather from bitwarden, and extract the zip folder into **resources//**. The files should be named the following. ``` . @@ -105,7 +105,7 @@ Each network will need it's own keys, which you can gather from bitwarden, and e ## Deploy resources. -1. Go to **aws//** directory and run the following commands to init terraform: +1. Go to **resources//** directory and run the following commands to init terraform: ``` terraform init diff --git a/aws/devnet/backend.tf b/resources/devnet/backend.tf similarity index 100% rename from aws/devnet/backend.tf rename to resources/devnet/backend.tf diff --git a/aws/devnet/common.tf b/resources/devnet/common.tf similarity index 100% rename from aws/devnet/common.tf rename to resources/devnet/common.tf diff --git a/aws/devnet/main.tf b/resources/devnet/main.tf similarity index 100% rename from aws/devnet/main.tf rename to resources/devnet/main.tf diff --git a/aws/devnet/outputs.tf b/resources/devnet/outputs.tf similarity index 100% rename from aws/devnet/outputs.tf rename to resources/devnet/outputs.tf diff --git a/aws/devnet/terrafrom.tfvars.example b/resources/devnet/terrafrom.tfvars.example similarity index 100% rename from aws/devnet/terrafrom.tfvars.example rename to resources/devnet/terrafrom.tfvars.example diff --git a/aws/devnet/variables.tf b/resources/devnet/variables.tf similarity index 100% rename from aws/devnet/variables.tf rename to resources/devnet/variables.tf diff --git a/aws/gemini-3f/backend.tf b/resources/gemini-3f/backend.tf similarity index 100% rename from aws/gemini-3f/backend.tf rename to resources/gemini-3f/backend.tf diff --git a/aws/gemini-3f/common.tf b/resources/gemini-3f/common.tf similarity index 100% rename from aws/gemini-3f/common.tf rename to resources/gemini-3f/common.tf diff --git a/aws/gemini-3f/main.tf b/resources/gemini-3f/main.tf similarity index 100% rename from aws/gemini-3f/main.tf rename to resources/gemini-3f/main.tf diff --git a/aws/gemini-3f/outputs.tf b/resources/gemini-3f/outputs.tf similarity index 100% rename from aws/gemini-3f/outputs.tf rename to resources/gemini-3f/outputs.tf diff --git a/aws/gemini-3f/terrafrom.tfvars.example b/resources/gemini-3f/terrafrom.tfvars.example similarity index 100% rename from aws/gemini-3f/terrafrom.tfvars.example rename to resources/gemini-3f/terrafrom.tfvars.example diff --git a/aws/gemini-3f/variables.tf b/resources/gemini-3f/variables.tf similarity index 100% rename from aws/gemini-3f/variables.tf rename to resources/gemini-3f/variables.tf diff --git a/aws/gemini-3g/backend.tf b/resources/gemini-3g/backend.tf similarity index 100% rename from aws/gemini-3g/backend.tf rename to resources/gemini-3g/backend.tf diff --git a/aws/gemini-3g/common.tf b/resources/gemini-3g/common.tf similarity index 100% rename from aws/gemini-3g/common.tf rename to resources/gemini-3g/common.tf diff --git a/aws/gemini-3g/main.tf b/resources/gemini-3g/main.tf similarity index 100% rename from aws/gemini-3g/main.tf rename to resources/gemini-3g/main.tf diff --git a/aws/gemini-3g/outputs.tf b/resources/gemini-3g/outputs.tf similarity index 100% rename from aws/gemini-3g/outputs.tf rename to resources/gemini-3g/outputs.tf diff --git a/aws/gemini-3g/terrafrom.tfvars.example b/resources/gemini-3g/terrafrom.tfvars.example similarity index 100% rename from aws/gemini-3g/terrafrom.tfvars.example rename to resources/gemini-3g/terrafrom.tfvars.example diff --git a/aws/gemini-3g/variables.tf b/resources/gemini-3g/variables.tf similarity index 100% rename from aws/gemini-3g/variables.tf rename to resources/gemini-3g/variables.tf diff --git a/aws/gemini-3h/backend.tf b/resources/gemini-3h/backend.tf similarity index 100% rename from aws/gemini-3h/backend.tf rename to resources/gemini-3h/backend.tf diff --git a/aws/gemini-3h/common.tf b/resources/gemini-3h/common.tf similarity index 100% rename from aws/gemini-3h/common.tf rename to resources/gemini-3h/common.tf diff --git a/aws/gemini-3h/main.tf b/resources/gemini-3h/main.tf similarity index 100% rename from aws/gemini-3h/main.tf rename to resources/gemini-3h/main.tf diff --git a/aws/gemini-3h/outputs.tf b/resources/gemini-3h/outputs.tf similarity index 100% rename from aws/gemini-3h/outputs.tf rename to resources/gemini-3h/outputs.tf diff --git a/aws/gemini-3h/terrafrom.tfvars.example b/resources/gemini-3h/terrafrom.tfvars.example similarity index 100% rename from aws/gemini-3h/terrafrom.tfvars.example rename to resources/gemini-3h/terrafrom.tfvars.example diff --git a/aws/gemini-3h/variables.tf b/resources/gemini-3h/variables.tf similarity index 100% rename from aws/gemini-3h/variables.tf rename to resources/gemini-3h/variables.tf diff --git a/aws/telemetry/backend.tf b/resources/telemetry/backend.tf similarity index 100% rename from aws/telemetry/backend.tf rename to resources/telemetry/backend.tf diff --git a/aws/telemetry/dns.tf b/resources/telemetry/dns.tf similarity index 100% rename from aws/telemetry/dns.tf rename to resources/telemetry/dns.tf diff --git a/resources/telemetry/ec2/base/ami.tf b/resources/telemetry/ec2/base/ami.tf new file mode 100644 index 00000000..640e51d9 --- /dev/null +++ b/resources/telemetry/ec2/base/ami.tf @@ -0,0 +1,26 @@ +# Data for AWS module +data "aws_ami" "ubuntu_amd64" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + filter { + name = "architecture" + values = ["x86_64"] + } + + filter { + name = "root-device-type" + values = ["ebs"] + } + + owners = ["099720109477"] +} diff --git a/resources/telemetry/ec2/base/backend.tf b/resources/telemetry/ec2/base/backend.tf new file mode 100644 index 00000000..a6cc8013 --- /dev/null +++ b/resources/telemetry/ec2/base/backend.tf @@ -0,0 +1,9 @@ +terraform { + cloud { + organization = "subspace-sre" + + workspaces { + name = "telemetry-aws" + } + } +} diff --git a/resources/telemetry/ec2/base/dns.tf b/resources/telemetry/ec2/base/dns.tf new file mode 100644 index 00000000..1a72a1ab --- /dev/null +++ b/resources/telemetry/ec2/base/dns.tf @@ -0,0 +1,11 @@ +data "cloudflare_zone" "cloudflare_zone" { + name = "subspace.network" +} + +resource "cloudflare_record" "telemetry_subspace_node" { + zone_id = data.cloudflare_zone.cloudflare_zone.id + name = "${var.domain_prefix}-new" + value = module.telemetry_subspace_node.public_ip + type = "A" + ttl = "3600" +} diff --git a/resources/telemetry/ec2/base/instances.tf b/resources/telemetry/ec2/base/instances.tf new file mode 100644 index 00000000..f264751a --- /dev/null +++ b/resources/telemetry/ec2/base/instances.tf @@ -0,0 +1,49 @@ +resource "aws_instance" "telemetry_subspace_node" { + ami = data.aws_ami.ubuntu_amd64.image_id + instance_type = var.instance_type + subnet_id = aws_subnet.public_subnets.id + availability_zone = var.azs + # Security Group + vpc_security_group_ids = ["${aws_security_group.telemetry-subspace-sg.id}"] + # the Public SSH key + key_name = var.aws_key_name + associate_public_ip_address = true + ebs_optimized = true + ebs_block_device { + device_name = "/dev/sda1" + volume_size = var.telemetry-subspace-node-config.disk-volume-size + volume_type = var.telemetry-subspace-node-config.disk-volume-type + iops = 3000 + throughput = 250 + } + + tags = { + name = "telemetry-subspace-node" + role = "telemetry server" + os_name = "ubuntu" + os_version = "22.04" + arch = "x86_64" + } + + depends_on = [ + aws_subnet.public_subnets, + aws_internet_gateway.gw + ] + + lifecycle { + + create_before_destroy = true + + } + + # Setting up the ssh connection + connection { + type = "ssh" + host = self.public_ip + user = "ubuntu" + agent = true + private_key = file("${var.private_key_path}") + timeout = "90s" + } + +} diff --git a/aws/telemetry/network.tf b/resources/telemetry/ec2/base/network.tf similarity index 100% rename from aws/telemetry/network.tf rename to resources/telemetry/ec2/base/network.tf diff --git a/resources/telemetry/ec2/base/outputs.tf b/resources/telemetry/ec2/base/outputs.tf new file mode 100644 index 00000000..9334b9b4 --- /dev/null +++ b/resources/telemetry/ec2/base/outputs.tf @@ -0,0 +1,23 @@ +// Output Variables + +output "telemetry_subspace_node_server_id" { + value = aws_instance.telemetry_subspace_node.id +} + +output "telemetry_subspace_node_public_ip" { + value = aws_instance.telemetry_subspace_node.public_ip +} + +output "telemetry_subspace_node_private_ip" { + value = aws_instance.telemetry_subspace_node.private_ip +} + +output "telemetry_subspace_node_ami" { + value = aws_instance.telemetry_subspace_node.ami +} + +output "dns-records" { + value = [ + cloudflare_record.telemetry_subspace_node.hostname, + ] +} diff --git a/aws/telemetry/provider.tf b/resources/telemetry/ec2/base/provider.tf similarity index 100% rename from aws/telemetry/provider.tf rename to resources/telemetry/ec2/base/provider.tf diff --git a/aws/telemetry/provision.tf b/resources/telemetry/ec2/base/provision.tf similarity index 100% rename from aws/telemetry/provision.tf rename to resources/telemetry/ec2/base/provision.tf diff --git a/resources/telemetry/ec2/base/scripts/installer.sh b/resources/telemetry/ec2/base/scripts/installer.sh new file mode 100755 index 00000000..02a65287 --- /dev/null +++ b/resources/telemetry/ec2/base/scripts/installer.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +# updates +export DEBIAN_FRONTEND=noninteractive +sudo apt update -y + +#install nginx, certbot and base packages +sudo apt install curl git openssl gnupg --no-install-recommends -y + +# install docker & Docker Compose +sudo install -m 0755 -d /etc/apt/keyrings +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --yes --dearmor -o /etc/apt/keyrings/docker.gpg +sudo chmod a+r /etc/apt/keyrings/docker.gpg + +echo \ + "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ + "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ + sudo tee /etc/apt/sources.list.d/docker.list > /dev/null +sudo apt update -y +sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y + +# set max socket connections +if ! (grep -iq "net.core.somaxconn" /etc/sysctl.conf && sed -i 's/.*net.core.somaxconn.*/net.core.somaxconn=65535/' /etc/sysctl.conf); then + sudo echo "net.core.somaxconn=65535" >> /etc/sysctl.conf +fi + +sudo sysctl -p /etc/sysctl.conf diff --git a/aws/telemetry/variables.tf b/resources/telemetry/ec2/base/variables.tf similarity index 100% rename from aws/telemetry/variables.tf rename to resources/telemetry/ec2/base/variables.tf diff --git a/resources/telemetry/ec2/modules/telemetry/main.tf b/resources/telemetry/ec2/modules/telemetry/main.tf new file mode 100644 index 00000000..bd029e58 --- /dev/null +++ b/resources/telemetry/ec2/modules/telemetry/main.tf @@ -0,0 +1,24 @@ +module "telemetry" { + source = "../../base/" + path_to_scripts = "../../base/scripts" + path_to_configs = "../../base/config" + + telemetry-subspace-node-config = { + domain-prefix = "telemetry" + instance-type = var.instance_type + deployment-version = 1 + regions = var.aws_region + instance-count = var.instance_count + disk-volume-size = var.disk_volume_size + disk-volume-type = var.disk_volume_type + } + + cloudflare_api_token = var.cloudflare_api_token + cloudflare_email = var.cloudflare_email + access_key = var.access_key + secret_key = var.secret_key + vpc_id = var.vpc_id + instance_type = var.instance_type + vpc_cidr_block = var.vpc_cidr_block + public_subnet_cidrs = var.public_subnet_cidrs +} diff --git a/resources/telemetry/ec2/modules/telemetry/outputs.tf b/resources/telemetry/ec2/modules/telemetry/outputs.tf new file mode 100644 index 00000000..fb2ecda3 --- /dev/null +++ b/resources/telemetry/ec2/modules/telemetry/outputs.tf @@ -0,0 +1,9 @@ +output "telemetry_subspace_node_ipv4_addresses" { + value = module.telemetry.*.telemetry_subspace_node_public_ip + description = "telemetry node IPv4 Addresses" +} + +output "dns-records" { + value = module.telemetry.*.dns-records + description = "DNS records" +} diff --git a/aws/telemetry/terraform.tfvars.example b/resources/telemetry/ec2/modules/telemetry/terraform.tfvars.example similarity index 100% rename from aws/telemetry/terraform.tfvars.example rename to resources/telemetry/ec2/modules/telemetry/terraform.tfvars.example diff --git a/resources/telemetry/ec2/modules/telemetry/variables.tf b/resources/telemetry/ec2/modules/telemetry/variables.tf new file mode 100644 index 00000000..b68ad019 --- /dev/null +++ b/resources/telemetry/ec2/modules/telemetry/variables.tf @@ -0,0 +1,80 @@ +variable "instance_type" { + default = "m6a.4xlarge" + type = string +} + +variable "vpc_id" { + default = "telemetry-vpc" + type = string +} + +variable "vpc_cidr_block" { + type = string +} + +variable "azs" { + type = string + description = "Availability Zones" + default = "us-west-2c" +} + +variable "instance_count" { + type = number + default = 1 +} + +variable "aws_region" { + description = "aws region" + type = string + default = "us-west-2" +} + +variable "public_subnet_cidrs" { + type = string + description = "Public Subnet CIDR values" + default = "172.31.1.0/24" +} + +variable "disk_volume_size" { + type = number +} + +variable "disk_volume_type" { + type = string + default = "gp3" +} + +variable "secret_key" { + type = string + sensitive = true +} + +variable "access_key" { + type = string + sensitive = true +} + +variable "aws_key_name" { + default = "deployer" + type = string +} + +variable "ssh_user" { + default = "ubuntu" + type = string +} + +variable "private_key_path" { + type = string + default = "~/.ssh/deployer.pem" +} + +variable "cloudflare_email" { + type = string + description = "cloudflare email address" +} + +variable "cloudflare_api_token" { + type = string + description = "cloudflare api token" +} diff --git a/aws/telemetry/main.tf b/resources/telemetry/main.tf similarity index 100% rename from aws/telemetry/main.tf rename to resources/telemetry/main.tf diff --git a/resources/telemetry/network.tf b/resources/telemetry/network.tf new file mode 100644 index 00000000..b5ea0ad6 --- /dev/null +++ b/resources/telemetry/network.tf @@ -0,0 +1,107 @@ +module "telemetry_vpc" { + source = "../../terraform/aws/vpc" + + cidr_block = var.public_subnet_cidrs + enable_dns_support = true + enable_dns_hostnames = true + + tags = { + name = "telemetry-vpc" + } +} + + +resource "aws_subnet" "public_subnets" { + vpc_id = module.telemetry-vpc.id + cidr_block = var.public_subnet_cidrs + availability_zone = var.azs + map_public_ip_on_launch = "true" + + tags = { + Name = "telemetry-public-subnet" + } +} + +resource "aws_internet_gateway" "gw" { + vpc_id = module.telemetry-vpc.id + + tags = { + Name = "telemetry-igw-public-subnet" + } + + lifecycle { + prevent_destroy = false + } +} + +resource "aws_route_table" "public_route_table" { + vpc_id = module.telemetry-vpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.gw.id + } + + route { + ipv6_cidr_block = "::/0" + gateway_id = aws_internet_gateway.gw.id + } + + tags = { + Name = "telemetry-public-route-tbl" + } + + depends_on = [ + aws_internet_gateway.gw + ] +} + +resource "aws_route_table_association" "public_route_table_subnets_association" { + subnet_id = aws_subnet.public_subnets.id + route_table_id = aws_route_table.public_route_table.id +} +resource "aws_security_group" "telemetry-subspace-sg" { + name = "telemetry-subspace-sg" + description = "Allow HTTP and HTTPS inbound traffic" + vpc_id = module.telemetry-vpc.id + + ingress { + description = "HTTPS for VPC" + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + description = "HTTP for VPC" + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + description = "SSH for VPC" + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + description = "egress for VPC" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags = { + Name = "telemetry-subspace-sg" + } + + depends_on = [ + module.telemetry_vpc + ] +} diff --git a/aws/telemetry/outputs.tf b/resources/telemetry/outputs.tf similarity index 100% rename from aws/telemetry/outputs.tf rename to resources/telemetry/outputs.tf diff --git a/resources/telemetry/provider.tf b/resources/telemetry/provider.tf new file mode 100644 index 00000000..70e3e592 --- /dev/null +++ b/resources/telemetry/provider.tf @@ -0,0 +1,31 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = ">=4.55.0" + } + + cloudflare = { + source = "cloudflare/cloudflare" + version = "~> 3.18.0" + } + } +} + +provider "aws" { + access_key = var.access_key + secret_key = var.secret_key + region = var.aws_region + default_tags { + tags = { + Environment = "Telemetry" + Owner = "subspace" + Project = "Subspace Network Telemetry APi" + } + } +} + +provider "cloudflare" { + email = var.cloudflare_email + api_token = var.cloudflare_api_token +} diff --git a/resources/telemetry/provision.tf b/resources/telemetry/provision.tf new file mode 100644 index 00000000..eceeb32d --- /dev/null +++ b/resources/telemetry/provision.tf @@ -0,0 +1,64 @@ +resource "null_resource" "setup-telemetry-server" { + + depends_on = [aws_instance.telemetry_subspace_node] + + connection { + host = aws_instance.telemetry_subspace_node.public_ip + user = var.ssh_user + type = "ssh" + agent = true + agent_identity = var.aws_key_name + private_key = file("${var.private_key_path}") + timeout = "300s" + } + + # create telemetry dir + provisioner "remote-exec" { + inline = [ + "mkdir -p /home/${var.ssh_user}/telemetry", + "sudo chown -R ${var.ssh_user}:${var.ssh_user} /home/${var.ssh_user}/telemetry/ && sudo chmod -R 750 /home/${var.ssh_user}/telemetry/" + ] + } + + # copy install file + provisioner "file" { + source = "${var.path_to_scripts}/installer.sh" + destination = "/home/${var.ssh_user}/telemetry/installer.sh" + } + +} + + +resource "null_resource" "start-telemetry-server" { + + depends_on = [null_resource.setup-telemetry-server] + + connection { + host = aws_instance.telemetry_subspace_node.public_ip + user = var.ssh_user + type = "ssh" + agent = true + agent_identity = var.aws_key_name + private_key = file("${var.private_key_path}") + timeout = "300s" + + } + + # install deployments + provisioner "remote-exec" { + inline = [ + # install nginx, certbot, docker and docker compose + "sudo bash /home/${var.ssh_user}/telemetry/installer.sh", + # set hostname + "sudo hostnamectl set-hostname telemetry-instance-1", + # start systemd services + "sudo systemctl daemon-reload", + # start docker daemon + "sudo systemctl enable --now docker.service", + "https://github.com/subspace/substrate-telemetry.git", + "sudo docker compose -f /home/${var.ssh_user}/substrate-telemetry/docker-compose.yml up -d", + "echo 'Installation Complete'", + ] + } + +} diff --git a/resources/telemetry/terraform.tfvars.example b/resources/telemetry/terraform.tfvars.example new file mode 100644 index 00000000..1f2b1cce --- /dev/null +++ b/resources/telemetry/terraform.tfvars.example @@ -0,0 +1,13 @@ +# AWS access key used to create infrastructure +access_key = "" +# AWS secret key used to create AWS infrastructure +secret_key = "" +vpc_id = "telemetry-vpc" +vpc_cidr_block = "172.31.0.0/16" +public_subnet_cidrs = "172.31.1.0/24" +aws_key_name = "deployer" +ssh_user = "ubuntu" +instance_type = "m6a.4xlarge" +disk_volume_size = 100 +cloudflare_email = "" +cloudflare_api_token = "" diff --git a/resources/telemetry/variables.tf b/resources/telemetry/variables.tf new file mode 100644 index 00000000..7cb723af --- /dev/null +++ b/resources/telemetry/variables.tf @@ -0,0 +1,108 @@ +variable "instance_type" { + default = "m6a.4xlarge" + type = string +} + +variable "vpc_id" { + default = "telemetry-vpc" + type = string +} + +variable "vpc_cidr_block" { + type = string +} + +variable "azs" { + type = string + description = "Availability Zones" + default = "us-west-2c" +} + +variable "instance_count" { + type = number + default = 1 +} + +variable "aws_region" { + description = "aws region" + type = string + default = "us-west-2" +} + +variable "public_subnet_cidrs" { + type = string + description = "Public Subnet CIDR values" + default = "172.31.1.0/24" +} + +variable "disk_volume_size" { + type = number +} + +variable "disk_volume_type" { + type = string + default = "gp3" +} + +variable "secret_key" { + type = string + sensitive = true +} + +variable "access_key" { + type = string + sensitive = true +} + +variable "aws_key_name" { + default = "deployer" + type = string +} + +variable "ssh_user" { + default = "ubuntu" + type = string +} + +variable "private_key_path" { + type = string + default = "~/.ssh/deployer.pem" +} + +variable "domain_prefix" { + type = string + default = "telemetry" + +} + +variable "cloudflare_email" { + type = string + description = "cloudflare email address" +} + +variable "cloudflare_api_token" { + type = string + description = "cloudflare api token" +} + +variable "path_to_scripts" { + description = "Path to the scripts" + type = string + default = "../../templates/scripts" +} + +variable "path_to_configs" { + description = "Path to the configs" + type = string + default = "../../templates/configs" +} + +variable "secret_key" { + type = string + sensitive = true +} + +variable "access_key" { + type = string + sensitive = true +}