Spaces:
Sleeping
Sleeping
File size: 2,323 Bytes
07ba06f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
# Key Pair for SSH access
resource "tls_private_key" "pk" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "kp" {
key_name = "swarmnet-deploy-key"
public_key = tls_private_key.pk.public_key_openssh
}
# Save private key locally
resource "local_file" "ssh_key" {
filename = "${path.module}/swarmnet-key.pem"
content = tls_private_key.pk.private_key_pem
file_permission = "0400"
}
# Security Group allowing HTTP and UDP DHT traffic
resource "aws_security_group" "swarmnet_sg" {
name = "swarmnet_security_group"
description = "Allow Swarmnet P2P traffic"
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Miner HTTP API"
from_port = 8000
to_port = 8005
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Miner Kademlia DHT (UDP)"
from_port = 8460
to_port = 8480
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Find latest Ubuntu 22.04 AMI
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
# EC2 Instance
resource "aws_instance" "swarmnet_miner" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro" # Free tier eligible, cheap for testing
key_name = aws_key_pair.kp.key_name
vpc_security_group_ids = [aws_security_group.swarmnet_sg.id]
# Automatically install docker via cloud-init
user_data = file("${path.module}/../cloud_init.yaml")
tags = {
Name = "Swarmnet-Cloud-Miner"
}
}
output "miner_public_ip" {
value = aws_instance.swarmnet_miner.public_ip
description = "The public IP address of the Swarmnet Cloud Miner"
}
output "ssh_command" {
value = "ssh -i swarmnet-key.pem ubuntu@${aws_instance.swarmnet_miner.public_ip}"
}
|